/
Entity.php
184 lines (168 loc) · 4.26 KB
/
Entity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Kblom_Model_Entity
*
* This class is adapted with a certain modifications from a GREAT book about
* Zend Framework, Survive The Deep End, written by Pádraic Brady [1]. The idea
* of Entity models and mappers was proposed by Benjamin Eberlei, but then
* discontinued in favour of doctrine integration [2].
*
* "Entities are lightweight PHP Objects that don't need to extend any abstract
* base class or interface. An entity class must not be final or contain final
* methods. Additionally it must not implement clone nor wakeup or do so safely.
*
* An entity contains persistable properties. A persistable property is an
* instance variable of the entity that is saved into and retrieved from the
* database ..." [3] For example your user model, Application_Model_User,
* is Entity.
*
* [1] http://survivethedeepend.com/
* [2] http://framework.zend.com/wiki/pages/viewpage.action?pageId=9437243
* [3] http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/getting-started-xml-edition.html
*
* @category Kblom
* @package Kblom_Model
* @copyright Copyright (c) 2010-2011 Kim Blomqvist
* @license http://github.com/kblomqvist/kblom-zf1/raw/master/LICENSE The MIT License
*/
class Kblom_Model_Entity
{
/**
* Entity data/properties
*
* Example:
* array(
* 'id' => null,
* 'blogpost' => '',
* 'author' => ''
* );
*
* @var array
* */
protected $_data = array();
/**
* Reference ids of refered entity models
*
* Provides lazy loading for these models
*
* Example:
* array(
* 'author' => 1 // Author model is found by this id
* );
*
* @var array
*/
protected $_references = array();
public function __construct($data = null, $references = null)
{
if (!is_null($data)) {
$this->setProperties($data);
}
if (!is_null($references)) {
$this->setReferenceIds($references);
}
}
public function setProperties($data)
{
if (!is_array($data)) {
throw new Exception('Invalid data type. Has to be array, '
. gettype($data) . ' given.');
}
foreach ($data as $name => $value) {
$this->{$name} = $value;
}
return $this;
}
public function toArray()
{
return $this->_data;
}
/**
* @param string $name Reference property name
* @param int|string $id Reference entity model's id
* @retrun Kblom_Model_Entity
*/
public function setReferenceId($name, $id)
{
if (!array_key_exists($name, $this->_data)) {
throw new Exception(get_class($this)
. ": invalid model reference, does not have property '$name'");
}
if (!is_numeric($id)) {
$id = (string) $id;
}
$this->_references[(string) $name] = $id;
return $this;
}
public function setReferenceIds(array $references)
{
foreach ($references as $name => $id) {
$this->setReferenceId($name, $id);
}
return $this;
}
/**
* @param string $name Reference property name
* @return int|string Reference id or null if not exist
*/
public function getReferenceId($name)
{
if (array_key_exists($name, $this->_references)) {
return $this->_references[$name];
}
}
/**
* Sets property value
*
* You can override this magic setter by creating property specific
* setter method.
*
* @param string $name Entity property name
* @param mixed $value Entity property value
* @return null
* @throw Exception in case when invalid model property is set
*/
public function __set($name, $value)
{
if (!array_key_exists($name, $this->_data)) {
throw new Exception(get_class($this)
. ": '$name' is invalid model property");
}
$method = 'set' . $name;
if (method_exists($this, $method)) {
$this->$method($value);
} else {
$this->_data[$name] = $value;
}
}
/**
* Returns property value
*
* You can override this magic getter by creating property specific
* getter method.
*
* @param string $name Entity property name
* @return mixed Entity property
*/
public function __get($name)
{
if (!array_key_exists($name, $this->_data)) {
return null;
}
$method = 'get' . $name;
if (method_exists($this, $method)) {
return $this->$method();
}
return $this->_data[$name];
}
public function __isset($name)
{
return isset($this->_data[$name]);
}
public function __unset($name)
{
if (isset($this->_data[$name])) {
unset($this->_data[$name]);
}
}
}