Passed
Branch develop (84afed)
by Paulius
02:35
created

Code::getCountryPatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace Dokobit\Gateway\Validator\Constraints;
3
4
use Symfony\Component\Validator\Constraints\Regex;
5
6
/**
7
 * Class Code
8
 * @package DokobitApiBundle
9
 */
10
class Code extends Regex
11
{
12
    /**
13
     * Base validation pattern. All other parts are built on the fly
14
     */
15
    const VALIDATION_PATTERN = '/^(%s)$/';
16
17
    /**
18
     * @var string
19
     */
20
    public $message = 'Code format is not valid';
21
22
    /**
23
     * @param mixed $options
24
     * @return self
25
     */
26
    public function __construct($options = null)
27
    {
28
        $options['pattern'] = sprintf(
29
            self::VALIDATION_PATTERN,
30
            $this->buildPattern($this->getCountryPatterns())
31
        );
32
        parent::__construct($options);
33
    }
34
35
    /**
36
     * Build one regexp pattern from given list of patterns
37
     * @param array $patterns
38
     * @return string built regexp
39
     */
40
    private function buildPattern(array $patterns)
41
    {
42
        $parts = [];
43
        foreach ($patterns as $pattern) {
44
            $parts[] = sprintf('(%s)', $pattern);
45
        }
46
47
        return implode('|', $parts);
48
    }
49
50
    /**
51
     * Regexp patterns by country
52
     * IMPORTANT!
53
     * DO NOT FORGET TO UPDATE REGULAR EXPRESSION VALIDATOR IN JAVASCRIPT!
54
     * @return array
55
     */
56
    private function getCountryPatterns()
57
    {
58
        return [
59
            'test' => '11412090004',
60
            'lt,ee' => '[3456]{1}[0-9]{2}(0[1-9]|1[0-2])\d{6}',
61
            'fi' => '[0-9]{2}(0[1-9]|1[0-2])[0-9]{2}[\+-A]\d{4}'
62
        ];
63
    }
64
}
65