Example #1
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;
 }