Completed
Push — master ( 9fd723...978a62 )
by Nikola
02:35
created

BaseIdentifyingMetadata::getIdentifiers()   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
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Version package.
5
 *
6
 * Copyright (c) Nikola Posa <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Version\Metadata;
13
14
use Version\Identifier\Identifier;
15
use Version\Exception\InvalidArgumentException;
16
use Version\Exception\LogicException;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
abstract class BaseIdentifyingMetadata
22
{
23
    /**
24
     * @var Identifier[]
25
     */
26
    private $identifiers;
27
28 95
    private function __construct(array $identifiers = [])
29
    {
30 95
        $this->identifiers = $identifiers;
31 95
    }
32
33
    /**
34
     * @param array|string $identifiers
35
     * @return static
36
     * @throws InvalidArgumentException
37
     */
38 45
    public static function create($identifiers)
39
    {
40 45
        if (is_array($identifiers)) {
41 7
            return self::createFromArray($identifiers);
42 38
        } elseif (is_string($identifiers)) {
43 36
            return self::createFromString($identifiers);
44
        }
45
46 2
        throw new InvalidArgumentException('Identifiers parameter should be either array or string');
47
    }
48
49 7
    private static function createFromArray(array $identifiersArray)
50
    {
51 7
        $identifiers = [];
52
53 7
        foreach ($identifiersArray as $id) {
54 7
            $identifiers[]= self::createIdentifier($id);
55
        }
56
57 4
        return new static($identifiers);
58
    }
59
60 36
    private static function createFromString($identifiersString)
61
    {
62 36
        if (strpos($identifiersString, '.') !== false) {
63 19
            $identifiers = [];
64
65 19
            $ids = explode('.', $identifiersString);
66
67 19
            foreach ($ids as $id) {
68 19
                $identifiers[]= self::createIdentifier($id);
69
            }
70
71 18
            return new static($identifiers);
72
        }
73
74 22
        return new static([self::createIdentifier($identifiersString)]);
75
    }
76
77 43
    private static function createIdentifier($value)
78
    {
79 43
        if ($value instanceof Identifier) {
80 2
            return $value;
81
        }
82
83 43
        return static::createAssociatedIdentifier($value);
84
    }
85
86
    /**
87
     * @return static
88
     */
89 79
    public static function createEmpty()
90
    {
91 79
        return new static([]);
92
    }
93
94
    /**
95
     * @param string $value
96
     * @return Identifier
97
     */
98
    protected static function createAssociatedIdentifier($value)
99
    {
100
        throw new LogicException(__METHOD__ . ' not implemented');
101
    }
102
103
    /**
104
     * @return array
105
     */
106 50
    public function getIdentifiers()
107
    {
108 50
        return $this->identifiers;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 55
    public function isEmpty()
115
    {
116 55
        return empty($this->identifiers);
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function toArray()
123
    {
124 6
        return array_map(function ($identifier) {
125 5
            return $identifier->getValue();
126 6
        }, $this->identifiers);
127
    }
128
129
    /**
130
     * @return string
131
     */
132 16
    public function __toString()
133
    {
134 16
        return implode('.', $this->getIdentifiers());
135
    }
136
}
137