Completed
Push — master ( 1d4dc2...f35f34 )
by brian
01:54
created

LabelBuilder::buildFromTokens()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 32
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
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var int|null
28
     */
29
    private $version;
30
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param string $name
36
     * @param int|null $version
37
     */
38 9
    public function __construct($name = '', $version = null)
39
    {
40 9
        $this->name = $name;
41 9
        $this->version = $version;
42 9
    }
43
44
    /**
45
     * Set label name.
46
     *
47
     * @param string $name
48
     *
49
     * @return $this
50
     */
51 6
    public function setName($name)
52
    {
53 6
        return new LabelBuilder(
54 6
            $name,
55 6
            $this->version
56 6
        );
57
    }
58
59
    /**
60
     * Set label version.
61
     *
62
     * @param int|null $version
63
     *
64
     * @return $this
65
     */
66 4
    public function setVersion($version)
67
    {
68 4
        return new LabelBuilder(
69 4
            $this->name,
70
            $version
71 4
        );
72
    }
73
74
    /**
75
     * Build a label from the provided specification
76
     *
77
     * @return LabelInterface
78
     */
79 9
    public function build()
80
    {
81
        $labelMap = [
82 9
            'alpha' => Label::PRECEDENCE_ALPHA,
83 9
            'beta' => Label::PRECEDENCE_BETA,
84
            'rc' => Label::PRECEDENCE_RC
85 9
        ];
86
87
        // No Label present or a dev label (these are a special-case for packagist - a version like 1.0.x-dev is
88
        // equivalent to 1.0.* in  conventional notation - there is additional semantics attached to this but it's not
89
        // 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, $labelMap)) {
95 5
            $label = new Label($labelMap[$this->name], $this->version);
96
97
        // Anything else is a miscellaneous 'dev' label
98 5
        } else {
99 1
            $label = new Label(Label::PRECEDENCE_DEV, $this->version, $this->name);
100
        }
101
102 9
        return $label;
103
    }
104
105
    /**
106
     * Build a label from a token list.
107
     *
108
     * @param Token[] $labelTokenList
109
     *
110
     * @return LabelInterface|null
111
     */
112 4
    public function buildFromTokens(array $labelTokenList)
113
    {
114 4
        $label = $this->build();
115
116 4
        switch (count($labelTokenList)) {
1 ignored issue
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
117
118
            // No label
119 4
            case 0:
120
                // Do Nothing
121 1
                break;
122
123
            // Version string part only
124 3
            case 1:
125 1
                $label = $this
126 1
                    ->setName($labelTokenList[0]->getValue())
127 1
                    ->build();
128 1
                break;
129
130
            // Label version
131 2
            case 3:
132 1
                $label = $this
133 1
                    ->setName($labelTokenList[0]->getValue())
134 1
                    ->setVersion($labelTokenList[2]->getValue())
135 1
                    ->build();
136 1
                break;
137
138 1
            default:
139 1
                throw new \RuntimeException('Invalid version');
140 4
        }
141
142 3
        return $label;
143
    }
144
}
145