1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
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
|
8 |
|
public static function sanitize($input, $maxLength) |
23
|
|
|
{ |
24
|
8 |
|
$input = preg_replace(self::TEXT_NON_CH, '', $input); |
25
|
8 |
|
$input = trim(preg_replace('/\s+/', ' ', (string) $input)); |
26
|
8 |
|
$input = trim(function_exists('mb_substr') ? mb_substr($input, 0, $maxLength, 'UTF-8') : substr($input, 0, $maxLength)); |
27
|
|
|
|
28
|
8 |
|
return $input; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sanitizes and trims a string to conform to the Swiss character |
33
|
|
|
* set. |
34
|
|
|
* |
35
|
|
|
* @param string|null $input |
36
|
|
|
* @param int $maxLength |
37
|
|
|
* |
38
|
|
|
* @return string|null The sanitized string or null if it is empty. |
39
|
|
|
*/ |
40
|
1 |
|
public static function sanitizeOptional($input, $maxLength) |
41
|
|
|
{ |
42
|
1 |
|
$sanitized = self::sanitize($input, $maxLength); |
43
|
|
|
|
44
|
1 |
|
return $sanitized !== '' ? $sanitized : null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @internal |
49
|
|
|
*/ |
50
|
3 |
|
public static function assertOptional($input, $maxLength) |
51
|
|
|
{ |
52
|
3 |
|
if ($input === null) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return self::assert($input, $maxLength); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @internal |
61
|
|
|
*/ |
62
|
7 |
|
public static function assert($input, $maxLength) |
63
|
|
|
{ |
64
|
7 |
|
return self::assertNotPattern($input, $maxLength, self::TEXT_NON_CH); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @internal |
69
|
|
|
*/ |
70
|
6 |
|
public static function assertIdentifier($input) |
71
|
|
|
{ |
72
|
6 |
|
$input = self::assertNotPattern($input, 35, self::TEXT_NON_SWIFT); |
73
|
6 |
|
if ($input[0] === '/' || strpos($input, '//') !== false) { |
74
|
2 |
|
throw new InvalidArgumentException('The identifier contains unallowed slashes.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return $input; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @internal |
82
|
|
|
*/ |
83
|
4 |
|
public static function assertCountryCode($input) |
84
|
|
|
{ |
85
|
4 |
|
if (!preg_match('/^[A-Z]{2}$/', $input)) { |
86
|
1 |
|
throw new InvalidArgumentException('The country code is invalid.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return $input; |
90
|
|
|
} |
91
|
|
|
|
92
|
10 |
|
protected static function assertNotPattern($input, $maxLength, $pattern) |
93
|
|
|
{ |
94
|
10 |
|
$length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input); |
95
|
10 |
|
if (!is_string($input) || $length === 0 || $length > $maxLength) { |
96
|
1 |
|
throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength)); |
97
|
|
|
} |
98
|
9 |
|
if (preg_match($pattern, $input)) { |
99
|
1 |
|
throw new InvalidArgumentException('The string contains invalid characters.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
8 |
|
return $input; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @internal |
107
|
|
|
*/ |
108
|
3 |
|
public static function xml(DOMDocument $doc, $tag, $content) |
109
|
|
|
{ |
110
|
3 |
|
$element = $doc->createElement($tag); |
111
|
3 |
|
$element->appendChild($doc->createTextNode($content)); |
112
|
|
|
|
113
|
3 |
|
return $element; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|