| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\AuthenticationExtensions; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use ArrayAccess; |
| 9 |
use ArrayIterator; |
| 10 |
use function count; |
| 11 |
use const COUNT_NORMAL; |
| 12 |
use Countable; |
| 13 |
use function is_string; |
| 14 |
use Iterator; |
| 15 |
use IteratorAggregate; |
| 16 |
use function sprintf; |
| 17 |
use Webauthn\Exception\AuthenticationExtensionException; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class AuthenticationExtensions implements Countable, IteratorAggregate, ArrayAccess |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public array $extensions; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct(array $extensions = []) |
| 34 |
{ |
| 35 |
$list = []; |
| 36 |
foreach ($extensions as $key => $extension) { |
| 37 |
if ($extension instanceof AuthenticationExtension) { |
| 38 |
$list[$extension->name] = $extension; |
| 39 |
|
| 40 |
continue; |
| 41 |
} |
| 42 |
if (is_string($key)) { |
| 43 |
$list[$key] = AuthenticationExtension::create($key, $extension); |
| 44 |
continue; |
| 45 |
} |
| 46 |
throw new AuthenticationExtensionException('Invalid extension'); |
| 47 |
} |
| 48 |
$this->extensions = $list; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static function create(array $extensions = []): static |
| 55 |
{ |
| 56 |
return new static($extensions); |
| 57 |
} |
| 58 |
|
| 59 |
public function has(string $key): bool |
| 60 |
{ |
| 61 |
return array_key_exists($key, $this->extensions); |
| 62 |
} |
| 63 |
|
| 64 |
public function get(string $key): AuthenticationExtension |
| 65 |
{ |
| 66 |
$this->has($key) || throw AuthenticationExtensionException::create(sprintf( |
| 67 |
'The extension with key "%s" is not available', |
| 68 |
$key |
| 69 |
)); |
| 70 |
|
| 71 |
return $this->extensions[$key]; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function getIterator(): Iterator |
| 78 |
{ |
| 79 |
return new ArrayIterator($this->extensions); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function count(int $mode = COUNT_NORMAL): int |
| 86 |
{ |
| 87 |
return count($this->extensions, $mode); |
| 88 |
} |
| 89 |
|
| 90 |
public function offsetExists(mixed $offset): bool |
| 91 |
{ |
| 92 |
return array_key_exists($offset, $this->extensions); |
| 93 |
} |
| 94 |
|
| 95 |
public function offsetGet(mixed $offset): AuthenticationExtension |
| 96 |
{ |
| 97 |
return $this->extensions[$offset]; |
| 98 |
} |
| 99 |
|
| 100 |
public function offsetSet(mixed $offset, mixed $value): void |
| 101 |
{ |
| 102 |
if ($value === null) { |
| 103 |
return; |
| 104 |
} |
| 105 |
if ($value instanceof AuthenticationExtension) { |
| 106 |
$this->extensions[$value->name] = $value; |
| 107 |
return; |
| 108 |
} |
| 109 |
if (is_string($offset)) { |
| 110 |
$this->extensions[$offset] = AuthenticationExtension::create($offset, $value); |
| 111 |
return; |
| 112 |
} |
| 113 |
throw new AuthenticationExtensionException('Invalid extension'); |
| 114 |
} |
| 115 |
|
| 116 |
public function offsetUnset(mixed $offset): void |
| 117 |
{ |
| 118 |
unset($this->extensions[$offset]); |
| 119 |
} |
| 120 |
} |