LabelBuilder::buildFromTokens()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 4
nop 1
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 precedences */
28
    private $labelPrecedenceMap = [
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 9
    public function __construct($name = '', $version = null)
42
    {
43 9
        $this->name = $name;
44 9
        $this->version = $version;
45 9
    }
46
47
    /**
48
     * Set label name.
49
     *
50
     * @param string $name
51
     *
52
     * @return $this
53
     */
54 6
    public function setName($name)
55
    {
56 6
        return new LabelBuilder(
57 6
            $name,
58 6
            $this->version
59 6
        );
60
    }
61
62
    /**
63
     * Set label version.
64
     *
65
     * @param int|null $version
66
     *
67
     * @return $this
68
     */
69 4
    public function setVersion($version)
70
    {
71 4
        return new LabelBuilder(
72 4
            $this->name,
73
            $version
74 4
        );
75
    }
76
77
    /**
78
     * Build a label from the provided specification
79
     *
80
     * @return LabelInterface
81
     */
82 9
    public function build()
83
    {
84
        // Default to miscellaneous 'dev' label
85 9
        $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 9
        if (!strlen($this->name) || 'dev' === $this->name) {
91 5
            $label = new Label(Label::PRECEDENCE_ABSENT);
92
93
        // Alpha, Beta & RC standard labels
94 9
        } elseif (array_key_exists($this->name, $this->labelPrecedenceMap)) {
95 5
            $label = new Label($this->labelPrecedenceMap[$this->name], $this->version);
96 5
        }
97
98 9
        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 4
    public function buildFromTokens(array $labelTokenList)
109
    {
110 4
        $label = $this->build();
111
112 4
        switch (count($labelTokenList)) {
113
            // No label
114 4
            case 0:
115
                // Do Nothing
116 1
                break;
117
118
            // Version string part only
119 3
            case 1:
120 1
                $label = $this
121 1
                    ->setName($labelTokenList[0]->getValue())
122 1
                    ->build();
123 1
                break;
124
125
            // Label version
126 2
            case 3:
127 1
                $label = $this
128 1
                    ->setName($labelTokenList[0]->getValue())
129 1
                    ->setVersion($labelTokenList[2]->getValue())
130 1
                    ->build();
131 1
                break;
132
133 1
            default:
134 1
                throw new \RuntimeException('Invalid version');
135 4
        }
136
137 3
        return $label;
138
    }
139
}
140