generated from vjik/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\CycleTypecast; | ||
|
||
use Cycle\ORM\Parser\CastableInterface; | ||
use Cycle\ORM\Parser\UncastableInterface; | ||
use Cycle\ORM\SchemaInterface; | ||
use ReflectionAttribute; | ||
use ReflectionClass; | ||
|
||
use function class_exists; | ||
|
||
final class AttributeTypecastHandler implements CastableInterface, UncastableInterface | ||
{ | ||
/** | ||
* @var TypeInterface[] | ||
* @psalm-var array<string, TypeInterface> | ||
*/ | ||
private array $types = []; | ||
|
||
public function __construct(SchemaInterface $schema, string $role) | ||
{ | ||
$entityClass = $schema->define($role, SchemaInterface::ENTITY); | ||
if (is_string($entityClass) && class_exists($entityClass)) { | ||
$reflection = new ReflectionClass($entityClass); | ||
foreach ($reflection->getProperties() as $property) { | ||
$attributes = $property->getAttributes(TypeInterface::class, ReflectionAttribute::IS_INSTANCEOF); | ||
if (empty($attributes)) { | ||
continue; | ||
Check warning on line 31 in src/AttributeTypecastHandler.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
} | ||
$this->types[$property->getName()] = $attributes[0]->newInstance(); | ||
} | ||
} | ||
} | ||
|
||
public function setRules(array $rules): array | ||
{ | ||
foreach ($rules as $key => $_rule) { | ||
if (isset($this->types[$key])) { | ||
unset($rules[$key]); | ||
} | ||
} | ||
return $rules; | ||
Check warning on line 45 in src/AttributeTypecastHandler.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
} | ||
|
||
public function cast(array $data): array | ||
{ | ||
/** @psalm-var array<non-empty-string, mixed> $data */ | ||
foreach ($data as $key => $value) { | ||
if (isset($this->types[$key])) { | ||
$data[$key] = $this->types[$key]->convertToPhpValue($value); | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
public function uncast(array $data): array | ||
{ | ||
/** @psalm-var array<non-empty-string, mixed> $data */ | ||
foreach ($data as $key => $value) { | ||
if (isset($this->types[$key])) { | ||
$data[$key] = $this->types[$key]->convertToDatabaseValue($value); | ||
} | ||
} | ||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\CycleTypecast\Tests; | ||
|
||
use Cycle\ORM\SchemaInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Ramsey\Uuid\Uuid; | ||
use Vjik\CycleTypecast\AttributeTypecastHandler; | ||
use Vjik\CycleTypecast\Tests\Support\EntityWithAttributes; | ||
|
||
final class AttributeTypecastHandlerTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$schema = $this->createMock(SchemaInterface::class); | ||
$schema | ||
->method('define') | ||
->with('role_user', SchemaInterface::ENTITY) | ||
->willReturn(EntityWithAttributes::class); | ||
|
||
$typecastHandler = new AttributeTypecastHandler($schema, 'role_user'); | ||
|
||
$uuid = Uuid::fromString('1f2d3897-a226-4eec-bd2c-d0145ef25df9'); | ||
|
||
$data = $typecastHandler->uncast([ | ||
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9', | ||
'names' => ['John', 'Doe'], | ||
]); | ||
$this->assertSame( | ||
[ | ||
'id' => $uuid->getBytes(), | ||
'names' => 'John,Doe', | ||
], | ||
$data, | ||
); | ||
|
||
$data = $typecastHandler->cast([ | ||
'id' => $uuid->getBytes(), | ||
'names' => 'John,Doe', | ||
]); | ||
$this->assertSame( | ||
[ | ||
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9', | ||
'names' => ['John', 'Doe'], | ||
], | ||
$data, | ||
); | ||
|
||
$rules = $typecastHandler->setRules([ | ||
'id' => 'string', | ||
'names' => 'array', | ||
'age' => 'int', | ||
]); | ||
$this->assertSame(['age' => 'int'], $rules); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\CycleTypecast\Tests\Support; | ||
|
||
use Vjik\CycleTypecast\ArrayToStringType; | ||
use Vjik\CycleTypecast\UuidString\UuidStringToBytesType; | ||
|
||
final class EntityWithAttributes | ||
{ | ||
#[UuidStringToBytesType] | ||
public string $id; | ||
|
||
#[ArrayToStringType(',')] | ||
public array $names; | ||
|
||
public int $age; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters