Exemplo n.º 1
0
 /**
  * @param array id => id {@see ManyToMany::$injectedValue}
  * @return array id => id will be set as {@see ManyToMany::$injectedValue}
  */
 public function validateInjectedValue($injectedValue)
 {
     if ($this->meta->getWhereIsMapped() !== RelationshipMetaDataToMany::MAPPED_THERE) {
         if (ValidationHelper::isValid(array('array'), $injectedValue) and $injectedValue) {
             $injectedValue = array_combine($injectedValue, $injectedValue);
         } else {
             $injectedValue = array();
         }
         return $injectedValue;
     }
 }
 /**
  * Nastavi parametr na hodnotu.
  * @param string
  * @param mixed
  * @return void
  * @throws NotValidException
  */
 private final function setValueHelper($name, $value, $rule)
 {
     if ($value === IEntity::DEFAULT_VALUE) {
         $value = $this->getDefaultValueHelper($name, $rule);
     }
     if ($rule['relationship'] === MetaData::ManyToOne or $rule['relationship'] === MetaData::OneToOne) {
         if ($value instanceof IEntity) {
             if ($model = $this->getModel(false)) {
                 $repo = $model->getRepository($rule['relationshipParam']);
                 $repo->attach($value);
             } else {
                 if ($model = $value->getModel(false)) {
                     $this->fireEvent('onAttachModel', NULL, $model);
                 }
             }
         } else {
             if ($value !== NULL) {
                 $id = (string) $value;
                 $repo = $this->getModel()->getRepository($rule['relationshipParam']);
                 $value = $repo->getById($id);
                 if (!$value and !isset($rule['types']['null'])) {
                     $type = implode('|', $rule['types']);
                     throw new EntityNotFoundException("Entity({$type}) '{$id}' not found in `" . get_class($repo) . "` in " . get_class($this) . "::\${$name}");
                 }
             }
         }
     } else {
         if (isset($rule['injection'])) {
             if (!isset($this->values[$name]) or !$this->values[$name] instanceof IEntityInjection) {
                 $xValues = isset($this->values[$name]) ? $this->values[$name] : NULL;
                 $tmp = $rule['injection']->invoke($this, $xValues);
                 if ($xValues !== $value) {
                     $tmp->setInjectedValue($value);
                 }
             } else {
                 $tmp = $this->values[$name];
                 $tmp->setInjectedValue($value);
             }
             $value = $tmp;
         }
     }
     if (isset($rule['enum'])) {
         if (in_array($value, $rule['enum']['constants'], true)) {
         } else {
             if (($tmp = array_search($value, $rule['enum']['constants'])) !== false) {
                 $value = $rule['enum']['constants'][$tmp];
             } else {
                 if (isset($rule['types']['null']) and $value === NULL) {
                     $value = NULL;
                 } else {
                     throw new NotValidException([$this, $name, $rule['enum']['original'], $value]);
                 }
             }
         }
     }
     if (!ValidationHelper::isValid($rule['types'], $value)) {
         throw new NotValidException([$this, $name, "'" . implode('|', $rule['types']) . "'", $value]);
     }
     $oldValue = isset($this->values[$name]) ? $this->values[$name] : NULL;
     $this->values[$name] = $value;
     $this->valid[$name] = true;
     $this->changed[$name] = true;
     if ($rule['relationship'] === MetaData::ManyToOne or $rule['relationship'] === MetaData::OneToOne) {
         $meta = $rule['relationshipParam'];
         if ($childParam = $meta->getChildParam()) {
             $oldEntity = $oldValue;
             $newEntity = $value;
             $changed = false;
             if ($oldEntity instanceof IEntity and $newEntity instanceof IEntity) {
                 $changed = $oldEntity !== $newEntity;
             } else {
                 if ($newEntity instanceof IEntity and isset($newEntity->id)) {
                     $changed = (string) $newEntity->id !== (string) $oldEntity;
                 } else {
                     if ($newEntity instanceof IEntity) {
                         if ($oldEntity !== NULL) {
                             $repo = $this->getModel()->getRepository($meta->getRepository());
                             $oldEntity = $repo->getById($oldEntity);
                         }
                         $changed = $oldEntity !== $newEntity;
                     } else {
                         if ($newEntity === NULL) {
                             if (!$oldEntity instanceof IEntity and $oldEntity !== NULL) {
                                 $repo = $this->getModel()->getRepository($meta->getRepository());
                                 $oldEntity = $repo->getById($oldEntity);
                             }
                             $changed = $oldEntity !== NULL;
                         }
                     }
                 }
             }
             if ($changed) {
                 if (!$oldEntity instanceof IEntity and $oldEntity !== NULL) {
                     $repo = $this->getModel()->getRepository($meta->getRepository());
                     $oldEntity = $repo->getById($oldEntity);
                 }
                 if ($rule['relationship'] === MetaData::ManyToOne) {
                     if ($oldEntity) {
                         $oldEntity->{$childParam}->remove($this, 'handled by ManyToOne');
                     }
                     if ($newEntity) {
                         $newEntity->{$childParam}->add($this);
                     }
                 } else {
                     if ($rule['relationship'] === MetaData::OneToOne) {
                         if ($oldEntity and $oldEntity->__isset($childParam) and $oldEntity->__get($childParam) === $this) {
                             $oldEntity->__set($childParam, NULL);
                         }
                         if ($newEntity and (!$newEntity->__isset($childParam) or $newEntity->__get($childParam) !== $this)) {
                             $newEntity->__set($childParam, $this);
                         }
                     }
                 }
             }
         }
     }
 }