Ejemplo n.º 1
0
 /**
  * @param IEntity
  * @param scalar|NULL id
  * @param string update|insert
  * @return array
  */
 protected function toArray(IEntity $entity, $id, $operation)
 {
     $values = $entity->toArray();
     if ($id !== NULL) {
         $values['id'] = $id;
     }
     $params = array('id' => isset($values['id'])) + (array) $this->params;
     $params += array_fill_keys(array_keys($values), true);
     if ($this->whichParams !== NULL) {
         $params = array('id' => $params['id']) + array_fill_keys($this->whichParams, true);
     }
     if ($this->whichParamsNot !== NULL) {
         $tmp = array_fill_keys($this->whichParamsNot, false);
         unset($tmp['id']);
         $params = $tmp + $params;
     }
     $arguments = array('params' => $params, 'values' => $values, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_BEFORE, $entity, $arguments);
     $params = $arguments['params'];
     $values = $arguments['values'];
     $result = array();
     foreach ($params as $key => $do) {
         if (array_key_exists($key, $values)) {
             $value = $values[$key];
         } else {
             // pokusi se precist, muze existovat getter, jinak vyhodi exception
             $value = $entity->{$key};
         }
         if ($do === false or $operation === 'update' and $entity->hasParam($key) and !$entity->isChanged($key)) {
             continue;
         }
         if ($do !== true) {
             $value = Callback::create($do)->invoke($value, $entity);
         }
         $result[$key] = $this->scalarizeValue($value, $key, $entity);
         if ($value instanceof IRelationship and $result[$key] === NULL) {
             unset($result[$key]);
         }
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_AFTER, $entity, $arguments);
     $result = $arguments['values'];
     $result = $this->conventional->formatEntityToStorage($result);
     $primaryKey = $this->conventional->getPrimaryKey();
     if ($primaryKey !== $this->primaryKey and array_key_exists($primaryKey, $result)) {
         $id = $result[$primaryKey];
         unset($result[$primaryKey]);
         $result = array($this->primaryKey => $id) + $result;
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_CONVENTIONAL, $entity, $arguments);
     $result = $arguments['values'];
     return $result;
 }