Skip to content
Closed
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
6 changes: 3 additions & 3 deletions tests/Annotated/Fixtures/Fixtures6/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
])]
class Address
{
/** @Column(type="string") */
#[Column(type: 'string')]
protected $city;
/** @Column(type="string", typecast="city") */
#[Column(type: 'string', typecast: CityTypecast::NAME)]
protected City $city;

/** @Column(type="string") */
#[Column(type: 'string')]
Expand Down
27 changes: 27 additions & 0 deletions tests/Annotated/Fixtures/Fixtures6/City.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Fixtures6;

use Stringable;

final class City implements Stringable
{
private string $value;

private function __construct(string $value)
{
$this->value = $value;
}

public function __toString(): string
{
return $this->value;
}

public static function fromString(string $value): self
{
return new self($value);
}
}
70 changes: 70 additions & 0 deletions tests/Annotated/Fixtures/Fixtures6/CityTypecast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Fixtures6;

use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;

final class CityTypecast implements CastableInterface, UncastableInterface
{
public const NAME = 'city';

private array $rules = [];

public function cast(array $data): array
{
foreach (array_keys($this->rules) as $column) {
if (!isset($data[$column])) {
continue;
}

if (!\is_string($data[$column]) || $data[$column] === '') {
$data[$column] = null;

continue;
}

$data[$column] = City::fromString($data[$column]);
}

return $data;
}

public function uncast(array $data): array
{
foreach (array_keys($this->rules) as $column) {
if (!isset($data[$column])) {
continue;
}

/** @var T $value */
$value = $data[$column];

if (!$value instanceof City) {
continue;
}

$data[$column] = (string)$value;
}

return $data;
}

public function setRules(array $rules): array
{
/** @var non-empty-string $rule */
foreach ($rules as $key => $rule) {
if ($rule !== self::NAME) {
continue;
}

unset($rules[$key]);

$this->rules[$key] = $rule;
}

return $rules;
}
}
8 changes: 7 additions & 1 deletion tests/Annotated/Fixtures/Fixtures6/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\Embedded;
use Cycle\ORM\Parser\Typecast as DefaultTypecast;

/**
* @Entity()
*/
#[Entity]
#[Entity(
typecast: [
CityTypecast::class,
DefaultTypecast::class,
]
)]
class User
{
/** @Column(type="primary") */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\Annotated\MergeColumns;
use Cycle\Annotated\MergeIndexes;
use Cycle\Annotated\Tests\Fixtures\Fixtures6\CityTypecast;
use Cycle\Annotated\Tests\Functional\Driver\Common\BaseTestCase;
use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
Expand Down Expand Up @@ -56,6 +57,9 @@ public function testRelation(ReaderInterface $reader): void
$this->assertArrayHasKey('address', $schema['user'][Schema::RELATIONS]);
$this->assertSame(Relation::EMBEDDED, $schema['user'][Schema::RELATIONS]['address'][Relation::TYPE]);

$this->assertSame(CityTypecast::class, $schema['user'][Schema::TYPECAST_HANDLER][0]);
$this->assertSame(CityTypecast::class, $schema['user:address:address'][Schema::TYPECAST_HANDLER][0]);

$this->assertSame('user:address:address', $schema['user'][Schema::RELATIONS]['address'][Relation::TARGET]);
$this->assertSame(Relation::LOAD_EAGER, $schema['user'][Schema::RELATIONS]['address'][Relation::LOAD]);
$this->assertTrue($r->getTableSchema($r->getEntity('user'))->hasIndex(['address_zipcode']));
Expand Down
Loading