Skip to content

Commit

Permalink
Merge bd05786 into 67bd7d8
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Sep 5, 2021
2 parents 67bd7d8 + bd05786 commit 5677029
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/Metadata/Property/Factory/AttributePropertyMetadataFactory.php
Expand Up @@ -55,7 +55,7 @@ public function create(string $resourceClass, string $property, array $options =
if ($reflectionClass->hasProperty($property)) {
$reflectionProperty = $reflectionClass->getProperty($property);
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflectionProperty->getAttributes(ApiProperty::class)) {
return $attributes[0]->newInstance();
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
}
}

Expand All @@ -70,9 +70,8 @@ public function create(string $resourceClass, string $property, array $options =
continue;
}

$annotation = null;
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflectionMethod->getAttributes(ApiProperty::class)) {
return $attributes[0]->newInstance();
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
}
}

Expand All @@ -96,4 +95,31 @@ private function handleNotFound($parentPropertyMetadata, string $resourceClass,

throw new PropertyNotFoundException(sprintf('Property "%s" of class "%s" not found.', $property, $resourceClass));
}

private function createMetadata(ApiProperty $attribute, ApiProperty $propertyMetadata = null): ApiProperty
{
if (null === $propertyMetadata) {
return $attribute;
}

foreach ([
['get', 'Description'],
['is', 'Readable'],
['is', 'Writable'],
['is', 'ReadableLink'],
['is', 'WritableLink'],
['is', 'Required'],
['is', 'Identifier'],
['get', 'Default'],
['get', 'Example'],
['get', 'Types'],
// TODO: do we need to copy more properties?
] as $property) {
if (null !== $val = $attribute->{$property[0].$property[1]}()) {
$propertyMetadata->{"with{$property[1]}"}($val);
}
}

return $propertyMetadata;
}
}
Expand Up @@ -35,6 +35,9 @@ class DummyPhp8ApiPropertyAttribute
*/
public $filtered;

#[ApiProperty]
public $empty;

#[ApiProperty(description: 'a foo')]
public function getFoo(): int
{
Expand Down
Expand Up @@ -62,4 +62,30 @@ public function testClassNotFoundButParentFound()
$factory = new AttributePropertyMetadataFactory($decoratedProphecy->reveal());
$this->assertEquals($propertyMetadata, $factory->create('\DoNotExist', 'foo'));
}

/**
* @requires PHP 8.0
*/
public function testClassFoundAndParentFound()
{
$parentPropertyMetadata = (new ApiProperty('Desc', true, false, true, false, true, false, 'Default', 'Example'))->withTypes(['https://example.com']);

$decoratedProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedProphecy->create(DummyPhp8ApiPropertyAttribute::class, 'empty', [])->willReturn($parentPropertyMetadata);

$factory = new AttributePropertyMetadataFactory($decoratedProphecy->reveal());
$metadata = $factory->create(DummyPhp8ApiPropertyAttribute::class, 'empty');

$this->assertSame($parentPropertyMetadata, $metadata);
$this->assertSame('Desc', $metadata->getDescription());
$this->assertTrue($metadata->isReadable());
$this->assertFalse($metadata->isWritable());
$this->assertTrue($metadata->isReadableLink());
$this->assertFalse($metadata->isWritableLink());
$this->assertTrue($metadata->isRequired());
$this->assertFalse($metadata->isIdentifier());
$this->assertSame('Default', $metadata->getDefault());
$this->assertSame('Example', $metadata->getExample());
$this->assertSame(['https://example.com'], $metadata->getTypes());
}
}

0 comments on commit 5677029

Please sign in to comment.