Completed
Push — master ( 3b79f3...426227 )
by Kirill
03:11
created

Input.php (6 issues)

1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Adapters\Webonyx;
11
12
use GraphQL\Type\Definition\ResolveInfo;
13
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
0 ignored issues
show
The type Railt\Reflection\Contrac...initions\TypeDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Railt\Reflection\Contracts\Dependent\Argument\HasArguments;
0 ignored issues
show
The type Railt\Reflection\Contrac...t\Argument\HasArguments was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Railt\Reflection\Contracts\Dependent\ArgumentDefinition;
0 ignored issues
show
The type Railt\Reflection\Contrac...dent\ArgumentDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Railt\Reflection\Contracts\Dependent\FieldDefinition;
0 ignored issues
show
The type Railt\Reflection\Contrac...pendent\FieldDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Railt\Routing\Contracts\InputInterface;
0 ignored issues
show
The type Railt\Routing\Contracts\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Class Input
21
 */
22
class Input implements InputInterface
23
{
24
    public const DEPTH_DELIMITER = '.';
25
26
    /**
27
     * @var array
28
     */
29
    private $arguments;
30
31
    /**
32
     * @var ResolveInfo
33
     */
34
    private $info;
35
36
    /**
37
     * @var null|string
38
     */
39
    private $path;
40
41
    /**
42
     * @var FieldDefinition|TypeDefinition
43
     */
44
    private $field;
45
46
    /**
47
     * @var mixed|null
48
     */
49
    private $parent;
50
51
    /**
52
     * Input constructor.
53
     * @param FieldDefinition $field
54
     * @param ResolveInfo $info
55
     * @param array $arguments
56
     * @param mixed|null $parent
57
     */
58
    public function __construct(FieldDefinition $field, ResolveInfo $info, array $arguments = [], $parent = null)
59
    {
60
        $this->info      = $info;
61
        $this->field     = $field;
62
        $this->arguments = $this->resolveArguments($field, $arguments);
63
        $this->parent    = $parent;
64
    }
65
66
    /**
67
     * @return mixed|null
68
     */
69
    public function getParentValue()
70
    {
71
        return $this->parent;
72
    }
73
74
    /**
75
     * @param HasArguments $reflection
76
     * @param array $input
77
     * @return array
78
     * @throws \InvalidArgumentException
79
     */
80
    private function resolveArguments(HasArguments $reflection, array $input = []): array
81
    {
82
        $result = [];
83
84
        /** @var ArgumentDefinition $default */
85
        foreach ($reflection->getArguments() as $default) {
86
            $name = $default->getName();
87
88
            if (
89
                ! \array_key_exists($name, $input) && // Empty argument
90
                ! $default->hasDefaultValue() &&      // And has no default value
91
                $default->isNonNull()                 // And required
92
            ) {
93
                $message = \sprintf('Argument %s required for field %s',
94
                    $name,
95
                    $default->getParent()->getName()
96
                );
97
                throw new \InvalidArgumentException($message);
98
            }
99
100
            if (\array_key_exists($name, $input)) {
101
                $result[$name] = $input[$name];
102
            } elseif ($default->hasDefaultValue()) {
103
                $result[$name] = $default->getDefaultValue();
104
            }
105
        }
106
107
        return $result;
108
    }
109
110
    /**
111
     * @return FieldDefinition
112
     */
113
    public function getFieldDefinition(): FieldDefinition
114
    {
115
        return $this->field;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getQueryType(): string
122
    {
123
        return $this->info->operation->operation;
124
    }
125
126
    /**
127
     * @return ResolveInfo
128
     */
129
    public function getResolveInfo(): ResolveInfo
130
    {
131
        return $this->info;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function all(): array
138
    {
139
        return $this->arguments;
140
    }
141
142
    /**
143
     * @param string $argument
144
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
145
     * @return mixed|null
146
     */
147
    public function get(string $argument, $default = null)
148
    {
149
        return $this->arguments[$argument] ?? $default;
150
    }
151
152
    /**
153
     * @param string $argument
154
     * @return bool
155
     */
156
    public function has(string $argument): bool
157
    {
158
        return \array_key_exists($argument, $this->arguments);
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getPath(): string
165
    {
166
        if ($this->path === null) {
167
            $path                    = $this->info->path;
168
            $path[\count($path) - 1] = $this->getFieldName();
169
170
            // Remove array indexes
171
            $path = \array_filter($path, '\\is_string');
172
173
            // Remove empty values
174
            $path = \array_filter($path, '\\trim');
175
176
            $this->path = \implode(self::DEPTH_DELIMITER, $path);
177
        }
178
179
        return $this->path;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getFieldName(): string
186
    {
187
        return $this->info->fieldName;
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function __debugInfo(): array
194
    {
195
        return [
196
            'path'      => $this->path,
197
            'arguments' => $this->arguments,
198
        ];
199
    }
200
201
    /**
202
     * @return bool
203
     * @throws \LogicException
204
     */
205
    public function hasAlias(): bool
206
    {
207
        return $this->getAlias() !== $this->getFieldName();
208
    }
209
210
    /**
211
     * @return string
212
     * @throws \LogicException
213
     */
214
    public function getAlias(): string
215
    {
216
        return $this->info->path[\count($this->info->path) - 1];
217
    }
218
}
219