Completed
Pull Request — master (#22)
by z38
05:31 queued 03:38
created

Text::sanitize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Z38\SwissPayment;
4
5
use DOMDocument;
6
use InvalidArgumentException;
7
8
class Text
9
{
10
    const TEXT_NOT_CH = '/[^A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]+/u';
11
    const TEXT_NOT_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)
23
    {
24 6
        $input = preg_replace('/\s+/', ' ', (string) $input);
25 6
        $input = trim(preg_replace(self::TEXT_NOT_CH, '', $input));
26
27 6
        return function_exists('mb_substr') ? mb_substr($input, 0, $maxLength, 'UTF-8') : substr($input, 0, $maxLength);
28
    }
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)
40
    {
41 1
        $sanitized = self::sanitize($input, $maxLength);
42
43 1
        return $sanitized !== '' ? $sanitized : null;
44
    }
45
46
    /**
47
     * @internal
48
     */
49 1
    public static function sanitizeCountryCode($input)
50
    {
51 1
        return preg_replace('/[^A-Z]+/', '', strtoupper($input));
52
    }
53
54
    /**
55
     * @internal
56
     */
57 3
    public static function assertOptional($input, $maxLength)
58
    {
59 3
        if ($input === null) {
60
            return null;
61
        }
62
63 3
        return self::assert($input, $maxLength);
64
    }
65
66
    /**
67
     * @internal
68
     */
69 7
    public static function assert($input, $maxLength)
70
    {
71 7
        return self::assertNotPattern($input, $maxLength, self::TEXT_NOT_CH);
72
    }
73
74
    /**
75
     * @internal
76
     */
77 6
    public static function assertIdentifier($input)
78
    {
79 6
        $input = self::assertNotPattern($input, 35, self::TEXT_NOT_SWIFT);
80 6
        if ($input[0] === '/' || strpos($input, '//') !== false) {
81 2
            throw new InvalidArgumentException('The identifier contains unallowed slashes.');
82
        }
83
84 4
        return $input;
85
    }
86
87
    /**
88
     * @internal
89
     */
90 3
    public static function assertCountryCode($input)
91
    {
92 3
        if (!preg_match('/^[A-Z]{2}$/', $input)) {
93
            throw new InvalidArgumentException('The country code is invalid.');
94
        }
95
96 3
        return $input;
97
    }
98
99 10
    protected static function assertNotPattern($input, $maxLength, $pattern)
100
    {
101 10
        $length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input);
102 10
        if (!is_string($input) || $length === 0 || $length > $maxLength) {
103 1
            throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength));
104
        }
105 9
        if (preg_match($pattern, $input)) {
106 1
            throw new InvalidArgumentException('The string contains invalid characters.');
107
        }
108
109 8
        return $input;
110
    }
111
112
    /**
113
     * @internal
114
     */
115 3
    public static function xml(DOMDocument $doc, $tag, $content)
116
    {
117 3
        $element = $doc->createElement($tag);
118 3
        $element->appendChild($doc->createTextNode($content));
119
120 3
        return $element;
121
    }
122
}
123