|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @internal |
|
10
|
|
|
*/ |
|
11
|
|
|
class Text |
|
12
|
|
|
{ |
|
13
|
|
|
const TEXT_CH = '/^[A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]*$/u'; |
|
14
|
|
|
const TEXT_SWIFT = '/^[A-Za-z0-9 .,:\'\/()?+\-]*$/'; |
|
15
|
|
|
|
|
16
|
3 |
|
public static function assertOptional($input, $maxLength) |
|
17
|
|
|
{ |
|
18
|
3 |
|
if ($input === null) { |
|
19
|
|
|
return null; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
3 |
|
return self::assert($input, $maxLength); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
7 |
|
public static function assert($input, $maxLength) |
|
26
|
|
|
{ |
|
27
|
7 |
|
return self::assertPattern($input, $maxLength, self::TEXT_CH); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
6 |
|
public static function assertIdentifier($input) |
|
31
|
|
|
{ |
|
32
|
6 |
|
$input = self::assertPattern($input, 35, self::TEXT_SWIFT); |
|
33
|
6 |
|
if ($input[0] === '/' || strpos($input, '//') !== false) { |
|
34
|
2 |
|
throw new InvalidArgumentException('The identifier contains unallowed slashes.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
return $input; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public static function assertCountryCode($input) |
|
41
|
|
|
{ |
|
42
|
3 |
|
if (!preg_match('/^[A-Z]{2}$/', $input)) { |
|
43
|
|
|
throw new InvalidArgumentException('The country code is invalid.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
return $input; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
10 |
|
private static function assertPattern($input, $maxLength, $pattern) |
|
50
|
|
|
{ |
|
51
|
10 |
|
$length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input); |
|
52
|
10 |
|
if (!is_string($input) || $length === 0 || $length > $maxLength) { |
|
53
|
1 |
|
throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength)); |
|
54
|
|
|
} |
|
55
|
9 |
|
if (!preg_match($pattern, $input)) { |
|
56
|
1 |
|
throw new InvalidArgumentException('The string contains invalid characters.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
return $input; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public static function xml(DOMDocument $doc, $tag, $content) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$element = $doc->createElement($tag); |
|
65
|
3 |
|
$element->appendChild($doc->createTextNode($content)); |
|
66
|
|
|
|
|
67
|
3 |
|
return $element; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|