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