|
22 | 22 |
|
23 | 23 | use DCarbone\PHPConsulAPI\AbstractModel; |
24 | 24 | use DCarbone\PHPConsulAPI\Consul; |
25 | | -use DCarbone\PHPConsulAPI\FakeSlice; |
26 | 25 |
|
27 | | -class HealthChecks extends FakeSlice |
| 26 | +/** |
| 27 | + * @implements \ArrayAccess<int, HealthCheck> |
| 28 | + * @implements \IteratorAggregate<int, HealthCheck> |
| 29 | + */ |
| 30 | +class HealthChecks extends AbstractModel implements \IteratorAggregate, \Countable, \ArrayAccess |
28 | 31 | { |
29 | | - protected string $containedClass = HealthCheck::class; |
| 32 | + /** @var \DCarbone\PHPConsulAPI\Health\HealthCheck[] */ |
| 33 | + protected array $Checks = []; |
| 34 | + |
| 35 | + public function __construct(HealthCheck ...$Checks) |
| 36 | + { |
| 37 | + $this->Checks = $Checks; |
| 38 | + } |
30 | 39 |
|
31 | 40 | public function AggregatedStatus(): string |
32 | 41 | { |
33 | 42 | $passing = $warning = $critical = $maintenance = false; |
34 | | - foreach ($this as $check) { |
35 | | - if (Consul::NodeMaint === $check->CheckID || 0 === strpos($check->CheckID, Consul::ServiceMaintPrefix)) { |
| 43 | + foreach ($this->Checks as $check) { |
| 44 | + if (Consul::NodeMaint === $check->CheckID || str_starts_with($check->CheckID, Consul::ServiceMaintPrefix)) { |
36 | 45 | // TODO: Maybe just return maintenance right now...? |
37 | 46 | $maintenance = true; |
38 | 47 | continue; |
@@ -68,8 +77,66 @@ public function AggregatedStatus(): string |
68 | 77 | return Consul::HealthPassing; |
69 | 78 | } |
70 | 79 |
|
71 | | - protected function newChild(array $data): AbstractModel |
| 80 | + public function getIterator(): \Traversable |
| 81 | + { |
| 82 | + return new \ArrayIterator($this->Checks); |
| 83 | + } |
| 84 | + |
| 85 | + public function count(): int |
| 86 | + { |
| 87 | + return count($this->Checks); |
| 88 | + } |
| 89 | + |
| 90 | + public function offsetExists($offset): bool |
| 91 | + { |
| 92 | + return is_int($offset) && isset($this->Checks[$offset]); |
| 93 | + } |
| 94 | + |
| 95 | + public function offsetGet($offset): null|HealthCheck |
| 96 | + { |
| 97 | + if (!isset($this->Checks[$offset])) { |
| 98 | + throw new \OutOfRangeException("Offset $offset does not exist"); |
| 99 | + } |
| 100 | + return $this->Checks[$offset]; |
| 101 | + } |
| 102 | + |
| 103 | + public function offsetSet($offset, $value): void |
| 104 | + { |
| 105 | + if (!$value instanceof HealthCheck) { |
| 106 | + throw new \InvalidArgumentException(sprintf("Value must be an instance of %s", HealthCheck::class)); |
| 107 | + } |
| 108 | + if (null === $offset) { |
| 109 | + $this->Checks[] = $value; |
| 110 | + } else { |
| 111 | + if (!is_int($offset)) { |
| 112 | + throw new \InvalidArgumentException('Offset must be an integer'); |
| 113 | + } |
| 114 | + $this->Checks[$offset] = $value; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public function offsetUnset($offset): void |
| 119 | + { |
| 120 | + unset($this->Checks[$offset]); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param array<\stdClass> $decoded |
| 125 | + */ |
| 126 | + public static function jsonUnserialize(array $decoded, null|self $into = null): static |
| 127 | + { |
| 128 | + $n = $into ?? new self(); |
| 129 | + foreach ($decoded as $d) { |
| 130 | + $n->Checks[] = HealthCheck::jsonUnserialize($d); |
| 131 | + } |
| 132 | + return $n; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return \DCarbone\PHPConsulAPI\Health\HealthCheck[] |
| 137 | + */ |
| 138 | + public function jsonSerialize(): array |
72 | 139 | { |
73 | | - return new HealthCheck($data); |
| 140 | + return $this->Checks; |
74 | 141 | } |
75 | 142 | } |
0 commit comments