|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Sirius\Orm\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use Sirius\Orm\CastingManager; |
|
7
|
|
|
use Sirius\Orm\Contract\EntityInterface; |
|
8
|
|
|
use Sirius\Orm\Contract\HydratorInterface; |
|
9
|
|
|
use Sirius\Orm\Contract\Relation\ToManyInterface; |
|
10
|
|
|
use Sirius\Orm\Contract\Relation\ToOneInterface; |
|
11
|
|
|
use Sirius\Orm\Helpers\Arr; |
|
12
|
|
|
use Sirius\Orm\Mapper; |
|
13
|
|
|
use Sirius\Orm\MapperConfig; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractHydrator implements HydratorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CastingManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $castingManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Mapper |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $mapper; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(CastingManager $castingManager) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->castingManager = $castingManager; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setMapper(Mapper $mapper) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->mapper = $mapper; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $attributes |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed|GenericEntity |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function hydrate(array $attributes = []); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param EntityInterface $entity |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function extract(EntityInterface $entity) |
|
50
|
|
|
{ |
|
51
|
|
|
$data = Arr::renameKeys( |
|
52
|
|
|
$entity->toArray(), |
|
53
|
|
|
array_flip($this->getMapperConfig()->getColumnAttributeMap()) |
|
54
|
|
|
); |
|
55
|
|
|
if ($this->castingManager) { |
|
56
|
|
|
$data = $this->castingManager |
|
57
|
|
|
->castArrayForDb($data, $this->getMapperConfig()->getCasts()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return Arr::only($data, $this->getMapperConfig()->getColumns()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
abstract public function get(EntityInterface $entity, string $attribute); |
|
64
|
|
|
|
|
65
|
|
|
abstract public function set(EntityInterface $entity, string $attribute, $value); |
|
66
|
|
|
|
|
67
|
|
|
public function getPk($entity) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->get($entity, $this->getMapperConfig()->getPrimaryKey()); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setPk($entity, $value) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->set($entity, $this->getMapperConfig()->getPrimaryKey(), $value); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function compileRelations(array $attributes) |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->mapper->getRelations() as $name) { |
|
80
|
|
|
$relation = $this->mapper->getRelation($name); |
|
81
|
|
|
if ($relation instanceof ToOneInterface && |
|
82
|
|
|
isset($attributes[$name]) && |
|
83
|
|
|
! is_object($attributes[$name])) { |
|
84
|
|
|
$attributes[$name] = $relation->getForeignMapper()->newEntity($attributes[$name]); |
|
85
|
|
|
} elseif ($relation instanceof ToManyInterface && |
|
86
|
|
|
! $relation instanceof ToOneInterface |
|
87
|
|
|
&& ( ! isset($attributes[$name]) || is_array($attributes[$name]))) { |
|
88
|
|
|
/** |
|
89
|
|
|
* we also need to check against ToOneInterface because OneToOne relation extends |
|
90
|
|
|
* OneToMany which implements ToOneInterface |
|
91
|
|
|
* @todo remove this quirk |
|
92
|
|
|
*/ |
|
93
|
|
|
$attributes[$name] = $relation->getForeignMapper()->newCollection($attributes[$name] ?? []); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $attributes; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getMapperConfig(): MapperConfig |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->mapper->getConfig(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|