|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Get; |
| 17 | +use ApiPlatform\Metadata\GetCollection; |
| 18 | +use ApiPlatform\Metadata\HeaderParameter; |
| 19 | +use ApiPlatform\Metadata\Link; |
| 20 | +use ApiPlatform\Metadata\Parameter; |
| 21 | +use ApiPlatform\Metadata\QueryParameter; |
| 22 | +use ApiPlatform\Serializer\Filter\GroupFilter; |
| 23 | +use ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider; |
| 24 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 25 | +use Symfony\Component\Serializer\Attribute\Groups; |
| 26 | + |
| 27 | +#[Get( |
| 28 | + uriTemplate: 'with_parameters/{id}{._format}', |
| 29 | + uriVariables: [ |
| 30 | + 'id' => new Link(schema: ['type' => 'uuid'], property: 'id'), |
| 31 | + ], |
| 32 | + parameters: [ |
| 33 | + 'groups' => new QueryParameter(filter: new GroupFilter(parameterName: 'groups', overrideDefaultGroups: false)), |
| 34 | + 'group' => new QueryParameter(provider: [self::class, 'provideGroup']), |
| 35 | + 'properties' => new QueryParameter(filter: 'my_dummy.property'), |
| 36 | + 'service' => new QueryParameter(provider: CustomGroupParameterProvider::class), |
| 37 | + 'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']), |
| 38 | + 'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10), |
| 39 | + 'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20), |
| 40 | + 'array' => new QueryParameter(provider: [self::class, 'assertArray']), |
| 41 | + ], |
| 42 | + provider: [self::class, 'provide'] |
| 43 | +)] |
| 44 | +#[GetCollection( |
| 45 | + uriTemplate: 'with_parameters_collection', |
| 46 | + parameters: [ |
| 47 | + 'hydra' => new QueryParameter(property: 'a', required: true), |
| 48 | + ], |
| 49 | + provider: [self::class, 'collectionProvider'] |
| 50 | +)] |
| 51 | +#[QueryParameter(key: 'everywhere')] |
| 52 | +class WithParameter |
| 53 | +{ |
| 54 | + protected static int $counter = 1; |
| 55 | + public int $id = 1; |
| 56 | + |
| 57 | + #[Groups(['a'])] |
| 58 | + public $a = 'foo'; |
| 59 | + #[Groups(['b', 'custom'])] |
| 60 | + public $b = 'bar'; |
| 61 | + |
| 62 | + public static function collectionProvider() |
| 63 | + { |
| 64 | + return [new self()]; |
| 65 | + } |
| 66 | + |
| 67 | + public static function provide() |
| 68 | + { |
| 69 | + return new self(); |
| 70 | + } |
| 71 | + |
| 72 | + public static function assertArray(): void |
| 73 | + { |
| 74 | + } |
| 75 | + |
| 76 | + public static function assertFirst(): void |
| 77 | + { |
| 78 | + \assert(1 === static::$counter); |
| 79 | + ++static::$counter; |
| 80 | + } |
| 81 | + |
| 82 | + public static function assertSecond(): void |
| 83 | + { |
| 84 | + \assert(2 === static::$counter); |
| 85 | + } |
| 86 | + |
| 87 | + public static function provideGroup(Parameter $parameter, array $parameters = [], array $context = []) |
| 88 | + { |
| 89 | + $operation = $context['operation']; |
| 90 | + |
| 91 | + return $operation->withNormalizationContext(['groups' => $parameters['group']]); |
| 92 | + } |
| 93 | + |
| 94 | + public static function restrictAccess(): void |
| 95 | + { |
| 96 | + throw new AccessDeniedHttpException(); |
| 97 | + } |
| 98 | +} |
0 commit comments