-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperations.php
More file actions
104 lines (83 loc) · 2.62 KB
/
Operations.php
File metadata and controls
104 lines (83 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Metadata;
/**
* An Operation dictionnary.
*/
final class Operations implements \IteratorAggregate, \Countable
{
private array $operations = [];
/**
* @param array<string|int, Operation> $operations
*/
public function __construct(array $operations = [])
{
foreach ($operations as $operationName => $operation) {
// When we use an int-indexed array in the constructor, compute priorities
if (\is_int($operationName) && null === $operation->getPriority()) {
$operation = $operation->withPriority($operationName);
$operationName = (string) $operationName;
}
if ($operation->getName()) {
$operationName = $operation->getName();
}
$this->operations[] = [$operationName, $operation];
}
$this->sort();
}
public function getIterator(): \Traversable
{
return (function (): \Generator {
foreach ($this->operations as [$operationName, $operation]) {
yield $operationName => $operation;
}
})();
}
public function add(string $key, Operation $value): self
{
foreach ($this->operations as $i => [$operationName, $operation]) {
if ($operationName === $key) {
$this->operations[$i] = [$key, $value];
return $this;
}
}
$this->operations[] = [$key, $value];
return $this;
}
public function remove(string $key): self
{
foreach ($this->operations as $i => [$operationName, $operation]) {
if ($operationName === $key) {
unset($this->operations[$i]);
return $this;
}
}
throw new \RuntimeException(sprintf('Could not remove operation "%s".', $key));
}
public function has(string $key): bool
{
foreach ($this->operations as $i => [$operationName, $operation]) {
if ($operationName === $key) {
return true;
}
}
return false;
}
public function count(): int
{
return \count($this->operations);
}
public function sort(): self
{
usort($this->operations, fn ($a, $b): int|float => $a[1]->getPriority() - $b[1]->getPriority());
return $this;
}
}