Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 0.4.0

### Added

- Type hinting for all functions.

### Changed

- PHP 8.0 is now the minimum required version.
- `phpunit^9.0` is now the minimum required version.
- Consolidated some redundant functions into one.

## 0.3.1

### Fixed
Expand All @@ -17,29 +29,24 @@
- Removed (temporary) paysera/lib-php-cs-fixer-config

## 0.2.2

### Fixed
- Saves originalData and introduces method `getOriginalData` to access it; Adds method `getDataAsArray` to get data in array form
- remove clone
- improve array handling in getObjectWrapperAsArray
- improve array handling in getObjectWrapperAsArray once more

## 0.2.1

### Fixed
- improve array handling in getObjectWrapperAsArray

## 0.2.0

### Added
- Ability to access originally passed data

## 0.1.1

### Fixed
- Fixed mistype for exception message

## 0.1.0

### Fixed
- Fixes strange test case with PHP 7.0
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
}
},
"require": {
"php": "^7.0 || ^8.0"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0 || ^9.0"
"phpunit/phpunit": "^9.0|^10.0"
},
"autoload-dev": {
"psr-4": {
Expand Down
113 changes: 39 additions & 74 deletions src/ObjectWrapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Paysera\Component\ObjectWrapper;
Expand All @@ -13,11 +14,11 @@

class ObjectWrapper implements ArrayAccess, IteratorAggregate
{
private $data;
private $originalData;
private $path;
private stdClass $data;
private stdClass $originalData;
private array $path;

public function __construct(stdClass $data, $path = [])
public function __construct(stdClass $data, array $path = [])
{
$this->path = $path;
$this->data = clone $data;
Expand All @@ -26,14 +27,14 @@ public function __construct(stdClass $data, $path = [])
$this->processData();
}

private function processData()
private function processData(): void
{
foreach ($this->data as $key => &$item) {
$item = $this->processItem($item, [$key]);
}
}

private function processItem($item, array $keys)
private function processItem(mixed $item, array $keys): mixed
{
if ($item instanceof stdClass) {
return new self($item, array_merge($this->path, $keys));
Expand All @@ -44,7 +45,7 @@ private function processItem($item, array $keys)
return $item;
}

private function processArray(array $data, array $keys)
private function processArray(array $data, array $keys): array
{
foreach ($data as $i => &$item) {
$item = $this->processItem($item, array_merge($keys, [(string)$i]));
Expand All @@ -53,32 +54,38 @@ private function processArray(array $data, array $keys)
return $data;
}

public function offsetExists($key)
public function offsetExists(mixed $offset): bool
{
return isset($this->data->$key);
return isset($this->data->$offset);
}

public function offsetGet($key)
public function offsetGet(mixed $offset): mixed
{
return isset($this->data->$key) ? $this->data->$key : null;
return $this->data->$offset ?? null;
}

public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
throw new RuntimeException('Modifying ObjectWrapper is not allowed');
}

public function offsetUnset($offset)
public function offsetUnset(mixed $offset): void
{
throw new RuntimeException('Modifying ObjectWrapper is not allowed');
}

public function getIterator()
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->data);
}

public function getRequired(string $key)
/**
* @param string $key
*
* @return mixed
* @throws MissingItemException
*/
public function getRequired(string $key): mixed
{
if (!isset($this->data->$key)) {
throw new MissingItemException($this->buildKey($key));
Expand All @@ -92,12 +99,7 @@ public function getRequiredBool(string $key): bool
return $this->getRequiredOfType($key, 'boolean');
}

/**
* @param string $key
* @param bool|null $default
* @return bool|null
*/
public function getBool(string $key, bool $default = null)
public function getBool(string $key, ?bool $default = null): ?bool
{
return $this->getOfType($key, 'boolean', $default);
}
Expand All @@ -107,12 +109,7 @@ public function getRequiredFloat(string $key): float
return $this->getRequiredOfType($key, 'float');
}

/**
* @param string $key
* @param float|null $default
* @return float|null
*/
public function getFloat(string $key, float $default = null)
public function getFloat(string $key, ?float $default = null): ?float
{
return $this->getOfType($key, 'float', $default);
}
Expand All @@ -122,26 +119,17 @@ public function getRequiredInt(string $key): int
return $this->getRequiredOfType($key, 'integer');
}

/**
* @param string $key
* @param int|null $default
* @return int|null
*/
public function getInt(string $key, int $default = null)
public function getInt(string $key, ?int $default = null): ?int
{
return $this->getOfType($key, 'integer', $default);
}

public function getRequiredObject(string $key): self
public function getRequiredObject(string $key): mixed
{
return $this->getRequiredOfType($key, 'object');
}

/**
* @param string $key
* @return ObjectWrapper|null
*/
public function getObject(string $key)
public function getObject(string $key): ?ObjectWrapper
{
return $this->getOfType($key, 'object', null);
}
Expand All @@ -151,12 +139,7 @@ public function getRequiredString(string $key): string
return $this->getRequiredOfType($key, 'string');
}

