Rev 59 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
51 | PointedEar | 3 | namespace PointedEars\PHPX; |
4 | |||
27 | PointedEar | 5 | /** |
6 | * Interface to be implemented if the model should be localizable |
||
7 | */ |
||
8 | interface ILocalizable |
||
9 | { |
||
10 | /** |
||
52 | PointedEar | 11 | * Localizes this model. The actual implementation is left to |
12 | * the model class implementing this interface. |
||
27 | PointedEar | 13 | */ |
52 | PointedEar | 14 | function localize (); |
27 | PointedEar | 15 | } |
16 | |||
17 | /** |
||
18 | * Abstract model class |
||
19 | * |
||
59 | PointedEar | 20 | * Provides a constructor to initialize properties using setters and getters. |
27 | PointedEar | 21 | * |
22 | * @author Thomas Lahn |
||
23 | */ |
||
59 | PointedEar | 24 | abstract class AbstractModel extends Base |
27 | PointedEar | 25 | { |
26 | /** |
||
27 | * Creates a new model object |
||
28 | * |
||
63 | PointedEar | 29 | * @param array $data |
30 | * Initialization data (optional) |
||
31 | * @param array $mapping |
||
32 | * Mapping for initialization data (optional) |
||
33 | * @param bool $exclusiveMapping |
||
34 | * Exclusive mapping (optional; default: inclusive) |
||
35 | * @see AbstractModel::map() |
||
27 | PointedEar | 36 | */ |
63 | PointedEar | 37 | protected function __construct ( |
38 | array $data = null, array $mapping = null, $exclusiveMapping = false) |
||
27 | PointedEar | 39 | { |
40 | if (!is_null($data)) |
||
41 | { |
||
63 | PointedEar | 42 | $this->map($data, $mapping, $exclusiveMapping); |
27 | PointedEar | 43 | } |
44 | } |
||
49 | PointedEar | 45 | |
27 | PointedEar | 46 | /** |
47 | * Returns <code>true</code> if a variable name is a property variable name |
||
48 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
49 | * |
||
50 | * @param string $varName |
||
51 | * @return boolean |
||
52 | * @see getPropertyVars() |
||
53 | */ |
||
52 | PointedEar | 54 | private static function _isPropertyVar ($varName) |
27 | PointedEar | 55 | { |
56 | return preg_match('/^_\\w/', $varName) > 0; |
||
57 | } |
||
49 | PointedEar | 58 | |
27 | PointedEar | 59 | /** |
60 | * Returns <code>true</code> if a variable name is a property variable name |
||
61 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
62 | * |
||
63 | * @param string $varName |
||
64 | * @return string |
||
65 | * @see getPropertyVars() |
||
66 | */ |
||
52 | PointedEar | 67 | private static function _toPropertyVar ($varName) |
27 | PointedEar | 68 | { |
69 | return preg_replace('/^_(\\w)/', '\\1', $varName); |
||
70 | } |
||
49 | PointedEar | 71 | |
27 | PointedEar | 72 | /** |
73 | * Returns the public names of the property variables of a {@link Model} |
||
74 | * as an array of strings |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
52 | PointedEar | 78 | public function getPropertyVars () |
27 | PointedEar | 79 | { |
80 | return array_map( |
||
81 | array('self', '_toPropertyVar'), |
||
82 | array_filter( |
||
83 | array_keys(get_object_vars($this)), |
||
84 | array('self', '_isPropertyVar') |
||
85 | ) |
||
86 | ); |
||
87 | } |
||
49 | PointedEar | 88 | |
27 | PointedEar | 89 | /** |
90 | * Maps the values of an associative array to a model object |
||
91 | * |
||
92 | * @param array $data |
||
35 | PointedEar | 93 | * Data to be mapped to properties of the object |
27 | PointedEar | 94 | * @param array $mapping = null |
95 | * <p>If <var>$mapping</var> is not provided, or <code>null</code> (default), |
||
96 | * the values of <var>$data</var> are mapped to properties of |
||
97 | * the model object as specified by the keys of <var>$data</var>.</p> |
||
98 | * <p>If <var>$mapping</var> is provided and an array, the keys of |
||
99 | * <var>$data</var> are mapped to properties as specified by |
||
100 | * the corresponding values of <var>$mapping</var>. If a value of |
||
101 | * <var>$mapping</var> is <code>null</code>, the corresponding value |
||
102 | * in <var>$data</var> is not mapped; if a key is missing in |
||
103 | * <var>$mapping</var>, the value is mapped as if <var>$mapping</var> |
||
104 | * was <code>null</code>.</p> |
||
35 | PointedEar | 105 | * @param bool $exclusive = false |
63 | PointedEar | 106 | * <p>If <code>true</code>, <em>only</em> the keys of <var>$data</var> |
107 | * that are present in <var>$mapping</var> are mapped.</p> |
||
49 | PointedEar | 108 | * @return AbstractModel |
109 | * The modified object |
||
27 | PointedEar | 110 | */ |
52 | PointedEar | 111 | public function map (array $data, array $mapping = null, $exclusive = false) |
27 | PointedEar | 112 | { |
113 | if (is_null($mapping)) |
||
114 | { |
||
115 | foreach ($data as $key => $value) |
||
116 | { |
||
117 | $this->$key = $value; |
||
118 | } |
||
119 | } |
||
35 | PointedEar | 120 | else |
27 | PointedEar | 121 | { |
122 | foreach ($data as $key => $value) |
||
123 | { |
||
124 | if (array_key_exists($key, $mapping)) |
||
125 | { |
||
126 | if ($exclusive || !is_null($mapping[$key])) |
||
127 | { |
||
128 | $this->{$mapping[$key]} = $value; |
||
129 | } |
||
130 | } |
||
131 | else |
||
132 | { |
||
133 | $this->$key = $value; |
||
134 | } |
||
135 | } |
||
136 | } |
||
49 | PointedEar | 137 | |
138 | return $this; |
||
27 | PointedEar | 139 | } |
140 | } |