1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Mapper\Proxy; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Cycle\ORM\Mapper\Proxy\Hydrator\PropertyMap; |
9
|
|
|
use Cycle\ORM\Reference\ReferenceInterface; |
10
|
|
|
use Cycle\ORM\RelationMap; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
trait EntityProxyTrait |
17
|
|
|
{ |
18
|
|
|
public RelationMap $__cycle_orm_rel_map; |
19
|
|
|
public PropertyMap $__cycle_orm_relation_props; |
20
|
|
|
public array $__cycle_orm_rel_data = []; |
21
|
|
|
|
22
|
840 |
|
public function __get(string $name) |
23
|
|
|
{ |
24
|
840 |
|
$relation = $this->__cycle_orm_rel_map->getRelations()[$name] ?? null; |
25
|
840 |
|
if ($relation === null) { |
26
|
2 |
|
return method_exists(parent::class, '__get') |
27
|
2 |
|
? parent::__get($name) |
28
|
2 |
|
: $this->$name; |
29
|
|
|
} |
30
|
|
|
|
31
|
838 |
|
$value = $this->__cycle_orm_rel_data[$name] ?? null; |
32
|
838 |
|
if ($value instanceof ReferenceInterface) { |
33
|
838 |
|
$value = $relation->collect($relation->resolve($value, true)); |
34
|
838 |
|
$this->$name = $value; |
35
|
838 |
|
unset($this->__cycle_orm_rel_data[$name]); |
36
|
838 |
|
return $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
throw new RuntimeException(sprintf('Property %s.%s is not initialized.', get_parent_class(static::class), $name)); |
40
|
|
|
} |
41
|
|
|
|
42
|
3882 |
|
public function __unset(string $name): void |
43
|
|
|
{ |
44
|
3882 |
|
if (\array_key_exists($name, $this->__cycle_orm_rel_map->getRelations())) { |
45
|
2636 |
|
$propertyClass = $this->__cycle_orm_relation_props->getPropertyClass($name); |
46
|
4 |
|
if ($propertyClass === PropertyMap::PUBLIC_CLASS) { |
47
|
|
|
unset($this->$name); |
48
|
2636 |
|
} else { |
49
|
|
|
Closure::bind(static function (object $object, string $property): void { |
50
|
|
|
unset($object->{$property}); |
51
|
3678 |
|
}, null, $propertyClass)($this, $name); |
52
|
2158 |
|
} |
53
|
2158 |
|
} |
54
|
|
|
if (\method_exists(parent::class, '__unset')) { |
55
|
3024 |
|
parent::__unset($name); |
56
|
|
|
} |
57
|
3024 |
|
} |
58
|
3024 |
|
|
59
|
2900 |
|
public function __set(string $name, $value): void |
60
|
|
|
{ |
61
|
132 |
|
if (!array_key_exists($name, $this->__cycle_orm_rel_map->getRelations())) { |
62
|
132 |
|
if (method_exists(parent::class, '__set')) { |
63
|
|
|
parent::__set($name, $value); |
64
|
|
|
} |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($value instanceof ReferenceInterface) { |
69
|
|
|
$this->__cycle_orm_rel_data[$name] = $value; |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
unset($this->__cycle_orm_rel_data[$name]); |
73
|
|
|
|
74
|
|
|
$propertyClass = $this->__cycle_orm_relation_props->getPropertyClass($name); |
75
|
|
|
if ($propertyClass === PropertyMap::PUBLIC_CLASS) { |
76
|
|
|
$this->$name = $value; |
77
|
|
|
} else { |
78
|
|
|
Closure::bind(static function (object $object, string $property, $value): void { |
79
|
|
|
$object->{$property} = $value; |
80
|
|
|
}, null, $propertyClass)($this, $name, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function __debugInfo(): array |
85
|
|
|
{ |
86
|
|
|
$result = method_exists(parent::class, '__debugInfo') ? parent::__debugInfo() : (array)$this; |
87
|
|
|
unset($result['__cycle_orm_rel_map'], $result['__cycle_orm_rel_data'], $result['__cycle_orm_relation_props']); |
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|