1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Version\Metadata; |
6
|
|
|
|
7
|
|
|
use Version\Identifier\Identifier; |
8
|
|
|
use Version\Exception\InvalidArgumentException; |
9
|
|
|
use Version\Exception\LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Nikola Posa <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseIdentifyingMetadata |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Identifier[] |
18
|
|
|
*/ |
19
|
|
|
private $identifiers; |
20
|
|
|
|
21
|
90 |
|
private function __construct(Identifier ...$identifiers) |
22
|
|
|
{ |
23
|
90 |
|
$this->identifiers = $identifiers; |
24
|
90 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array|string $identifiers |
28
|
|
|
* @return static |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
44 |
|
public static function create($identifiers) |
32
|
|
|
{ |
33
|
44 |
|
if ($identifiers instanceof self) { |
34
|
|
|
return $identifiers; |
35
|
|
|
} |
36
|
|
|
|
37
|
44 |
|
if (is_array($identifiers)) { |
38
|
6 |
|
return self::createFromArray($identifiers); |
39
|
|
|
} |
40
|
|
|
|
41
|
38 |
|
if (is_string($identifiers)) { |
42
|
36 |
|
return self::createFromString($identifiers); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
throw new InvalidArgumentException('Identifiers parameter should be either array or string'); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
private static function createFromArray(array $identifiersArray) |
49
|
|
|
{ |
50
|
6 |
|
$identifiers = []; |
51
|
|
|
|
52
|
6 |
|
foreach ($identifiersArray as $id) { |
53
|
6 |
|
$identifiers[]= self::createIdentifier($id); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return new static(...$identifiers); |
57
|
|
|
} |
58
|
|
|
|
59
|
36 |
|
private static function createFromString(string $identifiersString) |
60
|
|
|
{ |
61
|
36 |
|
if (strpos($identifiersString, '.') !== false) { |
62
|
19 |
|
$identifiers = []; |
63
|
|
|
|
64
|
19 |
|
$ids = explode('.', $identifiersString); |
65
|
|
|
|
66
|
19 |
|
foreach ($ids as $id) { |
67
|
19 |
|
$identifiers[]= self::createIdentifier($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
18 |
|
return new static(...$identifiers); |
71
|
|
|
} |
72
|
|
|
|
73
|
22 |
|
return new static(...[self::createIdentifier($identifiersString)]); |
74
|
|
|
} |
75
|
|
|
|
76
|
42 |
|
protected static function createIdentifier($value) : Identifier |
77
|
|
|
{ |
78
|
42 |
|
if ($value instanceof Identifier) { |
79
|
2 |
|
return $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
42 |
|
return static::createAssociatedIdentifier($value); |
83
|
|
|
} |
84
|
|
|
|
85
|
74 |
|
public static function createEmpty() |
86
|
|
|
{ |
87
|
74 |
|
return new static(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected static function createAssociatedIdentifier(string $value) : Identifier |
91
|
|
|
{ |
92
|
|
|
throw new LogicException(__METHOD__ . ' not implemented'); |
93
|
|
|
} |
94
|
|
|
|
95
|
50 |
|
public function getIdentifiers() : array |
96
|
|
|
{ |
97
|
50 |
|
return $this->identifiers; |
98
|
|
|
} |
99
|
|
|
|
100
|
53 |
|
public function isEmpty() : bool |
101
|
|
|
{ |
102
|
53 |
|
return empty($this->identifiers); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function toArray() : array |
106
|
|
|
{ |
107
|
6 |
|
return array_map(function (Identifier $identifier) { |
108
|
5 |
|
return $identifier->getValue(); |
109
|
6 |
|
}, $this->identifiers); |
110
|
|
|
} |
111
|
|
|
|
112
|
16 |
|
public function __toString() : string |
113
|
|
|
{ |
114
|
16 |
|
return implode('.', $this->getIdentifiers()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|