forked from z38/swiss-payment
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Text.php
135 lines (117 loc) · 3.57 KB
/
Text.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Z38\SwissPayment;
use DOMDocument;
use DOMElement;
use DOMException;
use InvalidArgumentException;
/**
* This class permits to sanitize all texts
*/
class Text
{
private const TEXT_NON_CH = '/[^A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]+/u';
private const TEXT_NON_SWIFT = '/[^A-Za-z0-9 .,:\'\/()?+\-]+/';
/**
* Sanitizes and trims a string to conform to the Swiss character
* set.
*
* @param string|null $input
* @param int $maxLength
*
* @return string The sanitized string
*/
public static function sanitize($input, $maxLength)
{
$input = preg_replace('/\s+/', ' ', (string) $input);
$input = trim(preg_replace(self::TEXT_NON_CH, '', $input));
return function_exists('mb_substr') ? mb_substr($input, 0, $maxLength, 'UTF-8') : substr($input, 0, $maxLength);
}
/**
* Sanitizes and trims a string to conform to the Swiss character
* set.
*
* @param string|null $input
* @param int $maxLength
*
* @return string|null The sanitized string or null if it is empty.
*/
public static function sanitizeOptional($input, $maxLength)
{
$sanitized = self::sanitize($input, $maxLength);
return $sanitized !== '' ? $sanitized : null;
}
/**
* @param $input
* @param $maxLength
* @return string|null
*/
public static function assertOptional($input, $maxLength)
{
if ($input === null) {
return null;
}
return self::assert($input, $maxLength);
}
/**
* @param $input
* @param $maxLength
* @return string
*/
public static function assert($input, $maxLength)
{
return self::assertNotPattern($input, $maxLength, self::TEXT_NON_CH);
}
/**
* @param $input
* @return string
*/
public static function assertIdentifier($input)
{
$input = self::assertNotPattern($input, 35, self::TEXT_NON_SWIFT);
if ($input[0] === '/' || strpos($input, '//') !== false) {
throw new InvalidArgumentException('The identifier contains unallowed slashes.');
}
return $input;
}
/**
* @param $input
* @return mixed
*/
public static function assertCountryCode($input)
{
if (!preg_match('/^[A-Z]{2}$/', $input)) {
throw new InvalidArgumentException('The country code is invalid.');
}
return $input;
}
/**
* @param $input
* @param $maxLength
* @param $pattern
* @return string
*/
protected static function assertNotPattern($input, $maxLength, $pattern)
{
$length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input);
if (!is_string($input) || $length === 0 || $length > $maxLength) {
throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength));
}
if (preg_match($pattern, $input)) {
throw new InvalidArgumentException('The string contains invalid characters.');
}
return $input;
}
/**
* @param DOMDocument $doc
* @param $tag
* @param $content
* @return DOMElement|false
* @throws DOMException
*/
public static function xml(DOMDocument $doc, $tag, $content)
{
$element = $doc->createElement($tag);
$element->appendChild($doc->createTextNode($content));
return $element;
}
}