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\Util; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Extracts data used by the library form given attributes. |
20
|
|
|
* |
21
|
|
|
* @author Antoine Bluchet <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
final class AttributesExtractor |
26
|
|
|
{ |
27
|
|
|
private function __construct() |
28
|
|
|
{ |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Extracts resource class, operation name and format request attributes. Returns an empty array if the request does |
33
|
|
|
* not contain required attributes. |
34
|
|
|
*/ |
35
|
|
|
public static function extractAttributes(array $attributes): array |
36
|
|
|
{ |
37
|
|
|
$result = ['resource_class' => $attributes['_api_resource_class'] ?? null]; |
38
|
|
|
|
39
|
|
|
if ($subresourceContext = $attributes['_api_subresource_context'] ?? null) { |
40
|
|
|
$result['subresource_context'] = $subresourceContext; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (null === $result['resource_class']) { |
44
|
|
|
return []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$hasRequestAttributeKey = false; |
48
|
|
|
foreach (OperationType::TYPES as $operationType) { |
49
|
|
|
$attribute = "_api_{$operationType}_operation_name"; |
50
|
|
|
if (isset($attributes[$attribute])) { |
51
|
|
|
$result["{$operationType}_operation_name"] = $attributes[$attribute]; |
52
|
|
|
$hasRequestAttributeKey = true; |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (false === $hasRequestAttributeKey) { |
58
|
|
|
return []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (null === $apiRequest = $attributes['_api_receive'] ?? null) { |
62
|
|
|
$result['receive'] = true; |
63
|
|
|
} else { |
64
|
|
|
$result['receive'] = (bool) $apiRequest; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $result; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|