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

Text   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 59
ccs 24
cts 26
cp 0.9231
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assertOptional() 0 8 2
A assert() 0 4 1
A assertIdentifier() 0 9 3
A assertCountryCode() 0 8 2
A xml() 0 7 1
B assertPattern() 0 12 6
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