| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2021 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Webauthn\MetadataService; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use Assert\Assertion; |
| 18 |
use JsonSerializable; |
| 19 |
use function Safe\sprintf; |
| 20 |
|
| 21 |
class MetadataTOCPayload implements JsonSerializable |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @var string|null |
| 25 |
*/ |
| 26 |
private $legalHeader; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
private $no; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $nextUpdate; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var MetadataTOCPayloadEntry[] |
| 40 |
*/ |
| 41 |
private $entries = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var string[] |
| 45 |
*/ |
| 46 |
private $rootCertificates; |
| 47 |
|
| 48 |
public function __construct(int $no, string $nextUpdate, ?string $legalHeader = null) |
| 49 |
{ |
| 50 |
$this->no = $no; |
| 51 |
$this->nextUpdate = $nextUpdate; |
| 52 |
$this->legalHeader = $legalHeader; |
| 53 |
} |
| 54 |
|
| 55 |
public function addEntry(MetadataTOCPayloadEntry $entry): self |
| 56 |
{ |
| 57 |
$this->entries[] = $entry; |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function getLegalHeader(): ?string |
| 63 |
{ |
| 64 |
return $this->legalHeader; |
| 65 |
} |
| 66 |
|
| 67 |
public function getNo(): int |
| 68 |
{ |
| 69 |
return $this->no; |
| 70 |
} |
| 71 |
|
| 72 |
public function getNextUpdate(): string |
| 73 |
{ |
| 74 |
return $this->nextUpdate; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return MetadataTOCPayloadEntry[] |
| 79 |
*/ |
| 80 |
public function getEntries(): array |
| 81 |
{ |
| 82 |
return $this->entries; |
| 83 |
} |
| 84 |
|
| 85 |
public static function createFromArray(array $data): self |
| 86 |
{ |
| 87 |
$data = Utils::filterNullValues($data); |
| 88 |
foreach (['no', 'nextUpdate', 'entries'] as $key) { |
| 89 |
Assertion::keyExists($data, $key, Utils::logicException(sprintf('Invalid data. The parameter "%s" is missing', $key))); |
| 90 |
} |
| 91 |
Assertion::integer($data['no'], Utils::logicException('Invalid data. The parameter "no" shall be an integer')); |
| 92 |
Assertion::string($data['nextUpdate'], Utils::logicException('Invalid data. The parameter "nextUpdate" shall be a string')); |
| 93 |
Assertion::isArray($data['entries'], Utils::logicException('Invalid data. The parameter "entries" shall be a n array of entries')); |
| 94 |
if (array_key_exists('legalHeader', $data)) { |
| 95 |
Assertion::string($data['legalHeader'], Utils::logicException('Invalid data. The parameter "legalHeader" shall be a string')); |
| 96 |
} |
| 97 |
$object = new self( |
| 98 |
$data['no'], |
| 99 |
$data['nextUpdate'], |
| 100 |
$data['legalHeader'] ?? null |
| 101 |
); |
| 102 |
foreach ($data['entries'] as $k => $entry) { |
| 103 |
$object->addEntry(MetadataTOCPayloadEntry::createFromArray($entry)); |
| 104 |
} |
| 105 |
$object->rootCertificates = $data['rootCertificates'] ?? []; |
| 106 |
|
| 107 |
return $object; |
| 108 |
} |
| 109 |
|
| 110 |
public function jsonSerialize(): array |
| 111 |
{ |
| 112 |
$data = [ |
| 113 |
'legalHeader' => $this->legalHeader, |
| 114 |
'nextUpdate' => $this->nextUpdate, |
| 115 |
'no' => $this->no, |
| 116 |
'entries' => array_map(static function (MetadataTOCPayloadEntry $object): array { |
| 117 |
return $object->jsonSerialize(); |
| 118 |
}, $this->entries), |
| 119 |
'rootCertificates' => $this->rootCertificates, |
| 120 |
]; |
| 121 |
|
| 122 |
return Utils::filterNullValues($data); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @return string[] |
| 127 |
*/ |
| 128 |
public function getRootCertificates(): array |
| 129 |
{ |
| 130 |
return $this->rootCertificates; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @param string[] $rootCertificates |
| 135 |
*/ |
| 136 |
public function setRootCertificates(array $rootCertificates): self |
| 137 |
{ |
| 138 |
$this->rootCertificates = $rootCertificates; |
| 139 |
|
| 140 |
return $this; |
| 141 |
} |
| 142 |
} |
| 143 |
|