Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/StringPrep/StringPreparer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\StringPrep;
6
7 /**
8 * Implement Internationalized String Preparation as specified by RFC 4518.
9 *
10 * @see https://tools.ietf.org/html/rfc4518
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 * @param PrepareStep[] $_steps Preparation steps to apply
28 */
29 private function __construct(
30 /**
31 * Preparation steps.
32 */
33 private array $_steps
34 ) {
35 }
36
37 /**
38 * Get default instance for given string type.
39 *
40 * @param int $string_type ASN.1 string type tag.
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 // @todo Vary by string type
51 self::STEP_INSIGNIFICANT_CHARS => new InsignificantNonSubstringSpaceStep(),
52 ];
53 return new self($steps);
54 }
55
56 /**
57 * Get self with case folding set.
58 *
59 * @param bool $fold True to apply case folding
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 * Prepare string.
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 }