• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 9838795518

08 Jul 2024 11:33AM UTC coverage: 64.239% (+0.07%) from 64.173%
9838795518

push

github

web-flow
Merge pull request #6459 from soyuka/main

Merge 3.4

154 of 179 new or added lines in 24 files covered. (86.03%)

36 existing lines in 12 files now uncovered.

11326 of 17631 relevant lines covered (64.24%)

68.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ParameterValidator/Validator/ArrayItems.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\ParameterValidator\Validator;
15

16
/**
17
 * @deprecated use Parameter constraint instead
18
 */
19
final class ArrayItems implements ValidatorInterface
20
{
21
    use CheckFilterDeprecationsTrait;
22

23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function validate(string $name, array $filterDescription, array $queryParameters): array
27
    {
UNCOV
28
        if (!\array_key_exists($name, $queryParameters)) {
×
UNCOV
29
            return [];
×
30
        }
31

32
        $this->checkFilterDeprecations($filterDescription);
×
33

34
        $maxItems = $filterDescription['openapi']['maxItems'] ?? $filterDescription['swagger']['maxItems'] ?? null;
×
35
        $minItems = $filterDescription['openapi']['minItems'] ?? $filterDescription['swagger']['minItems'] ?? null;
×
36
        $uniqueItems = $filterDescription['openapi']['uniqueItems'] ?? $filterDescription['swagger']['uniqueItems'] ?? false;
×
37

38
        $errorList = [];
×
39

40
        $value = $this->getValue($name, $filterDescription, $queryParameters);
×
41
        $nbItems = \count($value);
×
42

43
        if (null !== $maxItems && $nbItems > $maxItems) {
×
44
            $errorList[] = sprintf('Query parameter "%s" must contain less than %d values', $name, $maxItems);
×
45
        }
46

47
        if (null !== $minItems && $nbItems < $minItems) {
×
48
            $errorList[] = sprintf('Query parameter "%s" must contain more than %d values', $name, $minItems);
×
49
        }
50

51
        if (true === $uniqueItems && $nbItems > \count(array_unique($value))) {
×
52
            $errorList[] = sprintf('Query parameter "%s" must contain unique values', $name);
×
53
        }
54

55
        return $errorList;
×
56
    }
57

58
    private function getValue(string $name, array $filterDescription, array $queryParameters): array
59
    {
60
        $value = $queryParameters[$name] ?? null;
×
61

62
        if (empty($value) && '0' !== $value) {
×
63
            return [];
×
64
        }
65

66
        if (\is_array($value)) {
×
67
            return $value;
×
68
        }
69

70
        $collectionFormat = $filterDescription['openapi']['collectionFormat'] ?? $filterDescription['swagger']['collectionFormat'] ?? 'csv';
×
71

72
        return explode(self::getSeparator($collectionFormat), (string) $value) ?: []; // @phpstan-ignore-line
×
73
    }
74

75
    /**
76
     * @return non-empty-string
77
     */
78
    private static function getSeparator(string $collectionFormat): string
79
    {
80
        return match ($collectionFormat) {
×
81
            'csv' => ',',
×
82
            'ssv' => ' ',
×
83
            'tsv' => '\t',
×
84
            'pipes' => '|',
×
85
            default => throw new \InvalidArgumentException(sprintf('Unknown collection format %s', $collectionFormat)),
×
86
        };
×
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc