-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds EntityCaster for spiral/filters component
fixes #75
- Loading branch information
1 parent
09cac59
commit b933c0c
Showing
14 changed files
with
418 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cycle\Filter; | ||
|
||
use Cycle\ORM\ORMInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Spiral\Filters\Exception\SetterException; | ||
use Spiral\Filters\Model\FilterInterface; | ||
use Spiral\Filters\Model\Mapper\CasterInterface; | ||
|
||
final class EntityCaster implements CasterInterface | ||
{ | ||
/** | ||
* @var array<class-string, non-empty-string> | ||
*/ | ||
private static array $cache = []; | ||
private ?ORMInterface $orm = null; | ||
|
||
public function __construct( | ||
protected readonly ContainerInterface $container, | ||
) { | ||
} | ||
|
||
public function supports(\ReflectionNamedType $type): bool | ||
{ | ||
if ($type->isBuiltin()) { | ||
return false; | ||
} | ||
|
||
return $this->getOrm()->getSchema()->defines($type->getName()); | ||
} | ||
|
||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void | ||
{ | ||
$role = $this->resolveRole($property->getType()); | ||
$object = $this->getOrm()->getRepository($role)->findByPK($value); | ||
|
||
if ($object === null && !$property->getType()->allowsNull()) { | ||
throw new SetterException(message: \sprintf('Unable to find entity `%s` by primary key "%s"', $role, $value)); | ||
} | ||
|
||
$property->setValue($filter, $object); | ||
} | ||
|
||
private function resolveRole(\ReflectionNamedType $type): string | ||
{ | ||
if (isset(self::$cache[$type->getName()])) { | ||
return self::$cache[$type->getName()]; | ||
} | ||
|
||
$role = $this->getOrm()->resolveRole($type->getName()); | ||
self::$cache[$type->getName()] = $role; | ||
|
||
return $role; | ||
} | ||
|
||
private function getOrm(): ORMInterface | ||
{ | ||
if ($this->orm === null) { | ||
$this->orm = $this->container->get(ORMInterface::class); | ||
} | ||
|
||
return $this->orm; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Controller\Filter; | ||
|
||
use Spiral\App\Entities\Role; | ||
use Spiral\Filters\Attribute\Input\Post; | ||
use Spiral\Filters\Model\Filter; | ||
|
||
final class RoleFilter extends Filter | ||
{ | ||
#[Post] | ||
public string $name; | ||
|
||
#[Post] | ||
public Role $role; | ||
|
||
#[Post] | ||
public ?Role $nullableRole; | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Controller; | ||
|
||
use Spiral\App\Controller\Filter\RoleFilter; | ||
use Spiral\App\Entities\Role; | ||
use Spiral\Router\Annotation\Route; | ||
|
||
final class RoleController | ||
{ | ||
#[Route(route: "/role", methods: ["POST"])] | ||
public function create(RoleFilter $filter): array | ||
{ | ||
return [ | ||
'name' => $filter->name, | ||
'role' => $filter->role->name, | ||
'id' => $filter->role->id, | ||
]; | ||
} | ||
|
||
#[Route(route: "/role/<role>", methods: ["GET"])] | ||
public function show(Role $role): array | ||
{ | ||
return [ | ||
'name' => $role->name, | ||
'id' => $role->id, | ||
]; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Database\Factory; | ||
|
||
use Spiral\App\Entities\Role; | ||
use Spiral\DatabaseSeeder\Factory\AbstractFactory; | ||
use Spiral\DatabaseSeeder\Factory\FactoryInterface; | ||
|
||
/** | ||
* @implements FactoryInterface<Role> | ||
*/ | ||
final class RoleFactory extends AbstractFactory | ||
{ | ||
public function makeEntity(array $definition): object | ||
{ | ||
return new Role($definition['name']); | ||
} | ||
|
||
public function entity(): string | ||
{ | ||
return Role::class; | ||
} | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->word, | ||
]; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Database\Factory; | ||
|
||
use Spiral\App\Entities\Role; | ||
use Spiral\App\Entities\User; | ||
use Spiral\DatabaseSeeder\Factory\AbstractFactory; | ||
use Spiral\DatabaseSeeder\Factory\FactoryInterface; | ||
|
||
/** | ||
* @implements FactoryInterface<User> | ||
*/ | ||
final class UserFactory extends AbstractFactory | ||
{ | ||
public function makeEntity(array $definition): object | ||
{ | ||
$user = new User($definition['name']); | ||
$user->email = $definition['email']; | ||
$user->company = $definition['company']; | ||
|
||
return $user; | ||
} | ||
|
||
public function addRole(Role $role): self | ||
{ | ||
return $this->entityState(static function (User $user) use ($role) { | ||
$user->roles->add($role); | ||
|
||
return $user; | ||
}); | ||
} | ||
|
||
public function entity(): string | ||
{ | ||
return User::class; | ||
} | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->name, | ||
'email' => $this->faker->email, | ||
'company' => $this->faker->company, | ||
]; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests; | ||
|
||
use Spiral\DatabaseSeeder\Database\Traits\DatabaseAsserts; | ||
use Spiral\DatabaseSeeder\Database\Traits\Helper; | ||
use Spiral\DatabaseSeeder\Database\Traits\ShowQueries; | ||
use Spiral\DatabaseSeeder\Database\Traits\Transactions; | ||
|
||
abstract class DatabaseTest extends BaseTest | ||
{ | ||
use Transactions, Helper, DatabaseAsserts, ShowQueries; | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
$this->cleanIdentityMap(); | ||
$this->getCurrentDatabaseDriver()->disconnect(); | ||
} | ||
|
||
public function persist(object ...$entity): void | ||
{ | ||
$em = $this->getEntityManager(); | ||
foreach ($entity as $e) { | ||
$em->persist($e); | ||
} | ||
$em->run(); | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* @param T $entity | ||
* @return T | ||
*/ | ||
public function refreshEntity(object $entity, string $pkField = 'uuid'): object | ||
{ | ||
return $this->getRepositoryFor($entity)->findByPK($entity->{$pkField}); | ||
} | ||
} |
Oops, something went wrong.