|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine ORM |
|
5
|
|
|
* |
|
6
|
|
|
* Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern. |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine ORM |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @file Entity.php |
|
33
|
|
|
* |
|
34
|
|
|
* The Entity class |
|
35
|
|
|
* |
|
36
|
|
|
* @package Platine\Orm |
|
37
|
|
|
* @author Platine Developers Team |
|
38
|
|
|
* @copyright Copyright (c) 2020 |
|
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
40
|
|
|
* @link http://www.iacademy.cf |
|
41
|
|
|
* @version 1.0.0 |
|
42
|
|
|
* @filesource |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
declare(strict_types=1); |
|
46
|
|
|
|
|
47
|
|
|
namespace Platine\Orm; |
|
48
|
|
|
|
|
49
|
|
|
use JsonSerializable; |
|
50
|
|
|
use Platine\Orm\Exception\PropertyNotFoundException; |
|
51
|
|
|
use Platine\Orm\Mapper\DataMapper; |
|
52
|
|
|
use Platine\Orm\Mapper\DataMapperInterface; |
|
53
|
|
|
use Platine\Orm\Mapper\EntityMapper; |
|
54
|
|
|
use Platine\Orm\Mapper\EntityMapperInterface; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Class Entity |
|
58
|
|
|
* @package Platine\Orm |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract class Entity implements JsonSerializable |
|
61
|
|
|
{ |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The instance of data mapper |
|
65
|
|
|
* @var DataMapper|null |
|
66
|
|
|
*/ |
|
67
|
|
|
private ?DataMapper $dataMapper = null; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The data mapper constructor arguments |
|
71
|
|
|
* @var array<int, mixed> |
|
72
|
|
|
*/ |
|
73
|
|
|
private array $dataMapperArgs = []; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* @param EntityManager $manager |
|
78
|
|
|
* @param EntityMapper $mapper |
|
79
|
|
|
* @param array<string, mixed> $columns |
|
80
|
|
|
* @param array<string, \Platine\Orm\Relation\RelationLoader> $loaders |
|
81
|
|
|
* @param bool $isReadOnly |
|
82
|
|
|
* @param bool $isNew |
|
83
|
|
|
*/ |
|
84
|
|
|
final public function __construct( |
|
85
|
|
|
EntityManager $manager, |
|
86
|
|
|
EntityMapper $mapper, |
|
87
|
|
|
array $columns = [], |
|
88
|
|
|
array $loaders = [], |
|
89
|
|
|
bool $isReadOnly = false, |
|
90
|
|
|
bool $isNew = false |
|
91
|
|
|
) { |
|
92
|
|
|
$this->dataMapperArgs = [ |
|
93
|
|
|
$manager, |
|
94
|
|
|
$mapper, |
|
95
|
|
|
$columns, |
|
96
|
|
|
$loaders, |
|
97
|
|
|
$isReadOnly, |
|
98
|
|
|
$isNew |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Convert entity to JSON array |
|
104
|
|
|
* @return array<string, mixed> |
|
105
|
|
|
*/ |
|
106
|
|
|
public function jsonSerialize() |
|
107
|
|
|
{ |
|
108
|
|
|
$rawColumns = $this->mapper()->getRawColumns(); |
|
109
|
|
|
$data = []; |
|
110
|
|
|
foreach ($rawColumns as $name => $value) { |
|
111
|
|
|
if ($this->mapper()->hasRelation($name)) { |
|
112
|
|
|
$data[$name] = $this->mapper()->getRelated($name); |
|
113
|
|
|
} elseif ($this->mapper()->hasColumn($name)) { |
|
114
|
|
|
$data[$name] = $this->mapper()->getColumn($name); |
|
115
|
|
|
} else { |
|
116
|
|
|
$data[$name] = $value; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $data; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Shortcut to DataMapper getColumn and getRelated |
|
125
|
|
|
* @param string $name |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
public function __get(string $name) |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->mapper()->hasRelation($name)) { |
|
131
|
|
|
return $this->mapper()->getRelated($name); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($this->mapper()->hasColumn($name)) { |
|
135
|
|
|
return $this->mapper()->getColumn($name); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
throw new PropertyNotFoundException(sprintf( |
|
139
|
|
|
'Unknown column or relation [%s]', |
|
140
|
|
|
$name |
|
141
|
|
|
)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Shortcut to DataMapper setColumn and setRelated |
|
146
|
|
|
* @param string $name |
|
147
|
|
|
* @param mixed $value |
|
148
|
|
|
* @return void |
|
149
|
|
|
*/ |
|
150
|
|
|
public function __set(string $name, $value) |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->mapper()->hasRelation($name)) { |
|
153
|
|
|
if (is_array($value)) { |
|
154
|
|
|
foreach ($value as $entity) { |
|
155
|
|
|
$this->mapper()->link($name, $entity); |
|
156
|
|
|
} |
|
157
|
|
|
} else { |
|
158
|
|
|
$this->mapper()->setRelated($name, $value); |
|
159
|
|
|
} |
|
160
|
|
|
} else { |
|
161
|
|
|
$this->mapper()->setColumn($name, $value); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Shortcut to DataMapper hasColumn and hasRelated |
|
167
|
|
|
* @param string $name |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
|
|
public function __isset(string $name) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->mapper()->hasRelation($name) |
|
173
|
|
|
|| $this->mapper()->hasColumn($name); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Return the string representation of this entity |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public function __toString(): string |
|
181
|
|
|
{ |
|
182
|
|
|
$columns = $this->mapper()->getRawColumns(); |
|
183
|
|
|
$columnsStr = ''; |
|
184
|
|
|
foreach ($columns as $name => $value) { |
|
185
|
|
|
$columnsStr .= sprintf('%s=%s, ', $name, (string) $value); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return sprintf('[%s(%s)]', __CLASS__, rtrim($columnsStr, ', ')); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Map the entity information |
|
193
|
|
|
* @param EntityMapperInterface $mapper |
|
194
|
|
|
*/ |
|
195
|
|
|
abstract public static function mapEntity(EntityMapperInterface $mapper): void; |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Return the instance of data mapper |
|
199
|
|
|
* @return DataMapperInterface |
|
200
|
|
|
*/ |
|
201
|
|
|
final protected function mapper(): DataMapperInterface |
|
202
|
|
|
{ |
|
203
|
|
|
if ($this->dataMapper === null) { |
|
204
|
|
|
$this->dataMapper = new DataMapper(...$this->dataMapperArgs); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $this->dataMapper; |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|