/**
  * @param string $entityClass
  * @param string $key
  * @param null $column
  * @return StoredEntity|null
  * @throws NoReferenceException
  */
 public function reference($entityClass, $key = null, $column = null)
 {
     if ($this->cachedReferences == null) {
         $this->cachedReferences = new ArrayHash();
     }
     if ($this->isNewEntity()) {
         return null;
     }
     if ($key == null) {
         $columnToDiscoverReference = $entityClass;
         $reflectProperty = $this->getPropertyByColumnName($columnToDiscoverReference);
         if ($reflectProperty) {
             $entityClass = $this->getReferenceClassForProperty($reflectProperty);
             if (!$entityClass) {
                 throw new NoReferenceException($columnToDiscoverReference);
             }
             $column = $columnToDiscoverReference;
             $key = $entityClass::getTableName($this->annotationReader);
         }
     }
     self::mustBeChildOf($entityClass, StoredEntity::class);
     if (!$this->cachedReferences->offsetExists($key)) {
         $reference = $entityClass::create($this, $this->row->getTable()->getReferencedTable($this->row, $key, $column));
         $this->cachedReferences->offsetSet($key, $reference);
     }
     return $this->cachedReferences->offsetGet($key);
 }
 /**
  * Create new entity from array or string which is JSON
  * @param string|array $data
  * @param bool $inverse
  * @throws \Nette\Utils\JsonException
  */
 public function __construct($data = NULL, $inverse = false)
 {
     if ($data !== null) {
         if (is_string($data)) {
             $data = Json::decode($data);
         }
         $this->initMapping($inverse ? 'out' : 'in');
         foreach ($data as $key => $value) {
             if ($value instanceof \stdClass) {
                 $value = (array) $value;
             }
             $reflection = $this->getReflection();
             if ($this->attributeNames->offsetExists($key)) {
                 $propertyName = $this->attributeNames->offsetGet($key);
                 if ($reflection->hasProperty($propertyName)) {
                     $property = $reflection->getProperty($propertyName);
                     $type = $property->getAnnotation('var');
                     if (Strings::endsWith($type, 'Entity')) {
                         $classWithNamespace = sprintf("\\%s\\%s", $reflection->getNamespaceName(), (string) $type);
                         if (is_array($value) && $property->hasAnnotation('collection')) {
                             $arrayData = array();
                             foreach ($value as $valueData) {
                                 $arrayData[] = new $classWithNamespace($valueData, $inverse);
                             }
                             $this->{$propertyName} = $arrayData;
                         } else {
                             $this->{$propertyName} = new $classWithNamespace($value);
                         }
                     } else {
                         $this->{$propertyName} = $value;
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, $isNew = FALSE)
 {
     $classMetadata = $this->managerRegistry->getManagerForClass(get_class($entity))->getClassMetadata(get_class($entity));
     foreach (array_merge($classMetadata->getFieldNames(), $classMetadata->getAssociationNames()) as $fieldName) {
         $propertyReflection = new Nette\Reflection\Property(get_class($entity), $fieldName);
         /** @var Doctrine\Mapping\Annotation\Crud $crud */
         if ($crud = $this->annotationReader->getPropertyAnnotation($propertyReflection, self::EXTENSION_ANNOTATION)) {
             if ($isNew && $crud->isRequired() && !$values->offsetExists($fieldName)) {
                 throw new Exceptions\InvalidStateException('Missing required key "' . $fieldName . '"');
             }
             if (!$values->offsetExists($fieldName) || !$isNew && !$crud->isWritable() || $isNew && !$crud->isRequired()) {
                 continue;
             }
             $value = $values->offsetGet($fieldName);
             if ($value instanceof Utils\ArrayHash || is_array($value)) {
                 if (!$classMetadata->getFieldValue($entity, $fieldName) instanceof Entities\IEntity) {
                     $propertyAnnotations = $this->annotationReader->getPropertyAnnotations($propertyReflection);
                     $annotations = array_map(function ($annotation) {
                         return get_class($annotation);
                     }, $propertyAnnotations);
                     if (in_array('Doctrine\\ORM\\Mapping\\OneToOne', $annotations, TRUE)) {
                         $className = $this->annotationReader->getPropertyAnnotation($propertyReflection, 'Doctrine\\ORM\\Mapping\\OneToOne')->targetEntity;
                     } elseif (in_array('Doctrine\\ORM\\Mapping\\ManyToOne', $annotations, TRUE)) {
                         $className = $this->annotationReader->getPropertyAnnotation($propertyReflection, 'Doctrine\\ORM\\Mapping\\ManyToOne')->targetEntity;
                     } else {
                         $className = $propertyReflection->getAnnotation('var');
                     }
                     // Check if class is callable
                     if (class_exists($className)) {
                         $classMetadata->setFieldValue($entity, $fieldName, new $className());
                     } else {
                         $classMetadata->setFieldValue($entity, $fieldName, $value);
                     }
                 }
                 // Check again if entity was created
                 if (($fieldValue = $classMetadata->getFieldValue($entity, $fieldName)) && $fieldValue instanceof Entities\IEntity) {
                     $classMetadata->setFieldValue($entity, $fieldName, $this->fillEntity(Utils\ArrayHash::from((array) $value), $fieldValue, $isNew));
                 }
             } else {
                 if ($crud->validator !== NULL) {
                     $value = $this->validateProperty($crud->validator, $value);
                 }
                 $classMetadata->setFieldValue($entity, $fieldName, $value);
             }
         }
     }
     return $entity;
 }
Exemple #4
0
 /**
  * @param Utils\ArrayHash $element
  *
  * @return bool
  */
 protected function checkRoles(Utils\ArrayHash $element)
 {
     // Check if element has role parameter
     if ($element->offsetExists('role')) {
         $roles = (array) $element->offsetGet('role');
         foreach ($roles as $role) {
             if ($this->user->isInRole($role)) {
                 return TRUE;
             }
         }
         return FALSE;
     }
     return TRUE;
 }