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