Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 6df29e0

Browse files
Refactor Certificate class and add ChainItem class
In the Certificate class, we expanded its functionality to implement Arrayable interface and added data validation in the constructor. Additionally, we introduced a new ChainItem class which holds information about individual parts of the certificate chain. This aims to provide a more granular and controlled access over individual components of the SSL certificate chain. The toArray methods in both classes were also annotated with #[\Override] attribute.
1 parent 94f7948 commit 6df29e0

File tree

3 files changed

+101
-13
lines changed

3 files changed

+101
-13
lines changed

src/Certificate.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,43 @@
22

33
namespace UnitPhpSdk;
44

5+
use UnitPhpSdk\Certificate\ChainItem;
6+
use UnitPhpSdk\Contracts\Arrayable;
57
use UnitPhpSdk\Contracts\CertificateInterface;
68

79
/**
810
* Certificate class
911
*
1012
* @implements CertificateInterface
1113
*/
12-
readonly class Certificate implements CertificateInterface
14+
readonly class Certificate implements CertificateInterface, Arrayable
1315
{
16+
/**
17+
* @var array $chain
18+
*/
19+
private array $chain;
20+
21+
/**
22+
* @var string|mixed $key
23+
*/
24+
private string $key;
25+
1426
public function __construct(
15-
private array $data,
27+
array $data,
1628
private string $name
1729
) {
18-
//
30+
if (!isset($data['key']) || !isset($data['chain'])) {
31+
throw new \InvalidArgumentException('Certificate data should contain key and chain');
32+
}
33+
34+
$chainItems = [];
35+
foreach ($data['chain'] as $item) {
36+
$chainItems[] = new ChainItem($item);
37+
}
38+
39+
$this->chain = $chainItems;
40+
41+
$this->key = $data['key'];
1942
}
2043

2144
/**
@@ -33,24 +56,22 @@ public function getName(): string
3356
*/
3457
public function getKey(): string
3558
{
36-
return $this->data['key'];
59+
return $this->key;
3760
}
3861

3962
/**
4063
* @inheritDoc
4164
*/
4265
public function getChain(): array
4366
{
44-
return $this->data['chain'];
67+
return $this->chain;
4568
}
4669

47-
/**
48-
* Return all certificate data as array
49-
*
50-
* @return array
51-
*/
52-
public function getData(): array
70+
#[\Override] public function toArray(): array
5371
{
54-
return $this->data;
72+
return [
73+
'key' => $this->getKey(),
74+
'chain' => $this->getChain(),
75+
];
5576
}
5677
}

src/Certificate/ChainItem.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Certificate;
4+
5+
use UnitPhpSdk\Contracts\Arrayable;
6+
7+
class ChainItem implements Arrayable
8+
{
9+
/**
10+
* @var array|mixed $subjet
11+
*/
12+
private array $subjet;
13+
14+
/**
15+
* @var array|mixed $issuer
16+
*/
17+
private array $issuer;
18+
19+
/**
20+
* Holds the validity information for the current object.
21+
*
22+
* @var array|mixed
23+
*/
24+
private array $validity;
25+
26+
public function __construct(array $data)
27+
{
28+
$this->subjet = $data['subject'];
29+
$this->issuer = $data['issuer'];
30+
$this->validity = $data['validity'];
31+
}
32+
33+
/**
34+
* @return array
35+
*/
36+
public function getSubject(): array
37+
{
38+
return $this->subjet;
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
public function getIssuer(): array
45+
{
46+
return $this->issuer;
47+
}
48+
49+
/**
50+
* Retrieves the validity of the object.
51+
*
52+
* @return array The array containing the validity information.
53+
*/
54+
public function getValidity(): array
55+
{
56+
return $this->validity;
57+
}
58+
59+
#[\Override] public function toArray(): array
60+
{
61+
return [
62+
'subject' => $this->subjet,
63+
'issuer' => $this->issuer,
64+
'validity' => $this->validity,
65+
];
66+
}
67+
}

src/Config/Settings/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function isServerVersion(): bool
174174
*
175175
* @return array An associative array containing the properties of the object.
176176
*/
177-
public function toArray(): array
177+
#[\Override] public function toArray(): array
178178
{
179179
return [
180180
'body_read_timeout' => $this->body_read_timeout,

0 commit comments

Comments
 (0)