Rev 35 | Rev 51 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 35 | Rev 49 | ||
|---|---|---|---|
| Line 26... | Line 26... | ||
| 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
|
| Line 49... | Line 49... | ||
| 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
|
| Line 102... | Line 102... | ||
| 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
|
| Line 115... | Line 115... | ||
| 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
|
| Line 132... | Line 132... | ||
| 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
|
| Line 152... | Line 152... | ||
| 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 | {
|
| Line 179... | Line 181... | ||
| 179 | {
|
181 | {
|
| 180 | $this->$key = $value; |
182 | $this->$key = $value; |
| 181 | }
|
183 | }
|
| 182 | }
|
184 | }
|
| 183 | }
|
185 | }
|
| - | 186 | ||
| - | 187 | return $this; |
|
| 184 | }
|
188 | }
|
| 185 | }
|
189 | }
|
| 186 | 190 | ||