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\Core\PathResolver; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; |
18
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
19
|
|
|
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generates an operation path. |
23
|
|
|
* |
24
|
|
|
* @author Antoine Bluchet <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class OperationPathResolver implements OperationPathResolverInterface |
27
|
|
|
{ |
28
|
|
|
private $pathSegmentNameGenerator; |
29
|
|
|
|
30
|
|
|
public function __construct(PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
31
|
|
|
{ |
32
|
|
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string |
39
|
|
|
{ |
40
|
|
|
if (func_num_args() < 4) { |
41
|
|
|
@trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$operationType = OperationTypeDeprecationHelper::getOperationType($operationType); |
45
|
|
|
|
46
|
|
|
if (OperationType::SUBRESOURCE === $operationType) { |
47
|
|
|
throw new InvalidArgumentException('Subresource operations are not supported by the OperationPathResolver.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$path = '/'.$this->pathSegmentNameGenerator->getSegmentName($resourceShortName, true); |
51
|
|
|
|
52
|
|
|
if ($operationType === OperationType::ITEM) { |
53
|
|
|
$path .= '/{id}'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$path .= '.{_format}'; |
57
|
|
|
|
58
|
|
|
return $path; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: