Rev 35 | Go to most recent revision | Details | 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 | */ |
||
31 | public function __construct(array $data = null, array $mapping = null) |
||
32 | { |
||
33 | if (!is_null($data)) |
||
34 | { |
||
35 | $this->map($data, $mapping); |
||
36 | } |
||
37 | } |
||
38 | |||
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 | } |
||
54 | |||
55 | $method = 'get' . ucfirst($name); |
||
56 | |||
57 | if (method_exists($this, $method)) |
||
58 | { |
||
59 | return $this->$method(); |
||
60 | } |
||
61 | |||
62 | if (property_exists($this, "_$name")) |
||
63 | { |
||
64 | return $this->{"_$name"}; |
||
65 | } |
||
66 | |||
67 | return $this->$name; |
||
68 | } |
||
69 | |||
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); |
||
80 | |||
81 | if (method_exists($this, $method)) |
||
82 | { |
||
83 | return $this->$method($value); |
||
84 | } |
||
85 | |||
86 | if (property_exists($this, "_$name")) |
||
87 | { |
||
88 | $this->{"_$name"} = $value; |
||
89 | return $this->{"_$name"}; |
||
90 | } |
||
91 | |||
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 | } |
||
107 | |||
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 | } |
||
120 | |||
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 | } |
||
137 | |||
138 | /** |
||
139 | * Maps the values of an associative array to a model object |
||
140 | * |
||
141 | * @param array $data |
||
142 | * @param array $mapping = null |
||
143 | * <p>If <var>$mapping</var> is not provided, or <code>null</code> (default), |
||
144 | * the values of <var>$data</var> are mapped to properties of |
||
145 | * the model object as specified by the keys of <var>$data</var>.</p> |
||
146 | * <p>If <var>$mapping</var> is provided and an array, the keys of |
||
147 | * <var>$data</var> are mapped to properties as specified by |
||
148 | * the corresponding values of <var>$mapping</var>. If a value of |
||
149 | * <var>$mapping</var> is <code>null</code>, the corresponding value |
||
150 | * in <var>$data</var> is not mapped; if a key is missing in |
||
151 | * <var>$mapping</var>, the value is mapped as if <var>$mapping</var> |
||
152 | * was <code>null</code>.</p> |
||
153 | * @param bool $exclusive |
||
154 | * If <code>true</code>, <em>only</em> the keys of $data that are present |
||
155 | * in $mapping are mapped. |
||
156 | * @throws InvalidArgumentException if <var>$mapping</var> is neither |
||
157 | * <code>null</code> nor an array. |
||
158 | */ |
||
159 | public function map($data, $mapping = null, $exclusive = false) |
||
160 | { |
||
161 | if (is_null($mapping)) |
||
162 | { |
||
163 | foreach ($data as $key => $value) |
||
164 | { |
||
165 | $this->$key = $value; |
||
166 | } |
||
167 | } |
||
168 | else if (is_array($mapping)) |
||
169 | { |
||
170 | foreach ($data as $key => $value) |
||
171 | { |
||
172 | if (array_key_exists($key, $mapping)) |
||
173 | { |
||
174 | if ($exclusive || !is_null($mapping[$key])) |
||
175 | { |
||
176 | $this->{$mapping[$key]} = $value; |
||
177 | } |
||
178 | } |
||
179 | else |
||
180 | { |
||
181 | $this->$key = $value; |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | throw new InvalidArgumentException( |
||
188 | 'Expected null or array for $mapping, saw <pre>' |
||
189 | . print_r($mapping, true) . '</pre>'); |
||
190 | } |
||
191 | } |
||
192 | } |