| 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 Assert\Assertion; |
| 17 |
use JsonSerializable; |
| 18 |
use function Safe\json_decode; |
| 19 |
|
| 20 |
class PublicKeyCredentialParameters implements JsonSerializable |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $type; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
private $alg; |
| 31 |
|
| 32 |
public function __construct(string $type, int $alg) |
| 33 |
{ |
| 34 |
$this->type = $type; |
| 35 |
$this->alg = $alg; |
| 36 |
} |
| 37 |
|
| 38 |
public function getType(): string |
| 39 |
{ |
| 40 |
return $this->type; |
| 41 |
} |
| 42 |
|
| 43 |
public function getAlg(): int |
| 44 |
{ |
| 45 |
return $this->alg; |
| 46 |
} |
| 47 |
|
| 48 |
public static function createFromString(string $data): self |
| 49 |
{ |
| 50 |
$data = json_decode($data, true); |
| 51 |
Assertion::isArray($data, 'Invalid data'); |
| 52 |
|
| 53 |
return self::createFromArray($data); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param mixed[] $json |
| 58 |
*/ |
| 59 |
public static function createFromArray(array $json): self |
| 60 |
{ |
| 61 |
Assertion::keyExists($json, 'type', 'Invalid input. "type" is missing.'); |
| 62 |
Assertion::string($json['type'], 'Invalid input. "type" is not a string.'); |
| 63 |
Assertion::keyExists($json, 'alg', 'Invalid input. "alg" is missing.'); |
| 64 |
Assertion::integer($json['alg'], 'Invalid input. "alg" is not an integer.'); |
| 65 |
|
| 66 |
return new self( |
| 67 |
$json['type'], |
| 68 |
$json['alg'] |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @return mixed[] |
| 74 |
*/ |
| 75 |
public function jsonSerialize(): array |
| 76 |
{ |
| 77 |
return [ |
| 78 |
'type' => $this->type, |
| 79 |
'alg' => $this->alg, |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|