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