1<?php 2 3declare(strict_types=1); 4 5/* 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2014-2019 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 14namespace Webauthn\AuthenticationExtensions; 15 16use ArrayIterator; 17use Assert\Assertion; 18use function count; 19use Countable; 20use Iterator; 21use IteratorAggregate; 22use JsonSerializable; 23 24class AuthenticationExtensionsClientInputs implements JsonSerializable, Countable, IteratorAggregate 25{ 26 /** 27 * @var AuthenticationExtension[] 28 */ 29 private $extensions = []; 30 31 public function add(AuthenticationExtension $extension): void 32 { 33 $this->extensions[$extension->name()] = $extension; 34 } 35 36 public static function createFromArray(array $json): self 37 { 38 $object = new self(); 39 foreach ($json as $k => $v) { 40 $object->add(new AuthenticationExtension($k, $v)); 41 } 42 43 return $object; 44 } 45 46 public function has(string $key): bool 47 { 48 return \array_key_exists($key, $this->extensions); 49 } 50 51 /** 52 * @return mixed 53 */ 54 public function get(string $key) 55 { 56 Assertion::true($this->has($key), sprintf('The extension with key "%s" is not available', $key)); 57 58 return $this->extensions[$key]; 59 } 60 61 public function jsonSerialize(): array 62 { 63 return $this->extensions; 64 } 65 66 public function getIterator(): Iterator 67 { 68 return new ArrayIterator($this->extensions); 69 } 70 71 public function count(int $mode = COUNT_NORMAL): int 72 { 73 return \count($this->extensions, $mode); 74 } 75} 76