Skip to content

Commit 2727a44

Browse files
committed
more little bits of work
1 parent d9df71e commit 2727a44

10 files changed

Lines changed: 135 additions & 29 deletions

src/AbstractModel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222

2323
abstract class AbstractModel implements \JsonSerializable
2424
{
25+
/** @var array<string,mixed> */
2526
private array $_dyn = [];
2627

27-
public function __set(string $field, $value): void
28+
public function __set(string $field, mixed $value): void
2829
{
2930
$this->_dyn[$field] = $value;
3031
}
3132

32-
public function &__get(string $field)
33+
public function &__get(string $field): mixed
3334
{
3435
if (!array_key_exists($field, $this->_dyn)) {
3536
$this->_dyn[$field] = null;
@@ -42,6 +43,9 @@ public function __unset(string $field): void
4243
unset($this->_dyn[$field]);
4344
}
4445

46+
/**
47+
* @return array<string,mixed>
48+
*/
4549
public function _getDynamicFields(): array
4650
{
4751
return $this->_dyn;

src/Catalog/CatalogService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CatalogService extends AbstractModel
5252
public string $Partition;
5353

5454
/**
55-
* @param array<mixed>|null $data
55+
* @param array<string,mixed>|null $data
5656
* @param array<string> $ServiceTags
5757
*/
5858
public function __construct(

src/Catalog/CompoundServiceName.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public static function jsonUnserialize(\stdClass $decoded, null|self $into = nul
8585
return $n;
8686
}
8787

88-
8988
public function jsonSerialize(): \stdClass
9089
{
9190
$out = new \stdClass();

src/Catalog/Node.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Node extends AbstractModel
3737
public string $PeerName;
3838
public null|Locality $Locality;
3939

40+
/**
41+
* @param array<string,mixed>|null $data
42+
*/
4043
public function __construct(
4144
null|array $data = null, // Deprecated, will be removed.
4245
string $ID = '',
@@ -50,8 +53,7 @@ public function __construct(
5053
string $Partition = '',
5154
string $PeerName = '',
5255
null|Locality $Locality = null
53-
)
54-
{
56+
) {
5557
$this->ID = $ID;
5658
$this->Node = $Node;
5759
$this->Address = $Address;

src/Catalog/ServiceAddress.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class ServiceAddress extends AbstractModel
2727
public string $Address;
2828
public int $Port;
2929

30+
/**
31+
* @param array<string,mixed>|null $data
32+
*/
3033
public function __construct(
3134
null|array $data = null, // Deprecated, will be removed.
3235
string $address = '',

src/Catalog/Weights.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ class Weights extends AbstractModel
2727
public int $Passing;
2828
public int $Warning;
2929

30+
/**
31+
* @param array<string,mixed>|null $data
32+
*/
33+
public function __construct(
34+
null|array $data = null, // Deprecated, will be removed.
35+
int $Passing = 0,
36+
int $Warning = 0
37+
) {
38+
$this->Passing = $Passing;
39+
$this->Warning = $Warning;
40+
if (null !== $data && [] !== $data) {
41+
self::jsonUnserialize((object)$data, $this);
42+
}
43+
}
44+
3045
public function getPassing(): int
3146
{
3247
return $this->Passing;
@@ -48,4 +63,24 @@ public function setWarning(int $Warning): self
4863
$this->Warning = $Warning;
4964
return $this;
5065
}
66+
67+
public static function jsonUnserialize(\stdClass $decoded, null|self $into = null): static
68+
{
69+
$n = $into ?? new self();
70+
foreach ($decoded as $k => $v) {
71+
$n->{$k} = $v;
72+
}
73+
return $n;
74+
}
75+
76+
public function jsonSerialize(): \stdClass
77+
{
78+
$out = new \stdClass();
79+
foreach ($this->_getDynamicFields() as $k => $v) {
80+
$out->{$k} = $v;
81+
}
82+
$out->Passing = $this->Passing;
83+
$out->Warning = $this->Warning;
84+
return $out;
85+
}
5186
}

src/ConfigEntry/ConfigEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function GetName(): string;
3434

3535
public function GetNamespace(): string;
3636

37-
public function GetMeta(): array;
37+
public function GetMeta(): \stdClass;
3838

3939
public function GetCreateIndex(): int;
4040

src/ConfigEntry/ConfigEntryTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait ConfigEntryTrait
2525
public string $Kind;
2626
public string $Name;
2727
public string $Namespace;
28-
public array $Meta;
28+
public null|\stdClass $Meta;
2929
public int $CreateIndex;
3030
public int $ModifyIndex;
3131

@@ -62,14 +62,14 @@ public function setNamespace(string $Namespace): self
6262
return $this;
6363
}
6464

65-
public function getMeta(): array
65+
public function getMeta(): null|\stdClass
6666
{
6767
return $this->Meta;
6868
}
6969

70-
public function setMeta(array|\stdClass $Meta): self
70+
public function setMeta(null|\stdClass $Meta): self
7171
{
72-
$this->Meta = (array)$Meta;
72+
$this->Meta = $Meta;
7373
return $this;
7474
}
7575

src/ConfigEntry/EnvoyExtension.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ class EnvoyExtension extends AbstractModel
2626
{
2727
public string $Name;
2828
public bool $Required;
29-
public array $Arguments;
29+
public null|\stdClass $Arguments;
3030
public string $ConsulVersion;
3131
public string $EnvoyVersion;
3232

3333
public function __construct(
3434
null|array $data = null, // Deprecated, will be removed.
3535
string $Name = '',
3636
bool $Required = false,
37-
array|\stdClass $Arguments = [],
37+
null|\stdClass $Arguments = null,
3838
string $ConsulVersion = '',
3939
string $EnvoyVersion = '',
4040
) {
4141
$this->Name = $Name;
4242
$this->Required = $Required;
43-
$this->setArguments($Arguments);
43+
$this->Arguments = $Arguments;
4444
$this->ConsulVersion = $ConsulVersion;
4545
$this->EnvoyVersion = $EnvoyVersion;
4646
if (null !== $data && [] !== $data) {
@@ -70,14 +70,14 @@ public function setRequired(bool $Required): self
7070
return $this;
7171
}
7272

73-
public function getArguments(): array
73+
public function getArguments(): null|\stdClass
7474
{
7575
return $this->Arguments;
7676
}
7777

78-
public function setArguments(array|\stdClass $Arguments): self
78+
public function setArguments(null|\stdClass $Arguments): self
7979
{
80-
$this->Arguments = (array)$Arguments;
80+
$this->Arguments = $Arguments;
8181
return $this;
8282
}
8383

@@ -107,11 +107,7 @@ public static function jsonUnserialize(\stdClass $decoded, null|self $into = nul
107107
{
108108
$n = $into ?? new self();
109109
foreach ($decoded as $k => $v) {
110-
if ('Arguments' === $k) {
111-
$n->setArguments($v);
112-
} else {
113-
$n->{$k} = $v;
114-
}
110+
$n->{$k} = $v;
115111
}
116112
return $n;
117113
}

src/Health/HealthChecks.php

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,26 @@
2222

2323
use DCarbone\PHPConsulAPI\AbstractModel;
2424
use DCarbone\PHPConsulAPI\Consul;
25-
use DCarbone\PHPConsulAPI\FakeSlice;
2625

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
2831
{
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+
}
3039

3140
public function AggregatedStatus(): string
3241
{
3342
$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)) {
3645
// TODO: Maybe just return maintenance right now...?
3746
$maintenance = true;
3847
continue;
@@ -68,8 +77,66 @@ public function AggregatedStatus(): string
6877
return Consul::HealthPassing;
6978
}
7079

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
72139
{
73-
return new HealthCheck($data);
140+
return $this->Checks;
74141
}
75142
}

0 commit comments

Comments
 (0)