-
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.
Merge branch '2.x' into annotated-4-0
- Loading branch information
Showing
20 changed files
with
471 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cycle\Filter; | ||
|
||
use Cycle\ORM\ORMInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Spiral\Exceptions\ExceptionReporterInterface; | ||
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, | ||
protected readonly ExceptionReporterInterface $reporter, | ||
) { | ||
} | ||
|
||
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 | ||
{ | ||
try { | ||
$role = $this->resolveRole($property->getType()); | ||
$object = $this->getOrm()->getRepository($role)->findByPK($value); | ||
} catch (\Throwable $e) { | ||
$this->reporter->report($e); | ||
throw new SetterException(previous: $e); | ||
} | ||
|
||
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
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
Oops, something went wrong.