diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index 701c4596b44..40eeb3d8bb4 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -20,6 +20,9 @@ * * @Annotation * @Target({"METHOD", "PROPERTY"}) + * @Attributes( + * @Attribute("maxDepth", type="int"), + * ) */ #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] final class ApiSubresource @@ -36,6 +39,8 @@ public function __construct($maxDepth = null) { if (!\is_array($maxDepth)) { // @phpstan-ignore-line $this->maxDepth = $maxDepth; + } else { + $this->maxDepth = $maxDepth['maxDepth'] ?? null; } } } diff --git a/tests/Annotation/ApiSubresourceTest.php b/tests/Annotation/ApiSubresourceTest.php new file mode 100644 index 00000000000..fc1ed117299 --- /dev/null +++ b/tests/Annotation/ApiSubresourceTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Core\Tests\Annotation; + +use ApiPlatform\Core\Annotation\ApiSubresource; +use PHPUnit\Framework\TestCase; + +/** + * @author Cody Banman + */ +class ApiSubresourceTest extends TestCase +{ + public function testAssignation() + { + $property = new ApiSubresource(); + $property->maxDepth = 1; + + $this->assertEquals(1, $property->maxDepth); + } + + public function testConstruct() + { + $property = new ApiSubresource([ // @phpstan-ignore-line + 'maxDepth' => 1, + ]); + $this->assertEquals(1, $property->maxDepth); + } + + /** + * @requires PHP 8.0 + */ + public function testConstructAttribute() + { + $property = eval(<<<'PHP' +return new \ApiPlatform\Core\Annotation\ApiSubresource( + maxDepth: 1 +); +PHP + ); + $this->assertEquals(1, $property->maxDepth); + } +}