/** * Materialize raw data into an entity. It {@uses self::_instantiate} to instantiate the entity. * * @param array $data The data to create an entity from * * @return AnDomainEntityAbstract */ protected function _createEntity(array $data) { //create an instance with its unique keys first $keys = array(); $description = $this->_description; $keys = $description->getIdentifyingValues($data); if (empty($keys)) { throw new AnDomainRepositoryException('Trying to create an entity witout any identiftying keys'); } //if an entity found with them same key already //instantiated then return that entity to avoid having //duplicate objects $entity = $this->find($keys, false); if ($entity) { return $entity; } $inheritance_column = $description->getInheritanceColumn(); if ($inheritance_column && isset($data[$inheritance_column->key()])) { $identifier = $data[$inheritance_column->key()]; $identifier = substr($identifier, strrpos($identifier, ',') + 1); } else { $identifier = $this->_prototype->getIdentifier(); } $entity = $this->_instantiateEntity($identifier, $data); //insert the identity $this->_space->insertEntity($entity, $keys); $context = new KCommandContext(); $context->data = $data; $context->keys = $keys; $entity->execute('after.fetch', $context); //call after fetch $entity->reset(); //reset the entity return $entity; }
/** * Serialize an entity * * @param AnDomainEntityAbstract $entity * @return array */ public function serialize($entity) { return array($this->getName() . '.' . $this->_target_parent_key => $entity->get($this->_target_parent_key)); }
/** * Called to validate an entity. By deafult it validates all the entity * properties. * * @param AnDomainEntityAbstract $entity The entity that is being validated * * @return bool */ public function validateEntity($entity) { $description = $entity->getEntityDescription(); //if entity is persisted only look at the modified //properties if ($entity->isModified()) { $properties = array_intersect_key($description->getProperty(), KConfig::unbox($entity->getModifiedData())); } else { $properties = $description->getProperty(); } foreach ($properties as $property) { $value = $entity->get($property->getName()); $entity->getValidator()->validateData($entity, $property, $value); } return $entity->getErrors()->count() === 0; }
/** * Renders the comments for a commentable. * * @param AnDomainEntityAbstract $entity The parent of the comments * @param array $config An array of configuration * * @return string */ public function comments($entity, $config = array()) { $config = array_merge(array('truncate_body' => array(), 'editor' => false, 'pagination' => true, 'show_guest_prompt' => true, 'content_filter_exclude' => array()), $config); $data = $this->getTemplate()->getData(); $limit = isset($data['limit']) ? $data['limit'] : 0; $offset = isset($data['start']) ? $data['start'] : 0; if (!isset($config['comments'])) { $config['comments'] = $entity->comments->order('creationTime', 'ASC')->limit($limit, $offset); } if (!isset($config['can_comment'])) { $config['can_comment'] = false; if ($entity && $entity->isAuthorizer()) { $config['can_comment'] = $entity->authorize('add.comment'); //if can't comment then check if it needs to follow if ($config['can_comment'] === false && $entity->__require_follow) { $config['require_follow'] = true; } } } if (!isset($config['strip_tags'])) { $config['strip_tags'] = false; } $config['entity'] = $entity; if ($config['pagination'] === $config['comments'] instanceof AnDomainEntitysetDefault) { $config['pagination'] = $this->pagination($config['comments'], array('paginate' => true, 'options' => array('scrollToTop' => true))); } return $this->_render('comments', $config); }
/** * Initializes the default configuration for the object * * Called from {@link __construct()} as a first step of object instantiation. * * @param KConfig $config An optional KConfig object with configuration options. * * @return void */ protected function _initialize(KConfig $config) { $config->append(array('attributes' => array('id' => array('key' => true), 'serviceName' => array('required' => true, 'column' => 'service'), 'value' => array('required' => true, 'unique' => true, 'column' => 'token'), 'used' => array('default' => '0')), 'relationships' => array('inviter' => array('parent' => 'com:people.domain.entity.person')))); parent::_initialize($config); }
/** * Return an array of serializable data of the entity in format of associative array. * * The default implementation return an array of scalar attributes * * @param AnDomainEntityAbstract $entity * * @return array */ public function toSerializableArray($entity) { $data = array_intersect_key($entity->getData(), $entity->getEntityDescription()->getAttributes()); return $data; }
/** * Materialize a proeprty value before accesing * * @param string $key * @return void */ protected function _materialize($key) { if (empty($this->_row)) { //no data has been set, the entity is property //in the new state lets set the default value accordinly return; } $property = $this->_description->getProperty($key); if (isset($this->_materialized[$property->getName()])) { return; } $repository = $this->_entity->getRepository(); //if a property is serialzable but not materizable //then the data must be missing if ($property->isSerializable() && !$property->isMaterializable($this->_row)) { //lazy load the value alogn with all the entities whose //$key value is missing $repository->getCommandChain()->disable(); $entities = $repository->getEntities(); $query = $repository->getQuery(); $keys = array(); foreach ($entities as $entity) { if ($entity->persisted()) { $data = $entity->getIdentifyingData(); $key = current(array_keys($data)); $value = current($data); $keys[$key][] = $value; } } foreach ($keys as $key => $values) { $query->where($key, 'IN', $values, 'OR'); } $result = $repository->fetch($query, AnDomain::FETCH_ROW_LIST); foreach ($result as $data) { $keys = $repository->getDescription()->getIdentifyingValues($data); $entity = $repository->find($keys, false); if ($entity) { $entity->setRowData($data); } } $repository->getCommandChain()->enable(); } $value = $property->materialize($this->_row, $this->_entity); $this->_setPropertyValue($property->getName(), $value); //when materilize a proxy property //materilize the same proeprty in all the entities //of the repository to allow for lazy loading if ($property->isRelationship()) { if ($property->isOneToOne() || $property->isManyToOne()) { //prevents calling the block multiple time //as it tries to instantiate the same property for //all the entities if (!self::_isLocked($repository)) { self::_lock($repository, true); $entities = $repository->getEntities(); foreach ($entities as $entity) { $entity->get($key); } self::_lock($repository, false); } } } }