1: <?php
2: /**
3: * PHP version 5
4: * @package generalDriver
5: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
6: * @author Stefan Heimes <stefan_heimes@hotmail.com>
7: * @author Tristan Lins <tristan.lins@bit3.de>
8: * @copyright The MetaModels team.
9: * @license LGPL.
10: * @filesource
11: */
12:
13: namespace DcGeneral\Data;
14:
15: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
16:
17: /**
18: * Class DefaultModel.
19: *
20: * Reference implementation of a dumb model.
21: *
22: * @package DcGeneral\Data
23: */
24: class DefaultModel extends AbstractModel
25: {
26: /**
27: * A list with all properties.
28: *
29: * @var array
30: */
31: protected $arrProperties = array();
32:
33: /**
34: * The Id of this model.
35: *
36: * @var mixed
37: */
38: protected $mixID = null;
39:
40: /**
41: * The name of the corresponding data provider.
42: *
43: * @var string
44: */
45: protected $strProviderName = null;
46:
47: /**
48: * Copy this model, without the id.
49: *
50: * @return void
51: */
52: public function __clone()
53: {
54: $this->mixID = null;
55: }
56:
57: /**
58: * Get the id for this model.
59: *
60: * @return string The ID for this model.
61: */
62: public function getID()
63: {
64: return $this->mixID;
65: }
66:
67: /**
68: * Fetch the property with the given name from the model.
69: *
70: * This method returns null if an unknown property is retrieved.
71: *
72: * @param string $strPropertyName The property name to be retrieved.
73: *
74: * @return mixed The value of the given property.
75: */
76: public function getProperty($strPropertyName)
77: {
78: if ($strPropertyName == 'id')
79: {
80: return $this->getID();
81: }
82:
83: if (array_key_exists($strPropertyName, $this->arrProperties))
84: {
85: return $this->arrProperties[$strPropertyName];
86: }
87:
88: return null;
89: }
90:
91: /**
92: * Fetch all properties from the model as an name => value array.
93: *
94: * @return array
95: */
96: public function getPropertiesAsArray()
97: {
98: $arrArray = $this->arrProperties;
99: $arrArray['id'] = $this->mixID;
100:
101: return $arrArray;
102: }
103:
104: /**
105: * Set the id for this object.
106: *
107: * NOTE: when the Id has been set once to a non null value, it can NOT be changed anymore.
108: *
109: * Normally this should only be called from inside of the implementing provider.
110: *
111: * @param mixed $mixID Could be a integer, string or anything else - depends on the provider implementation.
112: *
113: * @return void
114: */
115: public function setID($mixID)
116: {
117: if ($this->mixID == null)
118: {
119: $this->mixID = $mixID;
120: }
121: }
122:
123: /**
124: * Update the property value in the model.
125: *
126: * @param string $strPropertyName The property name to be set.
127: *
128: * @param mixed $varValue The value to be set.
129: *
130: * @return void
131: */
132: public function setProperty($strPropertyName, $varValue)
133: {
134: if ($varValue !== $this->getProperty($strPropertyName))
135: {
136: $this->setMeta(DCGE::MODEL_IS_CHANGED, true);
137: $this->arrProperties[$strPropertyName] = $varValue;
138: }
139: }
140:
141: /**
142: * Update all properties in the model.
143: *
144: * @param array $arrProperties The property values as name => value pairs.
145: *
146: * @return void
147: */
148: public function setPropertiesAsArray($arrProperties)
149: {
150: if (is_array($arrProperties))
151: {
152: if (array_key_exists('id', $arrProperties))
153: {
154: unset($arrProperties['id']);
155: }
156:
157: foreach ($arrProperties as $strPropertyName => $varValue)
158: {
159: $this->setProperty($strPropertyName, $varValue);
160: }
161: }
162: }
163:
164: /**
165: * Check if this model have any properties.
166: *
167: * @return boolean true if any property has been stored, false otherwise.
168: */
169: public function hasProperties()
170: {
171: if (count($this->arrProperties) != 0)
172: {
173: return true;
174: }
175:
176: return false;
177: }
178:
179: /**
180: * Get an iterator for this model's property values.
181: *
182: * @return \IteratorAggregate
183: */
184: public function getIterator()
185: {
186: return new \ArrayIterator($this->arrProperties);
187: }
188:
189: /**
190: * Sets the provider name in the model.
191: *
192: * NOTE: this is intended to be used by the data provider only and not by any user.
193: * Changing this by hand may cause unexpected behaviour. So DO NOT USE IT.
194: * For this reason, this method is not interfaced, as only the data provider knows how
195: * to set itself to the model.
196: *
197: * @param string $strProviderName The name of the corresponding data provider.
198: *
199: * @return void
200: */
201: public function setProviderName($strProviderName)
202: {
203: $this->strProviderName = $strProviderName;
204: }
205:
206: /**
207: * Return the data provider name.
208: *
209: * @return string the name of the corresponding data provider.
210: */
211: public function getProviderName()
212: {
213: return $this->strProviderName;
214: }
215:
216: /**
217: * {@inheritDoc}
218: *
219: * @throws DcGeneralInvalidArgumentException When a property in the value bag has been marked as invalid.
220: */
221: public function readFromPropertyValueBag(PropertyValueBagInterface $valueBag)
222: {
223: foreach ($this as $name => $value)
224: {
225: if (!$valueBag->hasPropertyValue($name))
226: {
227: continue;
228: }
229:
230: if ($valueBag->isPropertyValueInvalid($name))
231: {
232: throw new DcGeneralInvalidArgumentException('The value for property ' . $name . ' is invalid.');
233: }
234:
235: $this->setProperty($name, $valueBag->getPropertyValue($value));
236: }
237:
238: return $this;
239: }
240:
241: /**
242: * {@inheritDoc}
243: */
244: public function writeToPropertyValueBag(PropertyValueBagInterface $valueBag)
245: {
246: foreach ($this as $name => $value)
247: {
248: if (!$valueBag->hasPropertyValue($name))
249: {
250: continue;
251: }
252:
253: $valueBag->setPropertyValue($name, $this->getProperty($value));
254: }
255:
256: return $this;
257: }
258: }
259: