Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, Property $property)
 {
     if (is_string($value)) {
         $value = new \DateTime($value);
     }
     if (!is_object($value) && !$value instanceof \DateTime) {
         throw new \InvalidArgumentException(sprintf('Property "%s" must be a string or a DateTime object', $property->getName()));
     }
     return $value->format(\DateTime::ISO8601);
 }
Exemple #2
0
 /**
  * Use inner type to convert each array's value
  *
  * @param mixed $value
  * @param Property $property
  * @param string $method
  *
  * @return array
  */
 protected function convert($value, Property $property, $method)
 {
     if (!$property->hasOption('inner_type')) {
         throw new IncompletePropertyDefinitionException(sprintf('An inner type must be defined for the property "%s"', $property->getName()));
     }
     $value = (array) $value;
     $type = Types::getType($property->getOption('inner_type'));
     if ($type instanceof self) {
         throw new \LogicException('Array imbrication is not allowed');
     }
     foreach ($value as &$val) {
         $val = $type->{$method}($val, $property);
     }
     return $value;
 }
Exemple #3
0
 /**
  * Find the nodes related to the relationship
  *
  * @param Property $property
  * @param array $info
  *
  * @return object
  */
 protected function getRelationshipNode(Property $property, array $info)
 {
     $nodeClass = $this->map->getClass($property->getOption('node'));
     foreach ($this->entities as $entity) {
         if (!$entity instanceof $nodeClass) {
             continue;
         }
         $nodeInfo = $this->entities->getInfo($entity);
         if ($nodeInfo['realId'] === $info[$property->getType()]) {
             return $entity;
         }
     }
     $query = new Query(sprintf('MATCH (n:%s) WHERE id(n) = {where}.id RETURN n;', $nodeClass));
     $query->addVariable('n', $nodeClass);
     $query->addParameters('where', ['id' => $info[$property->getType()]]);
     return $this->uow->execute($query)->current();
 }
Exemple #4
0
 /**
  * Build the metadata object for the given class
  *
  * @param string $class
  * @param array $config
  *
  * @return Metadata
  */
 protected function buildMetadata($class, $config)
 {
     switch ($config['type']) {
         case 'relationship':
             $metadata = new RelationshipMetadata();
             $this->configureRelationship($metadata, $config);
             break;
         default:
             $metadata = new NodeMetadata();
             $this->configureNode($metadata, $config);
     }
     $id = new Id();
     $id->setProperty(array_keys($config['id'])[0])->setType($config['id'][$id->getProperty()]['type'])->setStrategy($config['id'][$id->getProperty()]['generator']['strategy']);
     $metadata->setClass($class)->setId($id)->addProperty((new Property())->setName($id->getProperty())->setNullable(false)->setType($id->getType()));
     if (isset($config['repository'])) {
         $metadata->setRepositoryClass($config['repository']);
     }
     if (isset($config['alias'])) {
         $metadata->setAlias($config['alias']);
     }
     if (isset($config['properties'])) {
         foreach ($config['properties'] as $prop => $conf) {
             switch ($conf['type']) {
                 case 'relationship':
                     if ($config['type'] === 'relationship') {
                         throw new \LogicException(sprintf('The relationship "%s" can\'t have a relationship property on "%s"', $class, $prop));
                     }
                     if (!isset($conf['relationship'])) {
                         throw new \LogicException(sprintf('Missing option "relationship" for the property "%s" on "%s"', $prop, $class));
                     }
                     break;
                 case 'startNode':
                 case 'endNode':
                     if ($config['type'] === 'node') {
                         throw new \LogicException(sprintf('The node "%s" can\'t have the property "%s" type set to "%s"', $class, $prop, $conf['type']));
                     }
                     if (!isset($conf['node'])) {
                         throw new \LogicException(sprintf('Missing option "node" for the property "%s" on "%s"', $prop, $class));
                     }
                     break;
             }
             $property = new Property();
             $property->setName($prop)->setType($conf['type']);
             if (isset($conf['nullable'])) {
                 $property->setNullable($conf['nullable']);
                 unset($conf['nullable']);
             }
             unset($conf['type']);
             foreach ($conf as $key => $value) {
                 $property->addOption($key, $value);
             }
             $metadata->addProperty($property);
         }
     }
     return $metadata;
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, Property $property)
 {
     return json_decode($value, $property->hasOption('associative') ? (bool) $property->getOption('associative') : false);
 }