Inheritance: extends Nette\Object
Example #1
0
 protected function &_getValue(PropertyMetadata $metadata, $name, $allowNull = FALSE)
 {
     $this->initDefaultValue($metadata);
     if (!$metadata->isReadonly && !isset($this->validated[$name]) && !$metadata->container) {
         if (!($allowNull && empty($this->data[$name])) && in_array($name, $this->metadata->getStorageProperties(), TRUE)) {
             $this->_setValue($metadata, $name, $this->data[$name]);
             unset($this->modified[$name]);
         }
     } elseif ($metadata->container && !is_object($this->data[$name])) {
         $class = $metadata->container;
         $this->data[$name] = new $class($this, $metadata, $this->data[$name]);
         $this->validated[$name] = TRUE;
     }
     if ($metadata->hasGetter && !isset($this->getterCall[$name])) {
         $this->getterCall[$name] = TRUE;
         $value = call_user_func([$this, 'get' . $name]);
         unset($this->getterCall[$name]);
         return $value;
     }
     if ($this->data[$name] instanceof IPropertyContainer) {
         $value = $this->data[$name]->getInjectedValue();
         return $value;
     } else {
         if (!isset($this->data[$name]) && !$metadata->isNullable && !$allowNull) {
             throw new InvalidStateException("Property '{$name}' is not set.");
         }
         return $this->data[$name];
     }
 }
Example #2
0
 protected function addProperties(array $properties)
 {
     $path = $this->lookupPath(self::class, FALSE);
     $delimiter = strpos($path, '-');
     if ($delimiter !== FALSE) {
         $path = substr($path, 0, $delimiter);
     }
     $parent = $this->lookup(self::class, FALSE);
     if ($path && $parent instanceof self) {
         $parentProperty = $parent->getMetadata()->getProperty($path);
         if ($parentProperty->relationship && !$parentProperty->relationship->property) {
             $parent->relations[$path] = $this->getEntity();
         }
     }
     foreach ($properties as $metadata) {
         if (in_array($metadata->name, $this->metadata->getPrimaryKey())) {
             continue;
         }
         if ($path && $parent instanceof self && is_subclass_of($metadata->container, Nextras\Orm\Relationships\HasOne::class)) {
             if ($metadata->relationship && $metadata->relationship->property === $path && $metadata->relationship->repository === get_class($parent->getRepository())) {
                 $this->relations[$metadata->name] = $parent->getEntity();
                 continue;
             }
         }
         $this->addProperty($metadata);
     }
 }
 /**
  * @param string $propertyName
  * @throws InvalidPropertyException
  */
 public function validateProperty($propertyName)
 {
     try {
         $this->entityMetadata->getProperty($propertyName);
     } catch (InvalidArgumentException $e) {
         throw InvalidPropertyException::createNonexistentProperty($propertyName, $e);
     }
 }
Example #4
0
 protected function initPrimaryKey()
 {
     $primaryKey = array_values(array_filter(array_map(function (PropertyMetadata $metadata) {
         return $metadata->isPrimary && !$metadata->isVirtual ? $metadata->name : null;
     }, $this->metadata->getProperties())));
     if (empty($primaryKey)) {
         throw new InvalidStateException("Entity {$this->reflection->name} does not have defined any primary key.");
     } elseif (!$this->metadata->hasProperty('id') || !$this->metadata->getProperty('id')->isPrimary) {
         throw new InvalidStateException("Entity {$this->reflection->name} has to have defined \$id property as {primary} or {primary-proxy}.");
     }
     $this->metadata->setPrimaryKey($primaryKey);
 }
Example #5
0
 protected function parseFilteredRelationship(PropertyMetadata $property, $args)
 {
     $sourceName = ltrim($args[0], '$');
     $sourceProperty = $this->metadata->getProperty($sourceName);
     if ($sourceProperty->relationshipType === PropertyMetadata::RELATIONSHIP_ONE_HAS_ONE || $sourceProperty->relationshipType === PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE) {
         $property->container = 'Nextras\\Orm\\Entity\\PropertyContainers\\FilteredRelationshipContainerContainer';
     } else {
         $property->container = 'Nextras\\Orm\\Entity\\PropertyContainers\\FilteredRelationshipCollectionContainer';
     }
     $property->args = [$sourceName];
     unset($this->storageProperties[$property->name]);
 }
 protected function getterId()
 {
     $keys = $this->metadata->getPrimaryKey();
     if (count($keys) === 1) {
         return $this->getRawValue($keys[0]);
     } else {
         $primary = [];
         foreach ($keys as $key) {
             $primary[] = $this->getRawValue($key);
         }
         return $primary;
     }
 }
 protected function parseAnnotationValue($name, array $types, $access, $params)
 {
     $property = new PropertyMetadata($name, $types, $access);
     $this->metadata->setProperty($name, $property);
     if ($params) {
         preg_match_all('#\\{([^}]+)\\}#i', $params, $matches, PREG_SET_ORDER);
         if ($matches) {
             foreach ($matches as $match) {
                 $this->processPropertyModifier($property, preg_split('#\\s+#', $match[1]));
             }
         }
     }
 }
Example #8
0
 private function setterPrimaryProxy($value, PropertyMetadata $metadata)
 {
     $keys = $this->metadata->getPrimaryKey();
     if (!$metadata->isVirtual) {
         return $value;
     }
     if (count($keys) !== count($value)) {
         $class = get_class($this);
         throw new InvalidStateException("Value for {$class}::\$id has insufficient number of parameters.");
     }
     $value = (array) $value;
     foreach ($keys as $key) {
         $this->setRawValue($key, array_shift($value));
     }
     return IEntity::SKIP_SET_VALUE;
 }
Example #9
0
 protected function parseAnnotationValue($name, array $types, $access, $propertyComment)
 {
     $property = new PropertyMetadata($name, $types, $access);
     $this->metadata->setProperty($name, $property);
     if (!$propertyComment) {
         return;
     }
     $matches = $this->modifierParser->matchModifiers($propertyComment);
     foreach ($matches as $macroContent) {
         try {
             $args = $this->modifierParser->parse($macroContent, $this->currentReflection);
         } catch (InvalidModifierDefinitionException $e) {
             throw new InvalidModifierDefinitionException("Invalid maco definition for {$this->currentReflection->name}::\${$name} property.", 0, $e);
         }
         $this->processPropertyModifier($property, $args[0], $args[1]);
     }
 }
Example #10
0
 public function getter($element, $chain, EntityMetadata $sourceEntityMeta)
 {
     $column = array_shift($chain);
     $propertyMeta = $sourceEntityMeta->getProperty($column);
     // check if property exists
     $value = $element->hasValue($column) ? $element->getValue($column) : null;
     if ($value instanceof IRelationshipCollection) {
         throw new InvalidStateException('You can not sort by hasMany relationship.');
     }
     if (!$chain) {
         return $this->normalizeValue($value, $propertyMeta);
     } else {
         $targetEntityMeta = $this->metadataStorage->get($propertyMeta->relationship->entity);
         return $this->getter($value, $chain, $targetEntityMeta);
     }
 }