Abstract.php
00001 <?php
00012 abstract class Dsao_DataHandler_Abstract
00013 {
00017 protected $_data = array();
00018
00022 protected $_propertiesMandatory = array();
00023
00028 protected $_propertiesProtected = array();
00029
00033 protected $_readOnly = false;
00034
00038 protected $_valid = null;
00039
00048 public function __call($method, $parameters)
00049 {
00050
00051 $methodUnderscored = Dsao_Model_Abstract::underscore($method);
00052 $parts = explode('_', $methodUnderscored, 2);
00053
00054
00055 $operation = (isset($parts[0]) ? $parts[0] : null);
00056
00057 $property = (isset($parts[1]) ? $parts[1] : null);
00058
00059 switch ($operation)
00060 {
00061 case 'get':
00062 case 'unset':
00063 case 'isset':
00064 {
00065 $methodName = '__'.$operation;
00066
00067 return $this->$methodName($property);
00068 }
00069 break;
00070
00071 case 'set':
00072 {
00073
00074 if (!isset($parameters[0]))
00075 {
00076 $parameters[0] = null;
00077 }
00078
00079 $this->__set($property, $parameters[0]);
00080
00081
00082 return $this;
00083 }
00084 break;
00085
00086 default:
00087 {
00088 throw new Dsao_Exception(array(
00089 'message' =>'dsao_datahandler_abstract_method_not_found',
00090 'variables' => array('method' => $method)));
00091 }
00092 }
00093 }
00094
00101 public function __construct(array $data = array(), $readOnly = false)
00102 {
00103
00104 $this->_propertiesProtected = array_flip($this->_propertiesProtected);
00105
00106 $this->setData($data);
00107
00108 $this->setReadOnly($readOnly);
00109 }
00110
00116 public function __get($property)
00117 {
00118 $property = (string) $property;
00119
00120 return isset($this->_data[$property]) ?
00121 $this->_data[$property] : null;
00122 }
00123
00130 public function __isset($property)
00131 {
00132 return isset($this->_data[(string) $property]);
00133 }
00134
00142 public function __set($property, $value)
00143 {
00144 if (!$property)
00145 {
00146 return;
00147 }
00148
00149
00150 $this->_resetValidity(true);
00151
00152
00153 if (null === $value)
00154 {
00155 $this->__unset($property);
00156 }
00157 else
00158 {
00159 $this->_data[(string) $property] = $value;
00160 }
00161 }
00162
00171 public function __sleep()
00172 {
00173
00174 return array('_readOnly', '_data', '_valid');
00175 }
00176
00183 public function __unset($property)
00184 {
00185 $this->_resetValidity();
00186
00187 $property = (string) $property;
00188
00189
00190 if ($this->__isset($property))
00191 {
00192 unset($this->_data[$property]);
00193 }
00194 }
00195
00205 public function __wakeup()
00206 {
00207 $this->_propertiesProtected = array_flip($this->_propertiesProtected);
00208 }
00209
00215 protected function _checkValidity()
00216 {
00217
00218 foreach ($this->_propertiesMandatory as $property)
00219 {
00220
00221 if (!$this->__isset($property))
00222 {
00223 $this->_valid = false;
00224
00225 return false;
00226 }
00227 }
00228
00229 $this->_valid = true;
00230
00231 return true;
00232 }
00233
00241 protected function _cleanData($data)
00242 {
00243 foreach ($data as $property => $value)
00244 {
00245
00246
00247 if (isset($this->_propertiesProtected[$property])
00248 && $this->__isset($property))
00249 {
00250 unset($data[$property]);
00251 }
00252 }
00253
00254 return $data;
00255 }
00256
00262 protected function _resetValidity()
00263 {
00264 $this->isReadOnly(true);
00265
00266 $this->_valid = null;
00267 }
00268
00274 public function clearData()
00275 {
00276
00277 $this->_resetValidity();
00278
00279
00280 $this->_data = array();
00281
00282 return $this;
00283 }
00284
00303 public function getData($properties = null, $default = null)
00304 {
00305
00306 if (!$properties)
00307 {
00308 return $this->_data;
00309 }
00310
00311
00312 if (!is_array($properties))
00313 {
00314
00315 if (!$this->__isset($properties))
00316 {
00317 return $default;
00318 }
00319
00320 return $this->_data[$properties];
00321 }
00322
00323
00324 $results = array();
00325
00326
00327 foreach ($properties as $property => $defaultValue)
00328 {
00329
00330 if (is_numeric($property))
00331 {
00332 $property = $defaultValue;
00333 $defaultValue = $default;
00334 }
00335
00336
00337 if (!$this->__isset($property))
00338 {
00339 $results[$property] = $defaultValue;
00340 continue;
00341 }
00342
00343
00344 $results[$property] = $this->_data[$property];
00345 }
00346
00347 return $results;
00348 }
00349
00365 public function hasData(array $properties = array(), $or = false, array $skip = array())
00366 {
00367
00368
00369 if (empty($properties))
00370 {
00371 $propertiesCount = count($this->_data);
00372
00373
00374 foreach ($skip as $property)
00375 {
00376
00377 if ($this->__isset($property))
00378 {
00379 $parametersCount--;
00380 }
00381 }
00382
00383 return (bool) $propertiesCount;
00384 }
00385
00386
00387 foreach ($properties as $property)
00388 {
00389
00390 if ($this->__isset($property))
00391 {
00392
00393
00394 if ($or)
00395 {
00396 return true;
00397 }
00398 }
00399
00400
00401 else if (!$or)
00402 {
00403 return false;
00404 }
00405 }
00406
00407
00408 return true;
00409 }
00410
00418 public function isReadOnly($throw = false)
00419 {
00420
00421 if ($this->_readOnly && $throw)
00422 {
00423 throw new Dsao_Exception('dsao_datahandler_abstract_readonly');
00424 }
00425
00426 return (bool) $this->_readOnly;
00427 }
00428
00435 public function isValid($throw = false)
00436 {
00437
00438
00439 if (null === $this->_valid)
00440 {
00441 $this->_checkValidity();
00442 }
00443
00444
00445 if (!$this->_valid && $throw)
00446 {
00447 throw new Dsao_Exception('dsao_datahandler_abstract_invalid');
00448 }
00449
00450 return $this->_valid;
00451 }
00452
00461 public function setData(array $data = array(), $overwrite = true)
00462 {
00463
00464 if (empty($data))
00465 {
00466 return $this;
00467 }
00468
00469
00470 $this->_resetValidity();
00471
00472
00473 $data = $this->_cleanData($data);
00474
00475
00476 foreach ($data as $property => $value)
00477 {
00478
00479 if (!$overwrite && $this->__isset($property))
00480 {
00481 continue;
00482 }
00483
00484
00485 if (null === $value)
00486 {
00487 continue;
00488 }
00489
00490 else
00491 {
00492 $this->_data[$property] = $value;
00493 }
00494 }
00495
00496 return $this;
00497 }
00498
00505 public function setFromArray(array $data = array())
00506 {
00507
00508 $this->clearData();
00509
00510
00511 $this->_data = $this->_cleanData($data);
00512
00513 return $this;
00514 }
00515
00522 public function setReadOnly($readOnly = true)
00523 {
00524 $this->_readOnly = (bool) $readOnly;
00525
00526 return $this;
00527 }
00528
00539 public function setValid($valid = true)
00540 {
00541
00542 $validityChecked = $this->_checkValidity();
00543
00544 if (!$validityChecked && $valid)
00545 {
00546 throw new Dsao_Exception('dsao_datahandler_abstract_set_valid');
00547 }
00548
00549 $this->_valid = (bool) $valid;
00550
00551 return $this;
00552 }
00553
00559 public function toArray()
00560 {
00561 return $this->_data;
00562 }
00563 }