| 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 WP_DEFENDER_VENDOR\Webauthn\MetadataService; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 18 |
use JsonSerializable; |
| 19 |
|
| 20 |
class ExtensionDescriptor implements JsonSerializable |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $id; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var int|null |
| 29 |
*/ |
| 30 |
private $tag; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
private $data; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
private $fail_if_unknown; |
| 41 |
|
| 42 |
public function __construct(string $id, ?int $tag, ?string $data, bool $fail_if_unknown) |
| 43 |
{ |
| 44 |
if (null !== $tag) { |
| 45 |
Assertion::greaterOrEqualThan($tag, 0, Utils::logicException('Invalid data. The parameter "tag" shall be a positive integer')); |
| 46 |
} |
| 47 |
$this->id = $id; |
| 48 |
$this->tag = $tag; |
| 49 |
$this->data = $data; |
| 50 |
$this->fail_if_unknown = $fail_if_unknown; |
| 51 |
} |
| 52 |
|
| 53 |
public function getId(): string |
| 54 |
{ |
| 55 |
return $this->id; |
| 56 |
} |
| 57 |
|
| 58 |
public function getTag(): ?int |
| 59 |
{ |
| 60 |
return $this->tag; |
| 61 |
} |
| 62 |
|
| 63 |
public function getData(): ?string |
| 64 |
{ |
| 65 |
return $this->data; |
| 66 |
} |
| 67 |
|
| 68 |
public function isFailIfUnknown(): bool |
| 69 |
{ |
| 70 |
return $this->fail_if_unknown; |
| 71 |
} |
| 72 |
|
| 73 |
public static function createFromArray(array $data): self |
| 74 |
{ |
| 75 |
$data = Utils::filterNullValues($data); |
| 76 |
Assertion::keyExists($data, 'id', Utils::logicException('Invalid data. The parameter "id" is missing')); |
| 77 |
Assertion::string($data['id'], Utils::logicException('Invalid data. The parameter "id" shall be a string')); |
| 78 |
Assertion::keyExists($data, 'fail_if_unknown', Utils::logicException('Invalid data. The parameter "fail_if_unknown" is missing')); |
| 79 |
Assertion::boolean($data['fail_if_unknown'], Utils::logicException('Invalid data. The parameter "fail_if_unknown" shall be a boolean')); |
| 80 |
if (array_key_exists('tag', $data)) { |
| 81 |
Assertion::integer($data['tag'], Utils::logicException('Invalid data. The parameter "tag" shall be a positive integer')); |
| 82 |
} |
| 83 |
if (array_key_exists('data', $data)) { |
| 84 |
Assertion::string($data['data'], Utils::logicException('Invalid data. The parameter "data" shall be a string')); |
| 85 |
} |
| 86 |
|
| 87 |
return new self( |
| 88 |
$data['id'], |
| 89 |
$data['tag'] ?? null, |
| 90 |
$data['data'] ?? null, |
| 91 |
$data['fail_if_unknown'] |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
public function jsonSerialize(): array |
| 96 |
{ |
| 97 |
$result = [ |
| 98 |
'id' => $this->id, |
| 99 |
'tag' => $this->tag, |
| 100 |
'data' => $this->data, |
| 101 |
'fail_if_unknown' => $this->fail_if_unknown, |
| 102 |
]; |
| 103 |
|
| 104 |
return Utils::filterNullValues($result); |
| 105 |
} |
| 106 |
} |
| 107 |
|