/** * * Object constructor * @param t41\ObjectModel\DataObject|t41\ObjectModel\ObjectUri|string $val * @param array $params */ public function __construct($val = null, array $params = null) { $this->_setParameterObjects(); if (is_array($params)) { $this->_setParameters($params); } /* build data object and populate it if possible */ if ($val instanceof DataObject) { if ($val->getClass() != get_class($this)) { throw new Exception("Provided Data Object is not build on class definition"); } $this->_dataObject = $val; } else { if (!is_null($val)) { if (!$val instanceof ObjectUri) { // provide backend if uri is partial $backendUri = null; //substr($val,0,1) != Backend::PREFIX ? ObjectModel::getObjectBackend(get_class($this)) : null; $val = new ObjectUri($val, $backendUri); $val->setClass(get_class($this)); } $this->_dataObject = Core::_($val, get_class($this), ObjectModel::DATA); } else { $this->_dataObject = DataObject::factory(get_class($this)); } } /* get object rules from config */ $this->setRules(); }
/** * Return a recursive array of all (or changed) properties and their respective value * * @param t41\Backend\Adapter\AbstractAdapter $backend * @param boolean $changed * @param boolean $display * @return array * @todo implement backend-related specifications (mapper...) */ public function toArray(Backend\Adapter\AbstractAdapter $backend = null, $changed = false, $display = false) { if (is_null($backend)) { $backend = ObjectModel::getObjectBackend($this->_class); } $result = array('data' => array(), 'collections' => array()); /* @var $value t41\ObjectModel\Property\AbstractProperty */ foreach ($this->_data as $key => $value) { // meta properties are ignored if ($value instanceof MetaProperty) { continue; } // should we consider only changed properties (except for collection which status is ambiguous) ? if (!$value instanceof CollectionProperty && $changed === true && $value->hasChanged() !== true) { continue; } if ($display === true && !$value instanceof Property\CurrencyProperty) { // if $display is set to TRUE, store property's display value $result['data'][$key] = $value->getDisplayValue(); continue; } if ($value instanceof Property\CollectionProperty) { if ($value->getParameter('embedded') == true) { $array = array(); /* @var $member t41\ObjectModel\BaseObject */ foreach ($value->getValue()->getMembers() as $member) { $array[] = $member->getDataObject()->toArray(); } $result['data'][$key] = $array; } else { // this property is not part of the saved data set $result['collections'][$key] = $value; continue; } } else { if ($value instanceof Property\ObjectProperty || $value instanceof Property\MediaProperty) { $value = $value->getValue(); $doBackend = $this->_uri instanceof ObjectUri ? $this->_uri->getBackendUri() : ObjectModel::getObjectBackend($this->_class); $doBackend = $doBackend->getAlias(); if ($value instanceof BaseObject) { if (!$value->getUri()) { // object has not been saved yet $value = $value->getDataObject()->toArray($backend, $changed, $display); } else { $value = $value->getUri(); /* check backends if they're identical, just keep identifier value*/ if ($value->getBackendUri()->getAlias() == $doBackend) { $value = $value->getIdentifier(); } else { $value = $value->__toString(); } } } else { if ($value instanceof self) { if (!$value->getUri()) { $value = $value->toArray(); } else { $value = $value->getUri(); /* check backends if they're identical, just keep identifier value*/ if ($value->getBackendUri()->getAlias() == $doBackend) { $value = $value->getIdentifier(); } else { $value = $value->__toString(); } } } else { if ($value instanceof ObjectUri) { /* check backends if they're identical, just keep identifier value*/ if ($value->getBackendUri()->getAlias() == $doBackend) { $value = $value->getIdentifier(); } else { $value = $value->__toString(); } } } } $result['data'][$key] = $value; } else { if ($value instanceof Property\ArrayProperty) { $result['data'][$key] = serialize($value->getValue()); } else { if ($value instanceof Property\AbstractProperty) { $value = $value->getValue(); $result['data'][$key] = $value instanceof ObjectModel ? $value->getUri() : $value; } else { $result['data'][$key] = $value; } } } } } return $result; }