Skip to content

Commit

Permalink
Merge 0d78236 into e969429
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Dec 1, 2020
2 parents e969429 + 0d78236 commit 8df9437
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Annotation/ApiSubresource.php
Expand Up @@ -14,17 +14,28 @@
namespace ApiPlatform\Core\Annotation;

/**
* Property annotation.
* ApiSubresource annotation.
*
* @author Antoine Bluchet <soyuka@gmail.com>
*
* @Annotation
* @Target({"METHOD", "PROPERTY"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY|\Attribute::TARGET_METHOD)]
final class ApiSubresource
{
/**
* @var int
*/
public $maxDepth;

/**
* @param int $maxDepth
*/
public function __construct($maxDepth = null)
{
if (!\is_array($maxDepth)) { // @phpstan-ignore-line
$this->maxDepth = $maxDepth;
}
}
}
Expand Up @@ -50,8 +50,12 @@ public function create(string $resourceClass, string $property, array $options =
}

if ($reflectionClass->hasProperty($property)) {
$annotation = $this->reader->getPropertyAnnotation($reflectionClass->getProperty($property), ApiSubresource::class);
$reflectionProperty = $reflectionClass->getProperty($property);
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflectionProperty->getAttributes(ApiSubresource::class)) {
return $this->updateMetadata($attributes[0]->newInstance(), $propertyMetadata, $resourceClass, $property);
}

$annotation = $this->reader->getPropertyAnnotation($reflectionProperty, ApiSubresource::class);
if ($annotation instanceof ApiSubresource) {
return $this->updateMetadata($annotation, $propertyMetadata, $resourceClass, $property);
}
Expand All @@ -68,8 +72,11 @@ public function create(string $resourceClass, string $property, array $options =
continue;
}

$annotation = $this->reader->getMethodAnnotation($reflectionMethod, ApiSubresource::class);
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflectionMethod->getAttributes(ApiSubresource::class)) {
return $this->updateMetadata($attributes[0]->newInstance(), $propertyMetadata, $resourceClass, $property);
}

$annotation = $this->reader->getMethodAnnotation($reflectionMethod, ApiSubresource::class);
if ($annotation instanceof ApiSubresource) {
return $this->updateMetadata($annotation, $propertyMetadata, $resourceClass, $property);
}
Expand Down

0 comments on commit 8df9437

Please sign in to comment.