| 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 function Safe\sprintf; |
| 19 |
|
| 20 |
class PatternAccuracyDescriptor extends AbstractDescriptor |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
private $minComplexity; |
| 26 |
|
| 27 |
public function __construct(int $minComplexity, ?int $maxRetries = null, ?int $blockSlowdown = null) |
| 28 |
{ |
| 29 |
Assertion::greaterOrEqualThan($minComplexity, 0, Utils::logicException('Invalid data. The value of "minComplexity" must be a positive integer')); |
| 30 |
$this->minComplexity = $minComplexity; |
| 31 |
parent::__construct($maxRetries, $blockSlowdown); |
| 32 |
} |
| 33 |
|
| 34 |
public function getMinComplexity(): int |
| 35 |
{ |
| 36 |
return $this->minComplexity; |
| 37 |
} |
| 38 |
|
| 39 |
public static function createFromArray(array $data): self |
| 40 |
{ |
| 41 |
$data = Utils::filterNullValues($data); |
| 42 |
Assertion::keyExists($data, 'minComplexity', Utils::logicException('The key "minComplexity" is missing')); |
| 43 |
foreach (['minComplexity', 'maxRetries', 'blockSlowdown'] as $key) { |
| 44 |
if (array_key_exists($key, $data)) { |
| 45 |
Assertion::integer($data[$key], Utils::logicException(sprintf('Invalid data. The value of "%s" must be a positive integer', $key))); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return new self( |
| 50 |
$data['minComplexity'], |
| 51 |
$data['maxRetries'] ?? null, |
| 52 |
$data['blockSlowdown'] ?? null |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function jsonSerialize(): array |
| 57 |
{ |
| 58 |
$data = [ |
| 59 |
'minComplexity' => $this->minComplexity, |
| 60 |
'maxRetries' => $this->getMaxRetries(), |
| 61 |
'blockSlowdown' => $this->getBlockSlowdown(), |
| 62 |
]; |
| 63 |
|
| 64 |
return Utils::filterNullValues($data); |
| 65 |
} |
| 66 |
} |
| 67 |
|