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

api-platform / core / 10944992322

19 Sep 2024 04:21PM UTC coverage: 7.678% (+0.2%) from 7.518%
10944992322

push

github

soyuka
doc: changelog 4.0.1

12681 of 165161 relevant lines covered (7.68%)

16.3 hits per line

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

83.87
/src/Metadata/Parameters.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\Metadata;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17

18
/**
19
 * A parameter dictionnary.
20
 *
21
 * @implements \IteratorAggregate<string, Parameter>
22
 */
23
final class Parameters implements \IteratorAggregate, \Countable
24
{
25
    private array $parameters = [];
26

27
    /**
28
     * @param array<int|string, Parameter> $parameters
29
     */
30
    public function __construct(array $parameters = [])
31
    {
32
        foreach ($parameters as $parameterName => $parameter) {
37✔
33
            if ($parameter->getKey()) {
2✔
34
                $parameterName = $parameter->getKey();
2✔
35
            }
36

37
            $this->parameters[] = [$parameterName, $parameter];
2✔
38
        }
39

40
        $this->sort();
37✔
41
    }
42

43
    /**
44
     * @return \ArrayIterator<string, Parameter>
45
     */
46
    public function getIterator(): \Traversable
47
    {
48
        return (function (): \Generator {
824✔
49
            foreach ($this->parameters as [$parameterName, $parameter]) {
824✔
50
                yield $parameterName => $parameter;
804✔
51
            }
52
        })();
824✔
53
    }
54

55
    public function add(string $key, Parameter $value): self
56
    {
57
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
431✔
58
            if ($parameterName === $key && $value::class === $parameter::class) {
430✔
59
                $this->parameters[$i] = [$key, $value];
428✔
60

61
                return $this;
428✔
62
            }
63
        }
64

65
        $this->parameters[] = [$key, $value];
12✔
66

67
        return $this;
12✔
68
    }
69

70
    /**
71
     * @param class-string $parameterClass
72
     */
73
    public function remove(string $key, string $parameterClass): self
74
    {
75
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
2✔
76
            if ($parameterName === $key && $parameterClass === $parameter::class) {
2✔
77
                unset($this->parameters[$i]);
2✔
78

79
                return $this;
2✔
80
            }
81
        }
82

83
        throw new RuntimeException(\sprintf('Could not remove parameter "%s".', $key));
×
84
    }
85

86
    /**
87
     * @param class-string $parameterClass
88
     */
89
    public function get(string $key, string $parameterClass): ?Parameter
90
    {
91
        foreach ($this->parameters as [$parameterName, $parameter]) {
×
92
            if ($parameterName === $key && $parameterClass === $parameter::class) {
×
93
                return $parameter;
×
94
            }
95
        }
96

97
        return null;
×
98
    }
99

100
    /**
101
     * @param class-string $parameterClass
102
     */
103
    public function has(string $key, string $parameterClass): bool
104
    {
105
        foreach ($this->parameters as [$parameterName, $parameter]) {
2✔
106
            if ($parameterName === $key && $parameterClass === $parameter::class) {
2✔
107
                return true;
2✔
108
            }
109
        }
110

111
        return false;
2✔
112
    }
113

114
    public function count(): int
115
    {
116
        return \count($this->parameters);
32✔
117
    }
118

119
    public function sort(): self
120
    {
121
        usort($this->parameters, fn ($a, $b): int|float => $b[1]->getPriority() - $a[1]->getPriority());
37✔
122

123
        return $this;
37✔
124
    }
125
}
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

© 2026 Coveralls, Inc