|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/orm package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Orm; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
|
13
|
|
|
use Slick\Common\Exception\WriteOnlyException; |
|
14
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorRegistry; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Entity |
|
18
|
|
|
* |
|
19
|
|
|
* @package Slick\Orm |
|
20
|
|
|
* @readwrite |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class Entity extends Base implements EntityInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Saves current entity state |
|
27
|
|
|
* |
|
28
|
|
|
* Optionally saves only the partial data if $data argument is passed. If |
|
29
|
|
|
* no data is given al the field properties will be updated. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $data Partial data to save |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function save(array $data = []) |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->getMapper() |
|
38
|
2 |
|
->save($this, $data); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Deletes current entity from its storage |
|
44
|
|
|
* |
|
45
|
|
|
* @return self|$this|EntityInterface |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function delete() |
|
48
|
|
|
{ |
|
49
|
2 |
|
return $this->getMapper() |
|
50
|
2 |
|
->delete($this); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Retrieves the data mapper for this entity |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getMapper() |
|
57
|
|
|
{ |
|
58
|
2 |
|
return Orm::getMapper(get_class($this)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Retrieves the value of a property with the given name. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name The property name where to get the value from. |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed The property value. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws WriteOnlyException If the property being accessed |
|
69
|
|
|
* has the annotation @write |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getter($name) |
|
72
|
|
|
{ |
|
73
|
|
|
$value = parent::getter($name); |
|
74
|
|
|
$name = $this->getProperty($name); |
|
75
|
|
|
if (null == $value) { |
|
76
|
|
|
$relMap = EntityDescriptorRegistry::getInstance() |
|
77
|
|
|
->getDescriptorFor(get_class($this)) |
|
78
|
|
|
->getRelationsMap(); |
|
79
|
|
|
if ($relMap->containsKey($name)) { |
|
80
|
|
|
$value = $relMap->get($name)->load($this); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve the property name |
|
88
|
|
|
* |
|
89
|
|
|
* This method was designed to support old framework normalization |
|
90
|
|
|
* with the "_" underscore prefix character on property names. |
|
91
|
|
|
* The "_" should not be used in the PSR-2 standard |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* |
|
95
|
|
|
* @return string|false |
|
96
|
|
|
*/ |
|
97
|
|
|
private function getProperty($name) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$normalized = lcfirst($name); |
|
100
|
|
|
$old = "_{$normalized}"; |
|
101
|
|
|
$name = false; |
|
102
|
|
|
foreach ($this->getInspector()->getClassProperties() as $property) { |
|
103
|
|
|
if ($old == $property || $normalized == $property) { |
|
104
|
|
|
$name = $property; |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
return $name; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.