Completed
Push — master ( c8e170...6b0b35 )
by Nikola
01:10
created

Extension::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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