defender-security
/
lib
/
packages
/
Webauthn
/
AuthenticationExtensions
/
AuthenticationExtensionsClientInputs.php
AuthenticationExtensionsClientInputs.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at lib/packages/Webauthn/AuthenticationExtensions/AuthenticationExtensionsClientInputs.php
| 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\AuthenticationExtensions; |
| 15 | |
| 16 | use function array_key_exists; |
| 17 | use ArrayIterator; |
| 18 | use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 19 | use function count; |
| 20 | use Countable; |
| 21 | use Iterator; |
| 22 | use IteratorAggregate; |
| 23 | use JsonSerializable; |
| 24 | use function WP_DEFENDER_VENDOR\Safe\sprintf; |
| 25 | |
| 26 | class AuthenticationExtensionsClientInputs implements JsonSerializable, Countable, IteratorAggregate |
| 27 | { |
| 28 | /** |
| 29 | * @var AuthenticationExtension[] |
| 30 | */ |
| 31 | private $extensions = []; |
| 32 | |
| 33 | public function add(AuthenticationExtension $extension): void |
| 34 | { |
| 35 | $this->extensions[$extension->name()] = $extension; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param mixed[] $json |
| 40 | */ |
| 41 | public static function createFromArray(array $json): self |
| 42 | { |
| 43 | $object = new self(); |
| 44 | foreach ($json as $k => $v) { |
| 45 | $object->add(new AuthenticationExtension($k, $v)); |
| 46 | } |
| 47 | |
| 48 | return $object; |
| 49 | } |
| 50 | |
| 51 | public function has(string $key): bool |
| 52 | { |
| 53 | return array_key_exists($key, $this->extensions); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return mixed |
| 58 | */ |
| 59 | public function get(string $key) |
| 60 | { |
| 61 | Assertion::true($this->has($key), sprintf('The extension with key "%s" is not available', $key)); |
| 62 | |
| 63 | return $this->extensions[$key]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return AuthenticationExtension[] |
| 68 | */ |
| 69 | public function jsonSerialize(): array |
| 70 | { |
| 71 | return array_map(static function (AuthenticationExtension $object) { |
| 72 | return $object->jsonSerialize(); |
| 73 | }, $this->extensions); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return Iterator<string, AuthenticationExtension> |
| 78 | */ |
| 79 | public function getIterator(): Iterator |
| 80 | { |
| 81 | return new ArrayIterator($this->extensions); |
| 82 | } |
| 83 | |
| 84 | public function count(int $mode = COUNT_NORMAL): int |
| 85 | { |
| 86 | return count($this->extensions, $mode); |
| 87 | } |
| 88 | } |
| 89 |