| 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\MetadataService; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use Assert\Assertion; |
| 18 |
use JsonSerializable; |
| 19 |
use LogicException; |
| 20 |
use function Safe\sprintf; |
| 21 |
|
| 22 |
class Version implements JsonSerializable |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @var int|null |
| 26 |
*/ |
| 27 |
private $major; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var int|null |
| 31 |
*/ |
| 32 |
private $minor; |
| 33 |
|
| 34 |
public function __construct(?int $major, ?int $minor) |
| 35 |
{ |
| 36 |
if (null === $major && null === $minor) { |
| 37 |
throw new LogicException('Invalid data. Must contain at least one item'); |
| 38 |
} |
| 39 |
Assertion::greaterOrEqualThan($major, 0, Utils::logicException('Invalid argument "major"')); |
| 40 |
Assertion::greaterOrEqualThan($minor, 0, Utils::logicException('Invalid argument "minor"')); |
| 41 |
|
| 42 |
$this->major = $major; |
| 43 |
$this->minor = $minor; |
| 44 |
} |
| 45 |
|
| 46 |
public function getMajor(): ?int |
| 47 |
{ |
| 48 |
return $this->major; |
| 49 |
} |
| 50 |
|
| 51 |
public function getMinor(): ?int |
| 52 |
{ |
| 53 |
return $this->minor; |
| 54 |
} |
| 55 |
|
| 56 |
public static function createFromArray(array $data): self |
| 57 |
{ |
| 58 |
$data = Utils::filterNullValues($data); |
| 59 |
foreach (['major', 'minor'] as $key) { |
| 60 |
if (array_key_exists($key, $data)) { |
| 61 |
Assertion::integer($data[$key], sprintf('Invalid value for key "%s"', $key)); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return new self( |
| 66 |
$data['major'] ?? null, |
| 67 |
$data['minor'] ?? null |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function jsonSerialize(): array |
| 72 |
{ |
| 73 |
$data = [ |
| 74 |
'major' => $this->major, |
| 75 |
'minor' => $this->minor, |
| 76 |
]; |
| 77 |
|
| 78 |
return Utils::filterNullValues($data); |
| 79 |
} |
| 80 |
} |
| 81 |
|