| 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\TrustPath; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use Assert\Assertion; |
| 18 |
use function in_array; |
| 19 |
use InvalidArgumentException; |
| 20 |
use function Safe\class_implements; |
| 21 |
use function Safe\sprintf; |
| 22 |
|
| 23 |
abstract class TrustPathLoader |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @param mixed[] $data |
| 27 |
*/ |
| 28 |
public static function loadTrustPath(array $data): TrustPath |
| 29 |
{ |
| 30 |
Assertion::keyExists($data, 'type', 'The trust path type is missing'); |
| 31 |
$type = $data['type']; |
| 32 |
$oldTypes = self::oldTrustPathTypes(); |
| 33 |
switch (true) { |
| 34 |
case array_key_exists($type, $oldTypes): |
| 35 |
return $oldTypes[$type]::createFromArray($data); |
| 36 |
case class_exists($type): |
| 37 |
$implements = class_implements($type); |
| 38 |
if (in_array(TrustPath::class, $implements, true)) { |
| 39 |
return $type::createFromArray($data); |
| 40 |
} |
| 41 |
// no break |
| 42 |
default: |
| 43 |
throw new InvalidArgumentException(sprintf('The trust path type "%s" is not supported', $data['type'])); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return string[] |
| 49 |
*/ |
| 50 |
private static function oldTrustPathTypes(): array |
| 51 |
{ |
| 52 |
return [ |
| 53 |
'empty' => EmptyTrustPath::class, |
| 54 |
'ecdaa_key_id' => EcdaaKeyIdTrustPath::class, |
| 55 |
'x5c' => CertificateTrustPath::class, |
| 56 |
]; |
| 57 |
} |
| 58 |
} |
| 59 |
|