defender-security
/
vendor
/
web-auth
/
webauthn-lib
/
src
/
PublicKeyCredentialDescriptorCollection.php
PublicKeyCredentialDescriptorCollection.php in Defender Security – Malware Scanner, Login Security & Firewall 5.3.1, at vendor/web-auth/webauthn-lib/src/PublicKeyCredentialDescriptorCollection.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 Webauthn; |
| 15 | |
| 16 | use function array_key_exists; |
| 17 | use ArrayIterator; |
| 18 | use Assert\Assertion; |
| 19 | use function count; |
| 20 | use Countable; |
| 21 | use Iterator; |
| 22 | use IteratorAggregate; |
| 23 | use JsonSerializable; |
| 24 | use function Safe\json_decode; |
| 25 | |
| 26 | class PublicKeyCredentialDescriptorCollection implements JsonSerializable, Countable, IteratorAggregate |
| 27 | { |
| 28 | /** |
| 29 | * @var PublicKeyCredentialDescriptor[] |
| 30 | */ |
| 31 | private $publicKeyCredentialDescriptors = []; |
| 32 | |
| 33 | public function add(PublicKeyCredentialDescriptor $publicKeyCredentialDescriptor): void |
| 34 | { |
| 35 | $this->publicKeyCredentialDescriptors[$publicKeyCredentialDescriptor->getId()] = $publicKeyCredentialDescriptor; |
| 36 | } |
| 37 | |
| 38 | public function has(string $id): bool |
| 39 | { |
| 40 | return array_key_exists($id, $this->publicKeyCredentialDescriptors); |
| 41 | } |
| 42 | |
| 43 | public function remove(string $id): void |
| 44 | { |
| 45 | if (!$this->has($id)) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | unset($this->publicKeyCredentialDescriptors[$id]); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return Iterator<string, PublicKeyCredentialDescriptor> |
| 54 | */ |
| 55 | public function getIterator(): Iterator |
| 56 | { |
| 57 | return new ArrayIterator($this->publicKeyCredentialDescriptors); |
| 58 | } |
| 59 | |
| 60 | public function count(int $mode = COUNT_NORMAL): int |
| 61 | { |
| 62 | return count($this->publicKeyCredentialDescriptors, $mode); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return array[] |
| 67 | */ |
| 68 | public function jsonSerialize(): array |
| 69 | { |
| 70 | return array_map(static function (PublicKeyCredentialDescriptor $object): array { |
| 71 | return $object->jsonSerialize(); |
| 72 | }, $this->publicKeyCredentialDescriptors); |
| 73 | } |
| 74 | |
| 75 | public static function createFromString(string $data): self |
| 76 | { |
| 77 | $data = json_decode($data, true); |
| 78 | Assertion::isArray($data, 'Invalid data'); |
| 79 | |
| 80 | return self::createFromArray($data); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param mixed[] $json |
| 85 | */ |
| 86 | public static function createFromArray(array $json): self |
| 87 | { |
| 88 | $collection = new self(); |
| 89 | foreach ($json as $item) { |
| 90 | $collection->add(PublicKeyCredentialDescriptor::createFromArray($item)); |
| 91 | } |
| 92 | |
| 93 | return $collection; |
| 94 | } |
| 95 | } |
| 96 |