Skip to content

Commit fad4cf9

Browse files
Implement JSON serialization in interfaces and classes
Updated several classes and interfaces to extend the Arrayable and Jsonable interfaces. This allows for consistent serialization of objects to JSON format. Functionality was added to the Certificate.php, StatisticsInterface.php, UnitInterface.php, Statistics.php, and Unit.php files. Furthermore, the 'chain' property in Certificate.php has been mapped to an array for easy conversion to JSON.
1 parent 85193aa commit fad4cf9

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

src/Certificate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getChain(): array
7373
{
7474
return [
7575
'key' => $this->getKey(),
76-
'chain' => $this->getChain(),
76+
'chain' => array_map(fn (ChainItem $item) => $item->toArray(), $this->getChain()),
7777
];
7878
}
7979
}

src/Contracts/StatisticsInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use UnitPhpSdk\Abstract\AbstractApplication;
66

7-
interface StatisticsInterface
7+
interface StatisticsInterface extends Arrayable, Jsonable
88
{
99
/**
1010
* Get connections
@@ -34,4 +34,14 @@ public function getApplications(): array;
3434
* @return ApplicationStatisticsInterface
3535
*/
3636
public function getApplicationStatistics(AbstractApplication|string $application): ApplicationStatisticsInterface;
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toArray(): array;
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function toJson(int $options = 0): string;
3747
}

src/Contracts/UnitInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use UnitPhpSdk\Config;
66
use UnitPhpSdk\Exceptions\UnitException;
77

8-
interface UnitInterface
8+
interface UnitInterface extends Arrayable, Jsonable
99
{
1010
/**
1111
* Return Unit socket
@@ -107,4 +107,14 @@ public function getJsModules(): array;
107107
* @return void
108108
*/
109109
public function setJsModules(array $js_modules): void;
110+
111+
/**
112+
* @inheritDoc
113+
*/
114+
public function toArray(): array;
115+
116+
/**
117+
* @inheritDoc
118+
*/
119+
public function toJson(int $options = 0): string;
110120
}

src/Statistics/Statistics.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Exception;
66
use InvalidArgumentException;
7+
use Override;
78
use UnitPhpSdk\Abstract\AbstractApplication;
89
use UnitPhpSdk\Contracts\{ApplicationStatisticsInterface,
910
Arrayable,
1011
ConnectionsStatisticsInterface,
1112
RequestsStatisticsInterface,
12-
StatisticsInterface};
13+
StatisticsInterface
14+
};
1315
use UnitPhpSdk\Exceptions\UnitParseException;
1416

1517
/**
@@ -18,7 +20,7 @@
1820
* @implements StatisticsInterface
1921
* @final
2022
*/
21-
final readonly class Statistics implements StatisticsInterface, Arrayable
23+
final readonly class Statistics implements StatisticsInterface
2224
{
2325
/**
2426
* Connections statistics
@@ -49,7 +51,7 @@ public function __construct(array $data)
4951
// $this->unitInformation = new UnitStatistics($data['unit']);
5052
$this->connections = new ConnectionsStatistics($data['connections']);
5153
$this->requests = new RequestsStatistics($data['requests']);
52-
$this->applications = array_map(fn ($item) => new ApplicationStatistics($item), $data['applications']);
54+
$this->applications = array_map(fn($item) => new ApplicationStatistics($item), $data['applications']);
5355
}
5456

5557
/**
@@ -92,12 +94,23 @@ public function getApplicationStatistics(AbstractApplication|string $application
9294
return $this->applications[$application->getName()];
9395
}
9496

95-
#[\Override] public function toArray(): array
97+
/**
98+
* @inheritDoc
99+
*/
100+
#[Override] public function toArray(): array
96101
{
97102
return [
98103
'connections' => $this->connections->toArray(),
99104
'requests' => $this->requests->toArray(),
100-
'applications' => array_map(fn ($item) => $item->toArray(), $this->applications),
105+
'applications' => array_map(fn($item) => $item->toArray(), $this->applications),
101106
];
102107
}
108+
109+
/**
110+
* @inheritDoc
111+
*/
112+
#[Override] public function toJson(int $options = 0): string
113+
{
114+
return json_encode($this->toArray(), $options);
115+
}
103116
}

src/Unit.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace UnitPhpSdk;
44

55
use UnitPhpSdk\Contracts\{CertificateInterface, UnitInterface, Uploadable};
6+
use Override;
67
use UnitPhpSdk\Enums\HttpMethodsEnum;
78
use UnitPhpSdk\Exceptions\FileNotFoundException;
89
use UnitPhpSdk\Exceptions\UnitException;
@@ -50,7 +51,8 @@ class Unit implements UnitInterface
5051
public function __construct(
5152
private readonly string $address,
5253
private readonly ?string $socket = null
53-
) {
54+
)
55+
{
5456
$this->request = new UnitRequest(
5557
address: $this->address,
5658
socket: $this->socket
@@ -297,4 +299,19 @@ public function remove(Uploadable $object): void
297299
{
298300
$object->remove($this->request);
299301
}
302+
303+
#[Override] public function toArray(): array
304+
{
305+
return [
306+
'certificates' => array_map(fn ($certificate) => $certificate->toArray(), $this->getCertificates()),
307+
'config' => $this->getConfig()->toArray(),
308+
'js_modules' => array_map(fn ($module) => $module->toArray(), $this->getJsModules()),
309+
'status' => $this->getStatistics()->toArray()
310+
];
311+
}
312+
313+
#[Override] public function toJson(int $options = 0): string
314+
{
315+
return json_encode($this->toArray(), $options);
316+
}
300317
}

0 commit comments

Comments
 (0)