Passed
Push — master ( 99d0e8...9e7049 )
by Ondra
03:35
created

ReferenceNumber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParamName() 0 3 1
A validate() 0 7 2
A __construct() 0 4 1
A getValue() 0 3 1
A __toString() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Pixidos package.
5
 *
6
 *  (c) Ondra Votava <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 *
11
 */
12
13
declare(strict_types=1);
14
15
namespace Pixidos\GPWebPay\Param;
16
17
use Pixidos\GPWebPay\Enum\Param;
18
use Pixidos\GPWebPay\Exceptions\InvalidArgumentException;
19
20
class ReferenceNumber implements IParam
21
{
22
    private const PATTERN = '#(^[\dA-Za-z \#\$\*\+,\-.\/:;=\^_@]{1,20}$)#';
23
24
    /**
25
     * @var string
26
     */
27
    private $value;
28
29
    /**
30
     * ReferenceNumber constructor.
31
     *
32
     * @param string $value
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 2
    public function __construct(string $value)
37
    {
38 2
        $this->validate($value);
39 1
        $this->value = $value;
40
    }
41
42 1
    public function __toString(): string
43
    {
44 1
        return $this->value;
45
    }
46
47 2
    public function getParamName(): string
48
    {
49 2
        return Param::REFERENCENUMBER;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getValue(): string
56
    {
57 1
        return $this->value;
58
    }
59
60
    /**
61
     * Interní ID u obchodníka Podporované ASCII znaky: x20(space), x23(#), x24($), x2A-x3B(*+,-./0-9:;), x3D(=),
62
     * x40-x5A(@A-Z), x5E(^), x5F(_), x61-x7A(a-z)
63
     *
64
     * @param string $value
65
     *
66
     * @throws InvalidArgumentException
67
     */
68 2
    protected function validate(string $value): void
69
    {
70 2
        if (false === (bool)preg_match(self::PATTERN, $value)) {
71 1
            throw new InvalidArgumentException(
72 1
                sprintf(
73 1
                    '%s has invalid lenght(max 20) or containt invalid char. Alowed chars are(0-9A-Za-z(space)#*+,-./:;=@^_).',
74 1
                    $this->getParamName()
75 1
                )
76 1
            );
77
        }
78
    }
79
}
80