Completed
Push — master ( 8e5216...cac524 )
by Nikola
10s
created

BaseExtension::fromIdentifiers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Extension;
6
7
use Version\Exception\InvalidIdentifierException;
8
9
abstract class BaseExtension
10
{
11
    protected const IDENTIFIERS_SEPARATOR = '.';
12
13
    /**
14
     * @var array
15
     */
16
    private $identifiers;
17
18 90
    protected function __construct(string ...$identifiers)
19
    {
20 90
        foreach ($identifiers as $identifier) {
21 41
            $this->validate($identifier);
22
        }
23
24 87
        $this->identifiers = $identifiers;
25 87
    }
26
27
    /**
28
     * @param string $identifier
29
     * @throws InvalidIdentifierException
30
     * @return void
31
     */
32
    abstract protected function validate(string $identifier) : void;
33
34 8
    public static function fromIdentifiers(string $firstIdentifier, string ...$identifiers)
35
    {
36 8
        return new static(...array_merge([$firstIdentifier], $identifiers));
37
    }
38
39 33
    public static function fromIdentifiersString(string $identifiers)
40
    {
41 33
        return new static(...explode(self::IDENTIFIERS_SEPARATOR, $identifiers));
42
    }
43
44 35
    public function getIdentifiers() : array
45
    {
46 35
        return $this->identifiers;
47
    }
48
49 53
    public function isEmpty() : bool
50
    {
51 53
        return empty($this->identifiers);
52
    }
53
54 16
    public function __toString() : string
55
    {
56 16
        return implode(self::IDENTIFIERS_SEPARATOR, $this->identifiers);
57
    }
58
}
59