Example #1
0
 /**
  * Get field by path
  *
  * @param JsonDefinition $definition JSON definition
  * @param string         $path       Path to field
  * @return DefinitionElementInterface
  */
 private function getFieldByPath(JsonDefinition $definition, $path)
 {
     $items = explode('.', $path);
     $field = $definition->getField(array_shift($items));
     foreach ($items as $item) {
         if ($item === '0') {
             if (!$field instanceof JsonDefinitionArray) {
                 throw new \InvalidArgumentException(sprintf('Error path: "%s"', $path));
             }
             $field = $field->getElement();
         } else {
             if (!$field instanceof JsonDefinitionHash) {
                 throw new \InvalidArgumentException(sprintf('Error path: "%s"', $path));
             }
             $field = $field->getJsonDefinition()->getField($item);
         }
     }
     return $field;
 }
 /**
  * Test JsonDefinitionHash::getJsonDefinition()
  *
  * @return void
  */
 public function testGetJsonDefinition()
 {
     $parent = new JsonDefinition((new Schema\Definition())->setId('Parent')->setTarget((new Schema\Target())->setFields([(new Schema\Field())->setName('hash')->setType('hash')->setDescription('description'), (new Schema\Field())->setName('hash.b')->setType('class:B'), (new Schema\Field())->setName('hash.c')->setType('class:C[]'), (new Schema\Field())->setName('hash.d')->setType(JsonDefinitionHash::TYPE_BOOLEAN)])->setRelations([(new Schema\Relation())->setType(JsonDefinitionHash::REL_TYPE_EMBED)->setLocalProperty('hash.b'), (new Schema\Relation())->setType(JsonDefinitionHash::REL_TYPE_REF)->setLocalProperty('hash.c')])));
     /** @var JsonDefinitionHash $field */
     $field = $parent->getField('hash');
     $this->assertInstanceOf('Graviton\\GeneratorBundle\\Definition\\JsonDefinitionHash', $field);
     $this->assertEquals(new JsonDefinition((new Schema\Definition())->setId('ParentHash')->setDescription('description')->setIsSubDocument(true)->setTarget((new Schema\Target())->setFields([(new Schema\Field())->setName('b')->setType('class:B'), (new Schema\Field())->setName('c')->setType('class:C[]'), (new Schema\Field())->setName('d')->setType(JsonDefinitionHash::TYPE_BOOLEAN)])->setRelations([(new Schema\Relation())->setType(JsonDefinitionHash::REL_TYPE_EMBED)->setLocalProperty('b'), (new Schema\Relation())->setType(JsonDefinitionHash::REL_TYPE_REF)->setLocalProperty('c')]))), $field->getJsonDefinition());
 }