Skip to content

Commit

Permalink
Merge pull request #50 from CrazyTapok-bit/dev-master
Browse files Browse the repository at this point in the history
Improv: Arrayable converts only Make objects
  • Loading branch information
CrazyTapok-bit authored Jul 2, 2023
2 parents 477eeeb + 6819f56 commit d57d822
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Make/InitData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class InitData extends Make
/**
* @param array<string, int|string|bool> $props
*/
public function __construct(array $props)
public function __construct(array $props = [])
{
foreach ($props as $prop => $value) {
$value = match ($prop) {
Expand Down
2 changes: 1 addition & 1 deletion src/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class Make extends Arrayable
/**
* @param array<string, int|string|bool> $props
*/
public function __construct(array $props)
public function __construct(array $props = [])
{
foreach ($props as $prop => $value) {
$this->setProperty(camelize($prop), $value);
Expand Down
8 changes: 2 additions & 6 deletions src/Support/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ReflectionClass;
use ReflectionProperty;
use TgWebValid\Make\Make;

abstract class Arrayable
{
Expand All @@ -26,12 +27,7 @@ public function toArray(?object $object = null): array
$name = $property->getName();
$value = $object->$name ?? null;
$result[$name] = match(true) {
is_object($value) => $this->toArray($value),
is_array($value) => array_map(function($item) {
return is_object($item)
? $this->toArray($item)
: $item;
}, $value),
$value instanceof Make => $this->toArray($value),
default => $value
};
}
Expand Down
24 changes: 20 additions & 4 deletions tests/Support/ArrayableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace TgWebValid\Test\Support;

use PHPUnit\Framework\TestCase;
use stdClass;
use TgWebValid\Make\Make;
use TgWebValid\Support\Arrayable;

final class ArrayableTest extends TestCase
Expand Down Expand Up @@ -58,13 +60,18 @@ public function testPropertyAccess(): void

public function testPropertyDeep(): object
{
$user = new class
$user = new class ($this->createStdClass()) extends Make
{
public int $id;
public string $firstName = 'Сергій';
public function __construct(
public stdClass $stdClass
)
{
}
};

$initData = new class ($user)
$initData = new class ($user) extends Make
{
public ?string $queryId = null;
public function __construct(
Expand All @@ -88,7 +95,8 @@ public function __construct(
'queryId' => null,
'user' => [
'id' => null,
'firstName' => 'Сергій'
'firstName' => 'Сергій',
'stdClass' => $this->createStdClass()
]
]
], $arrayable->toArray());
Expand All @@ -107,9 +115,17 @@ public function testAlien(object $object): void
'queryId' => null,
'user' => [
'id' => null,
'firstName' => 'Сергій'
'firstName' => 'Сергій',
'stdClass' => $this->createStdClass()
]
]
], $arrayable->toArray($object));
}

public function createStdClass(): stdClass
{
$stdClass = new stdClass;
$stdClass->field = 'test';
return $stdClass;
}
}

0 comments on commit d57d822

Please sign in to comment.