limoncello-php-dist /
flute
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare (strict_types = 1); |
||
| 2 | |||
| 3 | namespace Limoncello\Flute\Http\Query; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Copyright 2015-2019 [email protected] |
||
| 7 | * |
||
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
| 9 | * you may not use this file except in compliance with the License. |
||
| 10 | * You may obtain a copy of the License at |
||
| 11 | * |
||
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 13 | * |
||
| 14 | * Unless required by applicable law or agreed to in writing, software |
||
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 17 | * See the License for the specific language governing permissions and |
||
| 18 | * limitations under the License. |
||
| 19 | */ |
||
| 20 | |||
| 21 | use Generator; |
||
| 22 | use Limoncello\Flute\Contracts\Api\CrudInterface; |
||
| 23 | use Limoncello\Flute\Contracts\Http\Query\AttributeInterface; |
||
| 24 | use Limoncello\Flute\Contracts\Http\Query\FilterParameterInterface; |
||
| 25 | use Limoncello\Flute\Contracts\Http\Query\ParametersMapperInterface; |
||
| 26 | use Limoncello\Flute\Contracts\Http\Query\RelationshipInterface; |
||
| 27 | use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface; |
||
| 28 | use Limoncello\Flute\Contracts\Schema\SchemaInterface; |
||
| 29 | use Limoncello\Flute\Contracts\Validation\JsonApiQueryParserInterface; |
||
| 30 | use Limoncello\Flute\Exceptions\InvalidQueryParametersException; |
||
| 31 | use Limoncello\Flute\Exceptions\LogicException; |
||
| 32 | use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface; |
||
| 33 | use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
||
| 34 | use Neomerx\JsonApi\Schema\Error; |
||
| 35 | use function array_key_exists; |
||
| 36 | use function assert; |
||
| 37 | use function count; |
||
| 38 | use function explode; |
||
| 39 | use function get_class; |
||
| 40 | use function is_array; |
||
| 41 | use function is_bool; |
||
| 42 | use function is_string; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @package Limoncello\Flute |
||
| 46 | * |
||
| 47 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
||
| 48 | * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
||
| 49 | */ |
||
| 50 | class ParametersMapper implements ParametersMapperInterface |
||
| 51 | { |
||
| 52 | /** Message */ |
||
| 53 | public const MSG_ERR_INVALID_OPERATION = 'Invalid Operation.'; |
||
| 54 | |||
| 55 | /** Message */ |
||
| 56 | public const MSG_ERR_INVALID_FIELD = 'Invalid field.'; |
||
| 57 | |||
| 58 | /** Message */ |
||
| 59 | public const MSG_ERR_ROOT_SCHEMA_IS_NOT_SET = 'Root Schema is not set.'; |
||
| 60 | |||
| 61 | /** Message */ |
||
| 62 | private const MSG_PARAM_INCLUDE = BaseQueryParserInterface::PARAM_INCLUDE; |
||
| 63 | |||
| 64 | /** Message */ |
||
| 65 | private const MSG_PARAM_FILTER = BaseQueryParserInterface::PARAM_FILTER; |
||
| 66 | |||
| 67 | /** internal constant */ |
||
| 68 | private const REL_FILTER_INDEX = 0; |
||
| 69 | |||
| 70 | /** internal constant */ |
||
| 71 | private const REL_SORT_INDEX = 1; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var SchemaInterface |
||
| 75 | */ |
||
| 76 | private $rootSchema; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var JsonSchemasInterface |
||
| 80 | */ |
||
| 81 | private $jsonSchemas; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array|null |
||
| 85 | */ |
||
| 86 | private $messages; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var iterable |
||
| 90 | */ |
||
| 91 | private $filters; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var iterable |
||
| 95 | */ |
||
| 96 | private $sorts; |
||
| 97 | |||
| 98 | /** |
||
| 99 | 25 | * @var iterable |
|
| 100 | */ |
||
| 101 | 25 | private $includes; |
|
| 102 | 25 | ||
| 103 | /** |
||
| 104 | 25 | * @param JsonSchemasInterface $jsonSchemas |
|
| 105 | * @param array|null $messages |
||
| 106 | */ |
||
| 107 | public function __construct(JsonSchemasInterface $jsonSchemas, array $messages = null) |
||
| 108 | { |
||
| 109 | $this->jsonSchemas = $jsonSchemas; |
||
| 110 | 23 | $this->messages = $messages; |
|
| 111 | |||
| 112 | 23 | $this->withoutFilters()->withoutSorts()->withoutIncludes(); |
|
| 113 | } |
||
| 114 | 23 | ||
| 115 | /** |
||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
| 118 | public function selectRootSchemaByResourceType(string $resourceType): ParametersMapperInterface |
||
| 119 | { |
||
| 120 | 25 | $this->rootSchema = $this->getJsonSchemas()->getSchemaByResourceType($resourceType); |
|
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | public function withFilters(iterable $filters): ParametersMapperInterface |
||
| 129 | { |
||
| 130 | // Sample format |
||
| 131 | // [ |
||
| 132 | // 'attribute' => [ |
||
| 133 | // 'op1' => [arg1], |
||
| 134 | // 'op2' => [arg1, arg1], |
||
| 135 | // ], |
||
| 136 | // 'relationship' => [ |
||
| 137 | // 'op1' => [arg1], |
||
| 138 | 25 | // 'op2' => [arg1, arg1], |
|
| 139 | // ], |
||
| 140 | 25 | // 'relationship.attribute' => [ |
|
| 141 | // 'op1' => [arg1], |
||
| 142 | // 'op2' => [arg1, arg1], |
||
| 143 | // ], |
||
| 144 | // ]; |
||
| 145 | |||
| 146 | 25 | $this->filters = $filters; |
|
|
0 ignored issues
–
show
|
|||
| 147 | |||
| 148 | 25 | return $this; |
|
| 149 | } |
||
| 150 | 25 | ||
| 151 | /** |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function withoutFilters(): self |
||
| 155 | { |
||
| 156 | 25 | $this->withFilters([]); |
|
|
0 ignored issues
–
show
array() is of type array, but the function expects a object<Limoncello\Flute\...ts\Http\Query\iterable>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | public function withSorts(iterable $sorts): ParametersMapperInterface |
||
| 165 | 25 | { |
|
| 166 | // Sample format (name => isAsc) |
||
| 167 | 25 | // [ |
|
| 168 | // 'attribute' => true, |
||
| 169 | // 'relationship' => false, |
||
| 170 | // 'relationship.attribute' => true, |
||
| 171 | // ]; |
||
| 172 | |||
| 173 | 25 | $this->sorts = $sorts; |
|
|
0 ignored issues
–
show
It seems like
$sorts of type object<Limoncello\Flute\...ts\Http\Query\iterable> is incompatible with the declared type object<Limoncello\Flute\Http\Query\iterable> of property $sorts.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 174 | |||
| 175 | 25 | return $this; |
|
| 176 | } |
||
| 177 | 25 | ||
| 178 | /** |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | public function withoutSorts(): self |
||
| 182 | { |
||
| 183 | 25 | $this->withSorts([]); |
|
|
0 ignored issues
–
show
array() is of type array, but the function expects a object<Limoncello\Flute\...ts\Http\Query\iterable>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 184 | |||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | 25 | public function withIncludes(iterable $includes): ParametersMapperInterface |
|
| 192 | { |
||
| 193 | 25 | // Sample format |
|
| 194 | // [ |
||
| 195 | // ['relationship'], |
||
| 196 | // ['relationship', 'next_relationship'], |
||
| 197 | // ]; |
||
| 198 | |||
| 199 | 25 | $this->includes = $includes; |
|
|
0 ignored issues
–
show
It seems like
$includes of type object<Limoncello\Flute\...ts\Http\Query\iterable> is incompatible with the declared type object<Limoncello\Flute\Http\Query\iterable> of property $includes.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 200 | |||
| 201 | 25 | return $this; |
|
| 202 | } |
||
| 203 | 25 | ||
| 204 | /** |
||
| 205 | * @return self |
||
| 206 | */ |
||
| 207 | public function withoutIncludes(): self |
||
| 208 | { |
||
| 209 | 18 | $this->withIncludes([]); |
|
|
0 ignored issues
–
show
array() is of type array, but the function expects a object<Limoncello\Flute\...ts\Http\Query\iterable>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 210 | |||
| 211 | 18 | return $this; |
|
| 212 | 10 | } |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | 10 | */ |
|
| 217 | public function getMappedFilters(): iterable |
||
| 218 | 9 | { |
|
| 219 | 9 | foreach ($this->getFilters() as $field => $operationsAndArgs) { |
|
| 220 | 9 | assert(is_string($field)); |
|
| 221 | 9 | ||
| 222 | /** @var RelationshipInterface|null $relationship */ |
||
| 223 | /** @var AttributeInterface $attribute */ |
||
| 224 | 9 | list ($relationship, $attribute) = $this->mapToRelationshipAndAttribute($field); |
|
| 225 | |||
| 226 | $filter = new FilterParameter( |
||
| 227 | $attribute, |
||
| 228 | $this->parseOperationsAndArguments(static::MSG_PARAM_FILTER, $operationsAndArgs), |
||
|
0 ignored issues
–
show
$this->parseOperationsAn...ER, $operationsAndArgs) is of type object<Generator>, but the function expects a object<Limoncello\Flute\Http\Query\iterable>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 229 | $relationship |
||
| 230 | ); |
||
| 231 | 15 | ||
| 232 | yield $filter; |
||
| 233 | 15 | } |
|
| 234 | 5 | } |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @inheritdoc |
||
| 238 | 5 | */ |
|
| 239 | public function getMappedSorts(): iterable |
||
| 240 | 5 | { |
|
| 241 | foreach ($this->getSorts() as $field => $isAsc) { |
||
| 242 | 5 | assert(is_string($field) === true && empty($field) === false && is_bool($isAsc) === true); |
|
| 243 | |||
| 244 | /** @var RelationshipInterface|null $relationship */ |
||
| 245 | /** @var AttributeInterface $attribute */ |
||
| 246 | list ($relationship, $attribute) = $this->mapToRelationshipAndAttribute($field); |
||
| 247 | |||
| 248 | $sort = new SortParameter($attribute, $isAsc, $relationship); |
||
| 249 | 17 | ||
| 250 | yield $sort; |
||
| 251 | 17 | } |
|
| 252 | } |
||
| 253 | 5 | ||
| 254 | 5 | /** |
|
| 255 | 5 | * @inheritdoc |
|
| 256 | 5 | */ |
|
| 257 | 4 | public function getMappedIncludes(): iterable |
|
| 258 | 4 | { |
|
| 259 | $fromSchema = $this->getRootSchema(); |
||
| 260 | 4 | $getMappedRelLinks = function (iterable $links) use ($fromSchema) : iterable { |
|
| 261 | foreach ($links as $link) { |
||
| 262 | 4 | assert(is_string($link)); |
|
| 263 | 4 | $fromSchemaClass = get_class($fromSchema); |
|
| 264 | if ($this->getJsonSchemas()->hasRelationshipSchema($fromSchemaClass, $link)) { |
||
| 265 | $toSchema = $this->getJsonSchemas()->getRelationshipSchema($fromSchemaClass, $link); |
||
| 266 | 1 | $relationship = new Relationship($link, $fromSchema, $toSchema); |
|
| 267 | 1 | ||
| 268 | yield $relationship; |
||
| 269 | 16 | ||
| 270 | $fromSchema = $toSchema; |
||
|
0 ignored issues
–
show
Consider using a different name than the imported variable
$fromSchema, or did you forget to import by reference?
It seems like you are assigning to a variable which was imported through a For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope. Change not visible in outer-scope$x = 1;
$callable = function() use ($x) {
$x = 2; // Not visible in outer scope. If you would like this, how
// about using a different variable name than $x?
};
$callable();
var_dump($x); // integer(1)
Change visible in outer-scope$x = 1;
$callable = function() use (&$x) {
$x = 2;
};
$callable();
var_dump($x); // integer(2)
Loading history...
|
|||
| 271 | 16 | continue; |
|
| 272 | 5 | } |
|
| 273 | |||
| 274 | $error = $this->createQueryError(static::MSG_PARAM_INCLUDE, static::MSG_ERR_INVALID_FIELD); |
||
| 275 | throw new InvalidQueryParametersException($error); |
||
| 276 | } |
||
| 277 | }; |
||
| 278 | |||
| 279 | foreach ($this->getIncludes() as $links) { |
||
| 280 | yield $getMappedRelLinks($links); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | 15 | ||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | * |
||
| 287 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 288 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 289 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
| 290 | 15 | */ |
|
| 291 | public function applyQueryParameters( |
||
| 292 | JsonApiQueryParserInterface $parser, |
||
| 293 | CrudInterface $api |
||
| 294 | ): CrudInterface { |
||
| 295 | // |
||
| 296 | // Paging |
||
| 297 | // |
||
| 298 | $api->withPaging($parser->getPagingOffset(), $parser->getPagingLimit()); |
||
| 299 | |||
| 300 | // |
||
| 301 | // Includes |
||
| 302 | // |
||
| 303 | 3 | ||
| 304 | 3 | // the two functions below compose a 2D array of relationship names in a form of iterable |
|
| 305 | 3 | // [ |
|
| 306 | // ['rel1_name1', 'rel1_name2', 'rel1_name3', ], |
||
| 307 | 15 | // ['rel2_name1', ], |
|
| 308 | 15 | // ['rel3_name1', 'rel3_name2', 'rel3_name3', ], |
|
| 309 | // ] |
||
| 310 | 14 | $includeAsModelNames = function (iterable $relationships): iterable { |
|
| 311 | 3 | foreach ($relationships as $relationship) { |
|
| 312 | assert($relationship instanceof RelationshipInterface); |
||
| 313 | 15 | yield $relationship->getNameInModel(); |
|
| 314 | } |
||
| 315 | }; |
||
| 316 | $mappedIncludes = $this->getMappedIncludes(); |
||
| 317 | $getIncludes = function () use ($mappedIncludes, $includeAsModelNames) : iterable { |
||
| 318 | foreach ($mappedIncludes as $relationships) { |
||
| 319 | 15 | yield $includeAsModelNames($relationships); |
|
| 320 | } |
||
| 321 | }; |
||
| 322 | 15 | ||
| 323 | 14 | // |
|
| 324 | 14 | // Filters and Sorts |
|
| 325 | // |
||
| 326 | 14 | ||
| 327 | 14 | $parser->areFiltersWithAnd() === true ? $api->combineWithAnd() : $api->combineWithOr(); |
|
| 328 | |||
| 329 | $this |
||
| 330 | ->withFilters($parser->getFilters()) |
||
| 331 | 14 | ->withSorts($parser->getSorts()) |
|
| 332 | ->withIncludes($parser->getIncludes()); |
||
| 333 | 14 | ||
| 334 | $attributeFilters = []; |
||
| 335 | 6 | $attributeSorts = []; |
|
| 336 | 6 | ||
| 337 | 3 | // As relationship filters and sorts should be applied together (in one SQL JOIN) |
|
| 338 | // we have to iterate through all filters and merge related to the same relationship. |
||
| 339 | 5 | $relFiltersAndSorts = []; |
|
| 340 | |||
| 341 | 5 | foreach ($this->getMappedFilters() as $filter) { |
|
| 342 | 6 | /** @var FilterParameterInterface $filter */ |
|
| 343 | $attributeName = $filter->getAttribute()->getNameInModel(); |
||
| 344 | if ($filter->getRelationship() === null) { |
||
| 345 | 14 | $attributeFilters[$attributeName] = $filter->getOperationsWithArguments(); |
|
| 346 | } else { |
||
| 347 | 4 | $relationshipName = $filter->getRelationship()->getNameInModel(); |
|
|
0 ignored issues
–
show
|
|||
| 348 | 4 | ||
| 349 | 3 | $relFiltersAndSorts[$relationshipName][self::REL_FILTER_INDEX][$attributeName] = |
|
| 350 | $filter->getOperationsWithArguments(); |
||
| 351 | 1 | } |
|
| 352 | } |
||
| 353 | 4 | foreach ($this->getMappedSorts() as $sort) { |
|
| 354 | /** @var SortParameter $sort */ |
||
| 355 | $attributeName = $sort->getAttribute()->getNameInModel(); |
||
| 356 | if ($sort->getRelationship() === null) { |
||
| 357 | 14 | $attributeSorts[$attributeName] = $sort->isAsc(); |
|
| 358 | 14 | } else { |
|
| 359 | 14 | $relationshipName = $sort->getRelationship()->getNameInModel(); |
|
|
0 ignored issues
–
show
|
|||
| 360 | |||
| 361 | 14 | $relFiltersAndSorts[$relationshipName][self::REL_SORT_INDEX][$attributeName] = $sort->isAsc(); |
|
| 362 | 5 | } |
|
| 363 | 5 | } |
|
| 364 | |||
| 365 | 5 | $api->withFilters($attributeFilters) |
|
|
0 ignored issues
–
show
$attributeFilters is of type array, but the function expects a object<Limoncello\Flute\Contracts\Api\iterable>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 366 | 5 | ->withSorts($attributeSorts) |
|
| 367 | ->withIncludes($getIncludes()); |
||
| 368 | |||
| 369 | foreach ($relFiltersAndSorts as $relationshipName => $filtersAndSorts) { |
||
| 370 | 14 | if (array_key_exists(self::REL_FILTER_INDEX, $filtersAndSorts) === true) { |
|
| 371 | $api->withRelationshipFilters($relationshipName, $filtersAndSorts[self::REL_FILTER_INDEX]); |
||
| 372 | } |
||
| 373 | if (array_key_exists(self::REL_SORT_INDEX, $filtersAndSorts) === true) { |
||
| 374 | $api->withRelationshipSorts($relationshipName, $filtersAndSorts[self::REL_SORT_INDEX]); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 14 | return $api; |
|
| 379 | } |
||
| 380 | 14 | ||
| 381 | 14 | /** |
|
| 382 | 10 | * @param string $field |
|
| 383 | 10 | * |
|
| 384 | 10 | * @return array |
|
| 385 | */ |
||
| 386 | 10 | private function mapToRelationshipAndAttribute(string $field): array |
|
| 387 | 8 | { |
|
| 388 | 5 | $rootSchema = $this->getRootSchema(); |
|
| 389 | 5 | if ($rootSchema->hasAttributeMapping($field) === true) { |
|
| 390 | 5 | $relationship = null; |
|
| 391 | 5 | $schema = $rootSchema; |
|
| 392 | $attribute = new Attribute($field, $schema); |
||
| 393 | 5 | ||
| 394 | 5 | return [$relationship, $attribute]; |
|
| 395 | } elseif ($rootSchema->hasRelationshipMapping($field) === true) { |
||
| 396 | $fromSchema = $rootSchema; |
||
| 397 | 4 | $toSchema = $this->getJsonSchemas()->getRelationshipSchema(get_class($fromSchema), $field); |
|
| 398 | 4 | $relationship = new Relationship($field, $fromSchema, $toSchema); |
|
| 399 | $attribute = new Attribute($toSchema::RESOURCE_ID, $toSchema); |
||
| 400 | 4 | ||
| 401 | 4 | return [$relationship, $attribute]; |
|
| 402 | 4 | } elseif (count($mightBeRelAndAttr = explode('.', $field, 2)) === 2) { |
|
| 403 | 4 | // Last chance. It could be a dot ('.') separated relationship with an attribute. |
|
| 404 | 4 | ||
| 405 | 4 | $mightBeRel = $mightBeRelAndAttr[0]; |
|
| 406 | $mightBeAttr = $mightBeRelAndAttr[1]; |
||
| 407 | 4 | ||
| 408 | $fromSchema = $rootSchema; |
||
| 409 | if ($fromSchema->hasRelationshipMapping($mightBeRel)) { |
||
| 410 | $toSchema = $this->getJsonSchemas()->getRelationshipSchema(get_class($fromSchema), $mightBeRel); |
||
| 411 | if ($toSchema::hasAttributeMapping($mightBeAttr) === true) { |
||
| 412 | 1 | $relationship = new Relationship($mightBeRel, $fromSchema, $toSchema); |
|
| 413 | 1 | $attribute = new Attribute($mightBeAttr, $toSchema); |
|
| 414 | |||
| 415 | return [$relationship, $attribute]; |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | $error = $this->createQueryError($field, static::MSG_ERR_INVALID_FIELD); |
||
| 421 | throw new InvalidQueryParametersException($error); |
||
| 422 | } |
||
| 423 | |||
| 424 | 9 | /** |
|
| 425 | * @param string $parameterName |
||
| 426 | * @param iterable $value |
||
| 427 | 9 | * |
|
| 428 | 9 | * @return iterable |
|
|
0 ignored issues
–
show
|
|||
| 429 | * |
||
| 430 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 431 | 9 | */ |
|
| 432 | 9 | private function parseOperationsAndArguments(string $parameterName, iterable $value): iterable |
|
| 433 | 9 | { |
|
| 434 | 2 | // in this case we interpret it as an [operation => [arg1, arg2]] |
|
| 435 | 2 | foreach ($value as $operationName => $arguments) { |
|
| 436 | 9 | assert(is_array($arguments) || $arguments instanceof Generator); |
|
| 437 | 9 | ||
| 438 | 9 | switch ($operationName) { |
|
| 439 | 1 | case '=': |
|
| 440 | 1 | case 'eq': |
|
| 441 | 9 | case 'equals': |
|
| 442 | 9 | $operation = FilterParameterInterface::OPERATION_EQUALS; |
|
| 443 | 9 | break; |
|
| 444 | 2 | case '!=': |
|
| 445 | 2 | case 'neq': |
|
| 446 | 9 | case 'not-equals': |
|
| 447 | 9 | $operation = FilterParameterInterface::OPERATION_NOT_EQUALS; |
|
| 448 | 9 | break; |
|
| 449 | 1 | case '<': |
|
| 450 | 1 | case 'lt': |
|
| 451 | 9 | case 'less-than': |
|
| 452 | 9 | $operation = FilterParameterInterface::OPERATION_LESS_THAN; |
|
| 453 | 8 | break; |
|
| 454 | 2 | case '<=': |
|
| 455 | 2 | case 'lte': |
|
| 456 | 8 | case 'less-or-equals': |
|
| 457 | 8 | $operation = FilterParameterInterface::OPERATION_LESS_OR_EQUALS; |
|
| 458 | 8 | break; |
|
| 459 | 1 | case '>': |
|
| 460 | 1 | case 'gt': |
|
| 461 | 8 | case 'greater-than': |
|
| 462 | 6 | $operation = FilterParameterInterface::OPERATION_GREATER_THAN; |
|
| 463 | 6 | break; |
|
| 464 | 6 | case '>=': |
|
| 465 | 2 | case 'gte': |
|
| 466 | 2 | case 'greater-or-equals': |
|
| 467 | 6 | $operation = FilterParameterInterface::OPERATION_GREATER_OR_EQUALS; |
|
| 468 | 5 | break; |
|
| 469 | 5 | case 'like': |
|
| 470 | 2 | $operation = FilterParameterInterface::OPERATION_LIKE; |
|
| 471 | 1 | break; |
|
| 472 | 1 | case 'not-like': |
|
| 473 | 2 | $operation = FilterParameterInterface::OPERATION_NOT_LIKE; |
|
| 474 | 1 | break; |
|
| 475 | 1 | case 'in': |
|
| 476 | 1 | $operation = FilterParameterInterface::OPERATION_IN; |
|
| 477 | 2 | break; |
|
| 478 | 1 | case 'not-in': |
|
| 479 | 1 | $operation = FilterParameterInterface::OPERATION_NOT_IN; |
|
| 480 | 1 | break; |
|
| 481 | case 'is-null': |
||
| 482 | 1 | $operation = FilterParameterInterface::OPERATION_IS_NULL; |
|
| 483 | 1 | $arguments = []; |
|
| 484 | break; |
||
| 485 | case 'not-null': |
||
| 486 | 8 | $operation = FilterParameterInterface::OPERATION_IS_NOT_NULL; |
|
| 487 | $arguments = []; |
||
| 488 | break; |
||
| 489 | default: |
||
| 490 | $error = $this->createQueryError($parameterName, static::MSG_ERR_INVALID_OPERATION); |
||
| 491 | throw new InvalidQueryParametersException($error); |
||
| 492 | } |
||
| 493 | 22 | ||
| 494 | yield $operation => $arguments; |
||
| 495 | 22 | } |
|
| 496 | 1 | } |
|
| 497 | |||
| 498 | /** |
||
| 499 | 21 | * @return SchemaInterface |
|
| 500 | */ |
||
| 501 | private function getRootSchema(): SchemaInterface |
||
| 502 | { |
||
| 503 | if ($this->rootSchema === null) { |
||
| 504 | throw new LogicException($this->getMessage(static::MSG_ERR_ROOT_SCHEMA_IS_NOT_SET)); |
||
| 505 | 23 | } |
|
| 506 | |||
| 507 | 23 | return $this->rootSchema; |
|
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return JsonSchemasInterface |
||
| 512 | */ |
||
| 513 | 18 | private function getJsonSchemas(): JsonSchemasInterface |
|
| 514 | { |
||
| 515 | 18 | return $this->jsonSchemas; |
|
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @return iterable |
||
| 520 | */ |
||
| 521 | 15 | private function getFilters(): iterable |
|
| 522 | { |
||
| 523 | 15 | return $this->filters; |
|
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return iterable |
||
| 528 | */ |
||
| 529 | 16 | private function getSorts(): iterable |
|
| 530 | { |
||
| 531 | 16 | return $this->sorts; |
|
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return iterable |
||
| 536 | */ |
||
| 537 | private function getIncludes(): iterable |
||
| 538 | { |
||
| 539 | return $this->includes; |
||
| 540 | 3 | } |
|
| 541 | |||
| 542 | 3 | /** |
|
| 543 | 3 | * @param string $name |
|
| 544 | 3 | * @param string $title |
|
| 545 | * |
||
| 546 | 3 | * @return ErrorInterface |
|
| 547 | */ |
||
| 548 | private function createQueryError(string $name, string $title): ErrorInterface |
||
| 549 | { |
||
| 550 | $title = $this->getMessage($title); |
||
| 551 | $source = [ErrorInterface::SOURCE_PARAMETER => $name]; |
||
| 552 | $error = new Error(null, null, null, null, null, $title, null, $source); |
||
| 553 | |||
| 554 | 4 | return $error; |
|
| 555 | } |
||
| 556 | 4 | ||
| 557 | /** |
||
| 558 | 4 | * @param string $message |
|
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | private function getMessage(string $message): string |
||
| 563 | { |
||
| 564 | $hasTranslation = $this->messages !== null && array_key_exists($message, $this->messages) === false; |
||
| 565 | |||
| 566 | return $hasTranslation === true ? $this->messages[$message] : $message; |
||
| 567 | } |
||
| 568 | } |
||
| 569 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..