Completed
Pull Request — master (#22)
by z38
03:08 queued 01:44
created

Text::assertNotPattern()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 8.8571
cc 6
eloc 7
nc 6
nop 3
crap 6
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 6
    public static function sanitize($input, $maxLength)
23
    {
24 6
        $input = preg_replace('/\s+/', ' ', (string) $input);
25 6
        $input = trim(preg_replace(self::TEXT_NON_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 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)
92
    {
93 10
        $length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input);
94 10
        if (!is_string($input) || $length === 0 || $length > $maxLength) {
95 1
            throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength));
96
        }
97 9
        if (preg_match($pattern, $input)) {
98 1
            throw new InvalidArgumentException('The string contains invalid characters.');
99
        }
100
101 8
        return $input;
102
    }
103
104
    /**
105
     * @internal
106
     */
107 3
    public static function xml(DOMDocument $doc, $tag, $content)
108
    {
109 3
        $element = $doc->createElement($tag);
110 3
        $element->appendChild($doc->createTextNode($content));
111
112 3
        return $element;
113
    }
114
}
115