Repository navigation
Expand file tree
/
Copy pathResourceExtractorTrait.php
More file actions
123 lines (100 loc) · 4.02 KB
/
Copy pathResourceExtractorTrait.php
File metadata and controls
123 lines (100 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Metadata\Extractor;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use Symfony\Component\Config\Util\XmlUtils;
/**
* Utils for ResourceExtractors.
*
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
trait ResourceExtractorTrait
{
private function buildArrayValue(\SimpleXMLElement|array|null $resource, string $key, mixed $default = null): ?array
{
if (\is_object($resource)) {
if (!isset($resource->{$key.'s'}->{$key})) {
return $default;
}
return (array) $resource->{$key.'s'}->{$key};
}
if (empty($resource[$key])) {
return $default;
}
if (!\is_array($resource[$key])) {
throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be an array, %s given', $key, \gettype($resource[$key])));
}
return $resource[$key];
}
/**
* Transforms an attribute's value in a PHP value.
*
* Container parameters (%param%) found in plain string values are resolved against the
* service container. ExpressionLanguage fields (security, conditions, …) must opt out via
* $resolve = false so their %param% tokens reach the expression engine untouched, then route
* the raw value through resolveExpressionPlaceholder() to recover whole-string %param% refs.
*/
private function phpize(\SimpleXMLElement|array|null $resource, string $key, string $type, mixed $default = null, bool $resolve = true): array|bool|int|string|null
{
if (!isset($resource[$key])) {
return $default;
}
switch ($type) {
case 'bool|string':
return \is_bool($resource[$key]) || \in_array((string) $resource[$key], ['1', '0', 'true', 'false'], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string', resolve: $resolve);
case 'string':
$value = (string) $resource[$key];
return $resolve ? $this->resolve($value) : $value;
case 'integer':
return (int) $resource[$key];
case 'bool':
if (\is_object($resource)) {
return (bool) XmlUtils::phpize($resource[$key]);
}
return \is_bool($resource[$key]) ? $resource[$key] : filter_var($resource[$key], \FILTER_VALIDATE_BOOLEAN);
}
throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key])));
}
private function buildArgs(\SimpleXMLElement $resource): ?array
{
if (!isset($resource->args->arg)) {
return null;
}
$data = [];
foreach ($resource->args->arg as $arg) {
$data[(string) $arg['id']] = $this->buildValues($arg->values);
}
return $data;
}
private function buildExtraArgs(\SimpleXMLElement $resource): ?array
{
if (!isset($resource->extraArgs->arg)) {
return null;
}
$data = [];
foreach ($resource->extraArgs->arg as $arg) {
$data[(string) $arg['id']] = $this->buildValues($arg->values);
}
return $data;
}
private function buildValues(\SimpleXMLElement $resource): array
{
$data = [];
foreach ($resource->value as $value) {
if (null !== $value->attributes()->name) {
$data[(string) $value->attributes()->name] = isset($value->values) ? $this->buildValues($value->values) : XmlUtils::phpize($value->__toString());
continue;
}
$data[] = isset($value->values) ? $this->buildValues($value->values) : XmlUtils::phpize($value->__toString());
}
return $data;
}
}