/** * @test */ public function test_parse_property_annotation() { $intf = new \ReflectionClass(Todo::class); $commentParser = new AnnotationConverterAdapter($intf); id: $annotations = $commentParser->getPropertyAnnotations($intf->getProperty('id')); $this->assertCount(2, $annotations); $this->assertInstanceOf(Column::class, $annotations[0]); $this->assertEquals('todo_id', $annotations[0]->name); $this->assertNull($annotations[0]->default); $this->assertCount(0, $annotations[0]->optFields); $this->assertInstanceOf(ColumnType::class, $annotations[1]); $this->assertEquals('integer', $annotations[1]->type); $this->assertEquals('id', $annotations[1]->name); todo: $annotations = $commentParser->getPropertyAnnotations($intf->getProperty('todo')); $this->assertCount(3, $annotations); $this->assertInstanceOf(Column::class, $annotations[0]); $this->assertEquals('content', $annotations[0]->name); $this->assertNull($annotations[0]->default); $this->assertCount(0, $annotations[0]->optFields); $this->assertInstanceOf(Alias::class, $annotations[1]); $this->assertEquals('content', $annotations[1]->name); $this->assertEquals(['text', 'memo'], $annotations[1]->alias); $this->assertInstanceOf(ColumnType::class, $annotations[2]); $this->assertEquals('string', $annotations[2]->type); $this->assertEquals('todo', $annotations[2]->name); created: $annotations = $commentParser->getPropertyAnnotations($intf->getProperty('created')); $this->assertCount(1, $annotations); $this->assertInstanceOf(ColumnType::class, $annotations[0]); $this->assertEquals(\DateTime::class, $annotations[0]->type); $this->assertEquals('created', $annotations[0]->name); hidden: $annotations = $commentParser->getPropertyAnnotations($intf->getProperty('hidden')); $this->assertCount(2, $annotations); $this->assertInstanceOf(Column::class, $annotations[0]); $this->assertNull(null, $annotations[0]->name); $this->assertEquals(0, $annotations[0]->default); $this->assertCount(0, $annotations[0]->optFields); $this->assertInstanceOf(ColumnType::class, $annotations[1]); $this->assertEquals(Hidden::class, $annotations[1]->type); $this->assertEquals('hidden', $annotations[1]->name); creator: $annotations = $commentParser->getPropertyAnnotations($intf->getProperty('creator')); $this->assertCount(4, $annotations); $this->assertInstanceOf(Column::class, $annotations[0]); $this->assertEquals('creator_id', $annotations[0]->name); $this->assertNull($annotations[0]->default); $this->assertEquals(['creator_name'], $annotations[0]->optFields); $this->assertInstanceOf(Alias::class, $annotations[1]); $this->assertEquals('creator_id', $annotations[1]->name); $this->assertEquals(['maintener_id'], $annotations[1]->alias); $this->assertInstanceOf(Alias::class, $annotations[2]); $this->assertEquals('creator_name', $annotations[2]->name); $this->assertEquals(['maintener_name'], $annotations[2]->alias); $this->assertInstanceOf(ColumnType::class, $annotations[3]); $this->assertEquals(Target\Editor::class, $annotations[3]->type); $this->assertEquals('creator', $annotations[3]->name); }
public function parseAsEntity(\ReflectionClass $ref, CaseSensor $sensor) { $reader = new AnnotationConverterAdapter($ref); $fields = array_reduce($ref->getProperties(), function (array &$tmp, \ReflectionProperty $f) use($reader, $sensor) { $annotations = $reader->getPropertyAnnotations($f); $columnType = $this->extractAnnotation($annotations, ColumnType::class, function ($a) { return $a->type; }); list($alias, $default, $optFields) = $this->extractAnnotation($annotations, Column::class, function ($a) { return [$a->name, $a->default, $a->optFields]; }); $domain = $this->parseInternal($f->name, $columnType, $sensor, false); $optFields = array_map(function ($name) use($sensor) { return $sensor->convert($name); }, $optFields !== null ? $optFields : []); return $tmp + [$f->name => new NamedAliasDomain($domain, $sensor->convert($f->name), $this->convertNames([$alias], $sensor), $default, array_flip($optFields))]; }, []); return new ObjectDomain($ref->name, $fields); }