-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuiltInTypeStrategy.php
46 lines (38 loc) · 1.32 KB
/
BuiltInTypeStrategy.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 EvolveORM\Bundle\Strategy;
use ReflectionNamedType;
use ReflectionProperty;
/**
* Стратегия гидрации для значений встроенных типов PHP.
*
* Эта стратегия обрабатывает гидрацию свойств объекта, имеющих встроенные типы PHP
* (такие как int, string, bool и т.д.).
*/
class BuiltInTypeStrategy implements HydrationStrategy
{
/** @inheritDoc */
public function canHydrate(ReflectionProperty $property, mixed $value): bool
{
return !$property->isStatic()
&& $property->getType() instanceof ReflectionNamedType
&& $property->getType()->isBuiltin()
&& (
($value === null && $property->getType()->allowsNull())
|| $value !== null
);
}
/** @inheritDoc */
public function hydrate(object $object, ReflectionProperty $property, mixed $value): void
{
if (!$property->isPublic()) {
$property->setAccessible(true);
}
if ($value !== null) {
/** @var ReflectionNamedType $type */
$type = $property->getType();
settype($value, $type->getName());
}
$property->setValue($object, $value);
}
}