Skip to content

Commit

Permalink
Merge pull request #7 from tenantcloud/v1.1.1
Browse files Browse the repository at this point in the history
feature: Add serialize with Enum values.
  • Loading branch information
KlebanAndrew authored Apr 22, 2022
2 parents 606a510 + e4065be commit 1e6eabb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=7.4 || ^8.0",
"ext-json": "*",
"illuminate/support": "^8.0 || ^9.0",
"spatie/macroable": "1.0.1"
"spatie/macroable": "1.0.1",
"tenantcloud/php-standard": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
17 changes: 14 additions & 3 deletions src/TenantCloud/DataTransferObjects/ExampleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

namespace TenantCloud\DataTransferObjects;

use TenantCloud\Standard\Enum\ValueEnum;
use Tests\Stubs\TestEnum;

/**
* @method bool hasFoo()
* @method self setFoo($foo)
* @method mixed getFoo()
* @method bool hasFoo()
* @method self setFoo($foo)
* @method mixed getFoo()
* @method bool hasEnum()
* @method self setEnum(ValueEnum $enum)
* @method ValueEnum getEnum()
*/
class ExampleDTO extends DataTransferObject
{
protected array $enums = [
'enum' => TestEnum::class,
];

protected array $fields = [
'foo',
'enum',
];
}
40 changes: 40 additions & 0 deletions src/TenantCloud/DataTransferObjects/IsDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use TenantCloud\Standard\Enum\ValueEnum;
use Webmozart\Assert\Assert;

/**
Expand All @@ -17,6 +18,9 @@ trait IsDataTransferObject

protected array $fields = [];

/** @var array<string, class-string<ValueEnum>> */
protected array $enums = [];

private array $data = [];

/**
Expand All @@ -42,6 +46,42 @@ public function __call($method, $arguments)
static::throwBadMethodCallException($method);
}

public function __serialize(): array
{
$data = $this->all();

foreach ($this->enums as $key => $item) {
$enum = Arr::get($data, $key);

if ($enum instanceof ValueEnum) {
Arr::set($data, $key, $enum->value());
}
}

return [
'fields' => $this->fields,
'enums' => $this->enums,
'data' => $data,
];
}

public function __unserialize(array $data): void
{
$this->fields = $data['fields'];
$this->enums = $data['enums'];

$dataItems = $data['data'];

foreach ($this->enums as $index => $enum) {
if (Arr::has($dataItems, $index)) {
/* @var ValueEnum|null $enum */
Arr::set($dataItems, $index, $enum::fromValue(Arr::get($dataItems, $index)));
}
}

$this->data = $dataItems;
}

/**
* @return static
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@

use Orchestra\Testbench\TestCase;
use TenantCloud\DataTransferObjects\ExampleDTO;
use TenantCloud\Standard\Enum\ValueEnum;
use Tests\Stubs\TestEnum;

class ExampleTest extends TestCase
{
public function testExample(): void
{
self::assertNotNull(ExampleDTO::create());
}

public function testSerializeWithDTO(): void
{
$dto = ExampleDTO::from([
'enum' => TestEnum::$ONE,
]);

$serialized = serialize($dto);

/** @var ExampleDTO $unserialized */
$unserialized = unserialize($serialized);

self::assertInstanceOf(ValueEnum::class, $unserialized->getEnum());
self::assertEquals(1, $unserialized->getEnum()->value());
}
}
15 changes: 15 additions & 0 deletions tests/Stubs/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Stubs;

use TenantCloud\Standard\Enum\ValueEnum;

class TestEnum extends ValueEnum
{
public static TestEnum $ONE;

protected static function initializeInstances(): void
{
self::$ONE = new self(1);
}
}

0 comments on commit 1e6eabb

Please sign in to comment.