Rev 35 | Rev 51 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
3 | /** |
||
4 | * Interface to be implemented if the model should be localizable |
||
5 | */ |
||
6 | interface ILocalizable |
||
7 | { |
||
8 | /** |
||
9 | * Localizes this model. The actual implementation is left to the model class |
||
10 | * implementing this interface. |
||
11 | */ |
||
12 | function localize(); |
||
13 | } |
||
14 | |||
15 | /** |
||
16 | * Abstract model class |
||
17 | * |
||
18 | * Provides basic setters and getters for protected/private properties |
||
19 | * and a constructor to initialize properties using setters and getters. |
||
20 | * |
||
21 | * @author Thomas Lahn |
||
22 | */ |
||
23 | abstract class AbstractModel |
||
24 | { |
||
25 | /** |
||
26 | * Creates a new model object |
||
27 | * |
||
28 | * @param array $data Initialization data (optional) |
||
29 | * @param array $mapping Mapping for initialization data (optional) |
||
30 | */ |
||
49 | PointedEar | 31 | protected function __construct(array $data = null, array $mapping = null) |
27 | PointedEar | 32 | { |
33 | if (!is_null($data)) |
||
34 | { |
||
35 | $this->map($data, $mapping); |
||
36 | } |
||
37 | } |
||
49 | PointedEar | 38 | |
27 | PointedEar | 39 | /** |
40 | * Getter for properties |
||
41 | * |
||
42 | * @param string $name |
||
43 | * @throws ModelPropertyException |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function __get($name) |
||
47 | { |
||
48 | /* Support for Object-Relational Mappers */ |
||
49 | if (strpos($name, 'persistent') === 0) |
||
50 | { |
||
51 | $class = get_class($this); |
||
52 | return $class::${$name}; |
||
53 | } |
||
49 | PointedEar | 54 | |
27 | PointedEar | 55 | $method = 'get' . ucfirst($name); |
49 | PointedEar | 56 | |
27 | PointedEar | 57 | if (method_exists($this, $method)) |
58 | { |
||
59 | return $this->$method(); |
||
60 | } |
||
49 | PointedEar | 61 | |
27 | PointedEar | 62 | if (property_exists($this, "_$name")) |
63 | { |
||
64 | return $this->{"_$name"}; |
||
65 | } |
||
49 | PointedEar | 66 | |
27 | PointedEar | 67 | return $this->$name; |
68 | } |
||
49 | PointedEar | 69 | |
27 | PointedEar | 70 | /** |
71 | * Setter for properties |
||
72 | * |
||
73 | * @param string $name |
||
74 | * @param mixed $value The new property value before assignment |
||
75 | * @throws ModelPropertyException |
||
76 | */ |
||
77 | public function __set($name, $value) |
||
78 | { |
||
79 | $method = 'set' . ucfirst($name); |
||
49 | PointedEar | 80 | |
27 | PointedEar | 81 | if (method_exists($this, $method)) |
82 | { |
||
83 | return $this->$method($value); |
||
84 | } |
||
49 | PointedEar | 85 | |
27 | PointedEar | 86 | if (property_exists($this, "_$name")) |
87 | { |
||
88 | $this->{"_$name"} = $value; |
||
89 | return $this->{"_$name"}; |
||
90 | } |
||
49 | PointedEar | 91 | |
27 | PointedEar | 92 | /* NOTE: Attempts to set other properties are _silently_ _ignored_ */ |
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns <code>true</code> if a variable name is a property variable name |
||
97 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
98 | * |
||
99 | * @param string $varName |
||
100 | * @return boolean |
||
101 | * @see getPropertyVars() |
||
102 | */ |
||
103 | private static function _isPropertyVar($varName) |
||
104 | { |
||
105 | return preg_match('/^_\\w/', $varName) > 0; |
||
106 | } |
||
49 | PointedEar | 107 | |
27 | PointedEar | 108 | /** |
109 | * Returns <code>true</code> if a variable name is a property variable name |
||
110 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
111 | * |
||
112 | * @param string $varName |
||
113 | * @return string |
||
114 | * @see getPropertyVars() |
||
115 | */ |
||
116 | private static function _toPropertyVar($varName) |
||
117 | { |
||
118 | return preg_replace('/^_(\\w)/', '\\1', $varName); |
||
119 | } |
||
49 | PointedEar | 120 | |
27 | PointedEar | 121 | /** |
122 | * Returns the public names of the property variables of a {@link Model} |
||
123 | * as an array of strings |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | public function getPropertyVars() |
||
128 | { |
||
129 | return array_map( |
||
130 | array('self', '_toPropertyVar'), |
||
131 | array_filter( |
||
132 | array_keys(get_object_vars($this)), |
||
133 | array('self', '_isPropertyVar') |
||
134 | ) |
||
135 | ); |
||
136 | } |
||
49 | PointedEar | 137 | |
27 | PointedEar | 138 | /** |
139 | * Maps the values of an associative array to a model object |
||
140 | * |
||
141 | * @param array $data |
||
35 | PointedEar | 142 | * Data to be mapped to properties of the object |
27 | PointedEar | 143 | * @param array $mapping = null |
144 | * <p>If <var>$mapping</var> is not provided, or <code>null</code> (default), |
||
145 | * the values of <var>$data</var> are mapped to properties of |
||
146 | * the model object as specified by the keys of <var>$data</var>.</p> |
||
147 | * <p>If <var>$mapping</var> is provided and an array, the keys of |
||
148 | * <var>$data</var> are mapped to properties as specified by |
||
149 | * the corresponding values of <var>$mapping</var>. If a value of |
||
150 | * <var>$mapping</var> is <code>null</code>, the corresponding value |
||
151 | * in <var>$data</var> is not mapped; if a key is missing in |
||
152 | * <var>$mapping</var>, the value is mapped as if <var>$mapping</var> |
||
153 | * was <code>null</code>.</p> |
||
35 | PointedEar | 154 | * @param bool $exclusive = false |
155 | * <p>If <code>true</code>, <em>only</em> the keys of $data that are present |
||
156 | * in $mapping are mapped.</p> |
||
49 | PointedEar | 157 | * @return AbstractModel |
158 | * The modified object |
||
27 | PointedEar | 159 | */ |
35 | PointedEar | 160 | public function map(array $data, array $mapping = null, $exclusive = false) |
27 | PointedEar | 161 | { |
162 | if (is_null($mapping)) |
||
163 | { |
||
164 | foreach ($data as $key => $value) |
||
165 | { |
||
166 | $this->$key = $value; |
||
167 | } |
||
168 | } |
||
35 | PointedEar | 169 | else |
27 | PointedEar | 170 | { |
171 | foreach ($data as $key => $value) |
||
172 | { |
||
173 | if (array_key_exists($key, $mapping)) |
||
174 | { |
||
175 | if ($exclusive || !is_null($mapping[$key])) |
||
176 | { |
||
177 | $this->{$mapping[$key]} = $value; |
||
178 | } |
||
179 | } |
||
180 | else |
||
181 | { |
||
182 | $this->$key = $value; |
||
183 | } |
||
184 | } |
||
185 | } |
||
49 | PointedEar | 186 | |
187 | return $this; |
||
27 | PointedEar | 188 | } |
189 | } |