/
UuidCaster.php
46 lines (35 loc) · 1.18 KB
/
UuidCaster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Spiral\Filters\Model\Mapper;
use Ramsey\Uuid\UuidInterface;
use Spiral\Filters\Model\FilterInterface;
final class UuidCaster implements CasterInterface
{
private ?bool $interfaceExists = null;
public function supports(\ReflectionNamedType $type): bool
{
if ($this->interfaceExists === null) {
$this->interfaceExists = \interface_exists(UuidInterface::class);
}
return $this->interfaceExists && $this->implements($type->getName(), UuidInterface::class);
}
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void
{
$property->setValue($filter, \Ramsey\Uuid\Uuid::fromString($value));
}
private function implements(string $haystack, string $interface): bool
{
if ($haystack === $interface) {
return true;
}
foreach ((array)\class_implements($haystack) as $implements) {
if ($implements === $interface) {
return true;
}
if (self::implements($implements, $interface)) {
return true;
}
}
return false;
}
}