Completed
Push — master ( 581a9c...240c2a )
by Nikola
02:24
created

BaseIdentifyingMetadata::isEmpty()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
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 98
    private function __construct(array $identifiers = [])
29
    {
30 98
        $this->identifiers = $identifiers;
31 98
    }
32
33
    /**
34
     * @param array|string $identifiers
35
     * @return static
36
     * @throws InvalidArgumentException
37
     */
38 51
    public static function create($identifiers)
39
    {
40 51
        if (is_array($identifiers)) {
41 6
            return self::createFromArray($identifiers);
42 45
        } elseif (is_string($identifiers)) {
43 45
            return self::createFromString($identifiers);
44
        }
45
46
        throw new InvalidArgumentException('Identifiers parameter should be either array or string');
47
    }
48
49 6
    private static function createFromArray(array $identifiersArray)
50
    {
51 6
        $identifiers = [];
52
53 6
        foreach ($identifiersArray as $id) {
54 6
            $identifiers[]= self::createIdentifier($id);
55 4
        }
56
57 4
        return new static($identifiers);
58
    }
59
60 45
    private static function createFromString($identifiersString)
61
    {
62 45
        if (strpos($identifiersString, '.') !== false) {
63 26
            $identifiers = [];
64
65 26
            $ids = explode('.', $identifiersString);
66
67 26
            foreach ($ids as $id) {
68 26
                $identifiers[]= self::createIdentifier($id);
69 26
            }
70
71 25
            return new static($identifiers);
72
        }
73
74 28
        return new static([self::createIdentifier($identifiersString)]);
75
    }
76
77 51
    private static function createIdentifier($value)
78
    {
79 51
        if ($value instanceof Identifier) {
80 2
            return $value;
81
        }
82
83 51
        return static::createAssociatedIdentifier($value);
84
    }
85
86
    /**
87
     * @return static
88
     */
89 81
    public static function createEmpty()
90
    {
91 81
        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 46
    public function getIdentifiers()
107
    {
108 46
        return $this->identifiers;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 63
    public function isEmpty()
115
    {
116 63
        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 24
    public function __toString()
133
    {
134 24
        return implode('.', $this->getIdentifiers());
135
    }
136
}
137