/**
* @param string $key
* @param string|null $default
* @return string|null
*/
public function getString(string $key, string $default = null)
public function getString(string $key, ?string $default = null): ?string
{
return $this->getOfType($key, 'string', $default);
}
Expand All @@ -173,34 +156,15 @@ public function getOriginalData(): stdClass

public function getDataAsArray(): array
{
return $this->getObjectWrapperAsArray($this);
}

private function getObjectWrapperAsArray(ObjectWrapper $objectWrapper)
{
$data = [];
foreach ($objectWrapper as $key => $item) {
if ($item instanceof ObjectWrapper) {
$data[$key] = $this->getObjectWrapperAsArray($item);
continue;
} elseif (is_array($item)) {
$data[$key] = $this->getObjectWrapperFromArray($item);
} else {
$data[$key] = $item;
}
}

return $data;
return $this->recursiveToArray($this);
}

private function getObjectWrapperFromArray(array $list)
private function recursiveToArray(ArrayAccess|array $inputData): array
{
$data = [];
foreach ($list as $key => $item) {
if ($item instanceof ObjectWrapper) {
$data[$key] = $this->getObjectWrapperAsArray($item);
} elseif (is_array($item)) {
$data[$key] = $this->getObjectWrapperFromArray($item);
foreach ($inputData as $key => $item) {
if ($item instanceof ArrayAccess || is_array($item)) {
$data[$key] = $this->recursiveToArray($item);
} else {
$data[$key] = $item;
}
Expand All @@ -209,14 +173,14 @@ private function getObjectWrapperFromArray(array $list)
return $data;
}

private function getRequiredOfType(string $key, string $expectedType)
private function getRequiredOfType(string $key, string $expectedType): mixed
{
$value = $this->getRequired($key);

return $this->assertValueType($value, $expectedType, $key);
}

private function getOfType(string $key, string $expectedType, $default)
private function getOfType(string $key, string $expectedType, $default): mixed
{
$value = $this[$key];
if ($value === null) {
Expand All @@ -230,10 +194,11 @@ private function getOfType(string $key, string $expectedType, $default)
* @param mixed $value
* @param string $expectedType
* @param string $key
*
* @return mixed
* @throws InvalidItemTypeException
*/
private function assertValueType($value, string $expectedType, string $key)
private function assertValueType(mixed $value, string $expectedType, string $key): mixed
{
$givenType = gettype($value);
if ($givenType === 'double') {
Expand Down Expand Up @@ -289,7 +254,7 @@ public function getArrayOfObject(string $key): array
return $this->getArrayOfType($key, 'object');
}

private function getArrayOfType(string $key, string $expectedType)
private function getArrayOfType(string $key, string $expectedType): array
{
$value = $this->getArray($key);

Expand All @@ -298,7 +263,7 @@ private function getArrayOfType(string $key, string $expectedType)
}, $value);
}

private function buildKey(string $key)
private function buildKey(string $key): string
{
return implode('.', array_merge($this->path, [$key]));
}
Expand Down
Loading