1 | <?php |
||
8 | class Text |
||
9 | { |
||
10 | const TEXT_NON_CH = '/[^A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]+/u'; |
||
11 | const TEXT_NON_SWIFT = '/[^A-Za-z0-9 .,:\'\/()?+\-]+/'; |
||
12 | |||
13 | /** |
||
14 | * Sanitizes and trims a string to conform to the Swiss character |
||
15 | * set. |
||
16 | * |
||
17 | * @param string|null $input |
||
18 | * @param int $maxLength |
||
19 | * |
||
20 | * @return string The sanitized string |
||
21 | */ |
||
22 | 6 | public static function sanitize($input, $maxLength) |
|
29 | |||
30 | /** |
||
31 | * Sanitizes and trims a string to conform to the Swiss character |
||
32 | * set. |
||
33 | * |
||
34 | * @param string|null $input |
||
35 | * @param int $maxLength |
||
36 | * |
||
37 | * @return string|null The sanitized string or null if it is empty. |
||
38 | */ |
||
39 | 1 | public static function sanitizeOptional($input, $maxLength) |
|
45 | |||
46 | /** |
||
47 | * @internal |
||
48 | */ |
||
49 | 3 | public static function assertOptional($input, $maxLength) |
|
50 | { |
||
51 | 3 | if ($input === null) { |
|
52 | return null; |
||
53 | } |
||
54 | |||
55 | 3 | return self::assert($input, $maxLength); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @internal |
||
60 | */ |
||
61 | 7 | public static function assert($input, $maxLength) |
|
62 | { |
||
63 | 7 | return self::assertNotPattern($input, $maxLength, self::TEXT_NON_CH); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @internal |
||
68 | */ |
||
69 | 6 | public static function assertIdentifier($input) |
|
70 | { |
||
71 | 6 | $input = self::assertNotPattern($input, 35, self::TEXT_NON_SWIFT); |
|
72 | 6 | if ($input[0] === '/' || strpos($input, '//') !== false) { |
|
73 | 2 | throw new InvalidArgumentException('The identifier contains unallowed slashes.'); |
|
74 | } |
||
75 | |||
76 | 4 | return $input; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @internal |
||
81 | */ |
||
82 | 4 | public static function assertCountryCode($input) |
|
83 | { |
||
84 | 4 | if (!preg_match('/^[A-Z]{2}$/', $input)) { |
|
85 | 1 | throw new InvalidArgumentException('The country code is invalid.'); |
|
86 | } |
||
87 | |||
88 | 3 | return $input; |
|
89 | } |
||
90 | |||
91 | 10 | protected static function assertNotPattern($input, $maxLength, $pattern) |
|
103 | |||
104 | /** |
||
105 | * @internal |
||
106 | */ |
||
107 | 3 | public static function xml(DOMDocument $doc, $tag, $content) |
|
114 | } |
||
115 |