TargetName   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 18 4
A __construct() 0 6 1
A getName() 0 4 1
A getIterator() 0 4 1
A isOptional() 0 4 1
1
<?php
2
namespace Wandu\Validator;
3
4
use InvalidArgumentException;
5
6
class TargetName
7
{
8
    const PATTERN = <<<'REGEXP'
9
([a-zA-Z_][a-zA-Z0-9_-]*)((?:\[\d*\])*)(\?)?
10
REGEXP;
11
12 18
    static public function parse(string $target): TargetName
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
13
    {
14 18
        if (!preg_match('~^' . static::PATTERN . '$~ux', $target, $matches)) {
15 1
            preg_match('~' . static::PATTERN . '~ux', $target, $matches);
16 1
            $name = $matches[0];
17 1
            throw new InvalidArgumentException(
18 1
                "Invalid target name. Did you mean this? '{$name}'."
19
            );
20
        }
21 17
        $iterator = [];
22 17
        if ($matches[2]) {
23 11
            $iterator = array_map(function ($iterator) {
24 11
                if ($iterator === '') return null;
25 2
                return (int) $iterator;
26 11
            }, explode('][', rtrim(ltrim($matches[2], '['), ']')));
27
        }
28 17
        return new static($matches[1], $iterator, !!($matches[3] ?? null));
29
    }
30
    
31
    /** @var string */
32
    protected $name;
33
    
34
    /** @var array */
35
    protected $iterator;
36
37
    /** @var bool */
38
    protected $optional;
39
40 17
    public function __construct(string $name, array $iterator = [], bool $optional)
41
    {
42 17
        $this->name = $name;
43 17
        $this->iterator = $iterator;
44 17
        $this->optional = $optional;
45 17
    }
46
47
    /**
48
     * @return string
49
     */
50 17
    public function getName(): string
51
    {
52 17
        return $this->name;
53
    }
54
55
    /**
56
     * @return array
57
     */
58 17
    public function getIterator(): array
59
    {
60 17
        return $this->iterator;
61
    }
62
63
    /**
64
     * @return boolean
65
     */
66 17
    public function isOptional(): bool
67
    {
68 17
        return $this->optional;
69
    }
70
}
71