1 | <?php |
||
2 | /******************************************************************************* |
||
3 | * This file is part of the GraphQL Bundle package. |
||
4 | * |
||
5 | * (c) YnloUltratech <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | ******************************************************************************/ |
||
10 | |||
11 | namespace Ynlo\GraphQLBundle\Util; |
||
12 | |||
13 | /** |
||
14 | * This helper is used to work with some field options |
||
15 | * in filters, orderByFields, searchFields etc. to get allowed fields |
||
16 | * and configuration if have. |
||
17 | * |
||
18 | * Work with options like: |
||
19 | * |
||
20 | * [ "*", "title, date": false, "description"=> [...] ] |
||
21 | */ |
||
22 | class FieldOptionsHelper |
||
23 | { |
||
24 | /** |
||
25 | * @param array $options |
||
26 | * @param string $name |
||
27 | * @param bool $default |
||
28 | * |
||
29 | * @return bool |
||
30 | */ |
||
31 | 3 | public static function isEnabled(array $options, string $name, $default = false): bool |
|
32 | { |
||
33 | 3 | $enabled = $default; |
|
34 | 3 | $options = self::normalize($options); |
|
35 | |||
36 | 3 | if (array_key_exists('*', $options)) { |
|
37 | 3 | $enabled = $options['*']; |
|
38 | } |
||
39 | |||
40 | 3 | if (array_key_exists($name, $options)) { |
|
41 | 3 | $enabled = (bool) $options[$name]; |
|
42 | } |
||
43 | |||
44 | 3 | return $enabled; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $options |
||
49 | * @param string $name |
||
50 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | 1 | public static function getConfig(array $options, string $name, $default = null) |
|
55 | { |
||
56 | 1 | $options = self::normalize($options); |
|
57 | 1 | if (array_key_exists($name, $options)) { |
|
58 | 1 | return $options[$name]; |
|
59 | } |
||
60 | |||
61 | 1 | return $default; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param array $options |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | 3 | public static function normalize($options) |
|
70 | { |
||
71 | //normalize not indexed fields |
||
72 | 3 | foreach ($options as $key => $value) { |
|
73 | 3 | if (\is_int($key)) { |
|
74 | 2 | if (!isset($options[$value])) { |
|
75 | 2 | $options[$value] = true; |
|
76 | } |
||
77 | 2 | unset($options[$key]); |
|
78 | } |
||
79 | } |
||
80 | |||
81 | //normalize comma separated names |
||
82 | 3 | foreach ($options as $fieldsNames => $option) { |
|
83 | 3 | if (strpos($fieldsNames, ',') !== false) { |
|
84 | 2 | $fieldNamesArray = explode(',', $fieldsNames); |
|
85 | 2 | foreach ($fieldNamesArray as $name) { |
|
86 | 2 | $name = trim($name); |
|
87 | 2 | if (!isset($options[$name])) { |
|
88 | 2 | $options[$name] = $option; |
|
89 | 2 | unset($options[$fieldsNames]); |
|
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | 3 | return $options; |
|
96 | } |
||
97 | } |
||
98 |