| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X501\StringPrep; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
final class StringPreparer |
| 13 |
{ |
| 14 |
final public const STEP_TRANSCODE = 1; |
| 15 |
|
| 16 |
final public const STEP_MAP = 2; |
| 17 |
|
| 18 |
final public const STEP_NORMALIZE = 3; |
| 19 |
|
| 20 |
final public const STEP_PROHIBIT = 4; |
| 21 |
|
| 22 |
final public const STEP_CHECK_BIDI = 5; |
| 23 |
|
| 24 |
final public const STEP_INSIGNIFICANT_CHARS = 6; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private function __construct( |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private array $_steps |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static function forStringType(int $string_type): self |
| 43 |
{ |
| 44 |
$steps = [ |
| 45 |
self::STEP_TRANSCODE => TranscodeStep::create($string_type), |
| 46 |
self::STEP_MAP => MapStep::create(), |
| 47 |
self::STEP_NORMALIZE => new NormalizeStep(), |
| 48 |
self::STEP_PROHIBIT => new ProhibitStep(), |
| 49 |
self::STEP_CHECK_BIDI => new CheckBidiStep(), |
| 50 |
|
| 51 |
self::STEP_INSIGNIFICANT_CHARS => new InsignificantNonSubstringSpaceStep(), |
| 52 |
]; |
| 53 |
return new self($steps); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function withCaseFolding(bool $fold): self |
| 62 |
{ |
| 63 |
$obj = clone $this; |
| 64 |
$obj->_steps[self::STEP_MAP] = MapStep::create($fold); |
| 65 |
return $obj; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function prepare(string $string): string |
| 72 |
{ |
| 73 |
foreach ($this->_steps as $step) { |
| 74 |
$string = $step->apply($string); |
| 75 |
} |
| 76 |
return $string; |
| 77 |
} |
| 78 |
} |