|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Sirius\Orm; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Sirius\Orm\Collection\Collection; |
|
8
|
|
|
use Sirius\Orm\Entity\EntityInterface; |
|
9
|
|
|
use Sirius\Orm\Helpers\Str; |
|
10
|
|
|
use Sirius\Orm\Relation\Relation; |
|
11
|
|
|
use Sirius\Orm\Relation\RelationBuilder; |
|
12
|
|
|
use Sirius\Orm\Relation\RelationConfig; |
|
13
|
|
|
|
|
14
|
|
|
class Orm |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $mappers = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $lazyMappers = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ConnectionLocator |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $connectionLocator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CastingManager |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $castingManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RelationBuilder |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $relationBuilder; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
ConnectionLocator $connectionLocator, |
|
43
|
|
|
CastingManager $castingManager = null |
|
44
|
|
|
) { |
|
45
|
|
|
$this->connectionLocator = $connectionLocator; |
|
46
|
|
|
|
|
47
|
|
|
if (! $castingManager) { |
|
48
|
|
|
$castingManager = new CastingManager(); |
|
49
|
|
|
} |
|
50
|
|
|
$this->castingManager = $castingManager; |
|
51
|
|
|
|
|
52
|
|
|
$this->relationBuilder = new RelationBuilder($this); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function register($name, $mapperOrConfigOrFactory): self |
|
56
|
|
|
{ |
|
57
|
|
|
if ($mapperOrConfigOrFactory instanceof MapperConfig || is_callable($mapperOrConfigOrFactory)) { |
|
58
|
|
|
$this->lazyMappers[$name] = $mapperOrConfigOrFactory; |
|
59
|
|
|
} elseif ($mapperOrConfigOrFactory instanceof Mapper) { |
|
60
|
|
|
$this->mappers[$name] = $mapperOrConfigOrFactory; |
|
61
|
|
|
$mapperOrConfigOrFactory->registerCasts($this->castingManager); |
|
62
|
|
|
} else { |
|
63
|
|
|
throw new InvalidArgumentException('$mapperOrConfigOrFactory must be a Mapper instance, |
|
64
|
|
|
a MapperConfig instance or a callable that returns a Mapper instance'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function has($name): bool |
|
71
|
|
|
{ |
|
72
|
|
|
return isset($this->mappers[$name]) || isset($this->lazyMappers[$name]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function get($name): Mapper |
|
76
|
|
|
{ |
|
77
|
|
|
if (isset($this->lazyMappers[$name])) { |
|
78
|
|
|
$this->mappers[$name] = $this->buildMapper($this->lazyMappers[$name]); |
|
79
|
|
|
$this->mappers[$name]->registerCasts($this->castingManager); |
|
80
|
|
|
unset($this->lazyMappers[$name]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (! isset($this->mappers[$name]) || ! $this->mappers[$name]) { |
|
84
|
|
|
throw new InvalidArgumentException(sprintf('Mapper named %s is not registered', $name)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->mappers[$name]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function save($mapperName, EntityInterface $entity, $withRelations = true) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->get($mapperName)->save($entity, $withRelations); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function delete($mapperName, EntityInterface $entity, $withRelations = true) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->get($mapperName)->delete($entity, $withRelations); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function find($mapperName, $id, array $load = []) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->get($mapperName)->find($id, $load); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function select($mapperName): Query |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->get($mapperName)->newQuery(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function createRelation(Mapper $nativeMapper, $name, $options): Relation |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->relationBuilder->newRelation($nativeMapper, $name, $options); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function buildMapper($mapperConfigOrFactory): Mapper |
|
116
|
|
|
{ |
|
117
|
|
|
if ($mapperConfigOrFactory instanceof MapperConfig) { |
|
118
|
|
|
return Mapper::make($this, $mapperConfigOrFactory); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$mapper = $mapperConfigOrFactory($this); |
|
122
|
|
|
if (! $mapper instanceof Mapper) { |
|
123
|
|
|
throw new InvalidArgumentException( |
|
124
|
|
|
'The mapper generated from the factory is not a valid `Mapper` instance' |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $mapper; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getConnectionLocator(): ConnectionLocator |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->connectionLocator; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return CastingManager |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getCastingManager(): CastingManager |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->castingManager; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|