Version::getLabel()   A
last analyzed

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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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;
13
14
use ptlis\SemanticVersion\Version\Label\Label;
15
use ptlis\SemanticVersion\Version\Label\LabelInterface;
16
17
/**
18
 * Value type for simple version numbers.
19
 */
20
final class Version implements VersionInterface
21
{
22
    /** @var int */
23
    private $major;
24
25
    /** @var int */
26
    private $minor = 0;
27
28
    /** @var int */
29
    private $patch = 0;
30
31
    /** @var LabelInterface */
32
    private $label;
33
34
35
    /**
36
     * Constructor.
37
     *
38
     * If no label is passed then an absent label type will be created & used.
39
     *
40
     * @param int $major
41
     * @param int $minor
42
     * @param int $patch
43
     * @param LabelInterface|null $label
44
     */
45 5
    public function __construct($major, $minor = 0, $patch = 0, LabelInterface $label = null)
46
    {
47 5
        if (is_null($label)) {
48 4
            $label = new Label(Label::PRECEDENCE_ABSENT);
49 4
        }
50
51 5
        $this->major = intval($major);
52 5
        $this->minor = intval($minor);
53 5
        $this->patch = intval($patch);
54 5
        $this->label = $label;
55 5
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 2
    public function isSatisfiedBy(VersionInterface $version)
61
    {
62 2
        return strval($this) === strval($version);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 2
    public function getMajor()
69
    {
70 2
        return $this->major;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 2
    public function getMinor()
77
    {
78 2
        return $this->minor;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 2
    public function getPatch()
85
    {
86 2
        return $this->patch;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 2
    public function getLabel()
93
    {
94 2
        return $this->label;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 4
    public function __toString()
101
    {
102 4
        $strVersion = implode(
103 4
            '.',
104
            [
105 4
                $this->major,
106 4
                $this->minor,
107 4
                $this->patch
108 4
            ]
109 4
        );
110
111
        // Only include the label portion if not absent
112 4
        if (LabelInterface::PRECEDENCE_ABSENT !== $this->label->getPrecedence()) {
113 1
            $strVersion .= '-' . $this->label;
114 1
        }
115
116 4
        return $strVersion;
117
    }
118
}
119