/** * * 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(); }
/** * Populate the given collection from the array of identifiers and the uri base * * @param array $ids * @param \t41\ObjectModel\Collection $collection * @param \t41\ObjectModel\ObjectUri $uriBase */ protected function _populateCollection(array $ids, ObjectModel\Collection $collection, ObjectModel\ObjectUri $uriBase) { if (count($ids) == 0) { return array(); } if (count($ids[0]) > 1) { $do = clone $collection->getDataObject(); } $class = $collection->getDataObject()->getClass(); // populate array with relevant objects type $array = array(); /** * If data object has been modified (meta property added) we need to use it to populate objects */ if ($collection->getParameter('memberType') != ObjectModel::URI && $collection->getDataObject()->getDna('custom')) { $do = clone $collection->getDataObject(); } foreach ($ids as $key => $id) { $uri = clone $uriBase; if (is_array($id)) { $uri->setUrl($uri->getUrl() . $id['id'])->setIdentifier($id['id']); unset($id['id']); } else { $uri->setUrl($uri->getUrl() . $id)->setIdentifier($id); } if (isset($do)) { $obj = clone $do; $obj->setUri($uri); } else { unset($obj); } switch ($collection->getParameter('memberType')) { case ObjectModel::URI: $obj = $uri; break; case ObjectModel::MODEL: if (isset($obj) || is_array($ids)) { Backend::read($obj, null, $id); } else { $obj = Core::_($uri, $class); } break; case ObjectModel::DATA: default: if (isset($obj) || is_array($ids)) { Backend::read($obj, null, $id); } else { $obj = Core::_($uri, $class); } $obj = $obj->getDataObject(); break; } $array[$key] = $obj; } return $array; }
/** * Convert the given member to the given type * @param object $member * @param string $toType * @return \t41\ObjectModel\DataObject|\t41\ObjectModel\ObjectUri>|\t41\ObjectModel\BaseObject */ protected function _castMember($member, $toType) { // Don't cast if it's already done if ($toType == ObjectModel::DATA && $member instanceof DataObject || $toType == ObjectModel::MODEL && $member instanceof BaseObject || $toType == ObjectModel::URI && $member instanceof ObjectUri) { return $member; } $class = $this->getClass(); switch ($toType) { case ObjectModel::DATA: if ($member instanceof BaseObject) { $member = $member->getDataObject(); } else { $member = Core::_($member)->getDataObject(); } break; case ObjectModel::MODEL: if ($member instanceof DataObject) { // careful with custom data objects ! $member = $member->getDna('custom') ? new $class($member) : Core::_($member->getUri()); } else { $member = Core::_($member); } break; case ObjectModel::URI: $member = $member->getUri(); break; } return $member; }
/** * Return the current value in the $param form * @see t41\ObjectModel\Property.AbstractProperty::getValue() * @param string $param define which format to use */ public function getValue($param = null) { if (is_null($this->_value)) { return null; } /* if param is null, return the value in its current format */ if (is_null($param)) { return $this->_value; } switch ($param) { case ObjectModel::MODEL: if ($this->_value instanceof ObjectModel\DataObject) { $this->_value = Core::_($this->_value->getUri()); return $this->_value; } else { if ($this->_value instanceof ObjectUri) { /* object uri */ $this->_value = Core::_($this->_value); } } return $this->_value; break; case ObjectModel::DATA: if ($this->_value instanceof ObjectUri) { $this->_value = Core::_($this->_value); return $this->_value->getDataObject(); } else { if ($this->_value instanceof DataObject) { return $this->_value; } else { return $this->_value->getDataObject(); } } break; case ObjectModel::URI: default: if ($this->_value instanceof ObjectUri) { return $this->_value; } else { if ($this->_value instanceof DataObject) { return $this->_value->getUri(); } else { return $this->_value->getDataObject()->getUri(); } } break; } }