Completed
Push — master ( c8bd50...a6df06 )
by brian
01:58
created

LabelBuilder::build()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 0
crap 4
1
<?php
2
3
/**
4
 * @copyright   (c) 2014-2017 brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ptlis\SemanticVersion\Version\Label;
13
14
use ptlis\SemanticVersion\Parse\Token;
15
16
/**
17
 * Immutable builder for label instances.
18
 */
19
final class LabelBuilder
20
{
21
    /** @var string */
22
    private $name;
23
24
    /** @var int|null */
25
    private $version;
26
27
    /** @var array Map of string encoded labels to label constants */
28
    private $labelMap = [
29
        'alpha' => Label::PRECEDENCE_ALPHA,
30
        'beta' => Label::PRECEDENCE_BETA,
31
        'rc' => Label::PRECEDENCE_RC
32
    ];
33
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param string $name
39
     * @param int|null $version
40
     */
41 17
    public function __construct($name = '', $version = null)
42
    {
43 17
        $this->name = $name;
44 17
        $this->version = $version;
45 17
    }
46
47
    /**
48
     * Set label name.
49
     *
50
     * @param string $name
51
     *
52
     * @return $this
53
     */
54 8
    public function setName($name)
55
    {
56 8
        return new LabelBuilder(
57
            $name,
58 8
            $this->version
59
        );
60
    }
61
62
    /**
63
     * Set label version.
64
     *
65
     * @param int|null $version
66
     *
67
     * @return $this
68
     */
69 5
    public function setVersion($version)
70
    {
71 5
        return new LabelBuilder(
72 5
            $this->name,
73
            $version
74
        );
75
    }
76
77
    /**
78
     * Build a label from the provided specification
79
     *
80
     * @return LabelInterface
81
     */
82 11
    public function build()
83
    {
84
        // Default to miscellaneous 'dev' label
85 11
        $label = new Label(Label::PRECEDENCE_DEV, $this->version, $this->name);
86
87
        // No Label present or a dev label (these are a special-case for packagist branch versions - a version like
88
        // 1.0.x-dev is equivalent to 1.0.* in  conventional notation - there are additional semantics attached to this
89
        // but they're not important for our purposes
90 11
        if (!strlen($this->name) || 'dev' === $this->name) {
91 7
            $label = new Label(Label::PRECEDENCE_ABSENT);
92
93
        // Alpha, Beta & RC standard labels
94 8
        } elseif (array_key_exists($this->name, $this->labelMap)) {
95 7
            $label = new Label($this->labelMap[$this->name], $this->version);
96
        }
97
98 11
        return $label;
99
    }
100
101
    /**
102
     * Build a label from a token list.
103
     *
104
     * @param Token[] $labelTokenList
105
     *
106
     * @return LabelInterface|null
107
     */
108 6
    public function buildFromTokens(array $labelTokenList)
109
    {
110 6
        $label = $this->build();
111
112 6
        switch (count($labelTokenList)) {
113
114
            // No label
115 6
            case 0:
116
                // Do Nothing
117 1
                break;
118
119
            // Version string part only
120 5
            case 1:
121
                $label = $this
122 2
                    ->setName($labelTokenList[0]->getValue())
123 2
                    ->build();
124 2
                break;
125
126
            // Label version
127 3
            case 3:
128
                $label = $this
129 2
                    ->setName($labelTokenList[0]->getValue())
130 2
                    ->setVersion($labelTokenList[2]->getValue())
131 2
                    ->build();
132 2
                break;
133
134
            default:
135 1
                throw new \RuntimeException('Invalid version');
136
        }
137
138 5
        return $label;
139
    }
140
}
141