Issues (126)

src/Version.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata;
6
7
/**
8
 * Class Version Type to represents the version number of data service and edmx.
9
 */
10
class Version
11
{
12
    /** @var Version[] */
13
    private static $fixedVersion = null;
14
    /**
15
     * The major component of the version.
16
     *
17
     * @var int
18
     */
19
    private $major;
20
    /**
21
     * The minor component of the version.
22
     *
23
     * @var int
24
     */
25
    private $minor;
26
27
    /**
28
     * Constructs a new instance of Version class.
29
     *
30
     * @param int $major The major component of the version
31
     * @param int $minor The minor component of the version
32
     */
33
    public function __construct($major, $minor)
34
    {
35
        $this->major = $major;
36
        $this->minor = $minor;
37
    }
38
39
    /**
40
     * @return Version
41
     */
42
    public static function v1()
43
    {
44
        self::fillVersions();
45
46
        return self::$fixedVersion[1];
47
    }
48
    /**
49
     * @return Version
50
     */
51
    public static function v1point1()
52
    {
53
        self::fillVersions();
54
55
        return self::$fixedVersion[11];
56
    }
57
58
    /**
59
     * @return Version
60
     */
61
    public static function v1point2()
62
    {
63
        self::fillVersions();
64
65
        return self::$fixedVersion[12];
66
    }
67
68
    private static function fillVersions()
69
    {
70
        if (null === self::$fixedVersion) {
0 ignored issues
show
The condition null === self::fixedVersion is always false.
Loading history...
71
            self::$fixedVersion = [
72
                1 => new self(1, 0),
73
                11 => new self(1, 1),
74
                12 => new self(1, 2),
75
                2 => new self(2, 0),
76
                3 => new self(3, 0)
77
            ];
78
        }
79
    }
80
81
    /**
82
     * @return Version
83
     */
84
    public static function v2()
85
    {
86
        self::fillVersions();
87
88
        return self::$fixedVersion[2];
89
    }
90
91
    /**
92
     * @return Version
93
     */
94
    public static function v3()
95
    {
96
        self::fillVersions();
97
98
        return self::$fixedVersion[3];
99
    }
100
101
    /**
102
     * Gets the major component of the version.
103
     *
104
     * @return int
105
     */
106
    public function getMajor()
107
    {
108
        return $this->major;
109
    }
110
111
    /**
112
     * Gets the minor component of the version.
113
     *
114
     * @return int
115
     */
116
    public function getMinor()
117
    {
118
        return $this->minor;
119
    }
120
121
    /**
122
     * If necessary raises version to the version given.
123
     *
124
     * @param int $major The major component of the new version
125
     * @param int $minor The minor component of the new version
126
     *
127
     * @return bool true if the version was raised, false otherwise
128
     */
129
    public function raiseVersion($major, $minor)
130
    {
131
        if ($major > $this->major) {
132
            $this->major = $major;
133
            $this->minor = $minor;
134
135
            return true;
136
        } elseif ($major == $this->major && $minor > $this->minor) {
137
            $this->minor = $minor;
138
139
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    /**
146
     * Compare this version with a target version.
147
     *
148
     * @param Version $targetVersion The target version to compare with
149
     *
150
     * @return int Return 1 if this version is greater than target version
151
     *             -1 if this version is less than the target version
152
     *             0 if both are equal
153
     */
154
    public function compare(Version $targetVersion)
155
    {
156
        if ($this->major > $targetVersion->major) {
157
            return 1;
158
        }
159
160
        if ($this->major == $targetVersion->major) {
161
            if ($this->minor == $targetVersion->minor) {
162
                return 0;
163
            }
164
165
            if ($this->minor > $targetVersion->minor) {
166
                return 1;
167
            }
168
        }
169
170
        return -1;
171
    }
172
173
    /**
174
     * Gets the value of the current Version object as string.
175
     *
176
     * @return string
177
     */
178
    public function toString()
179
    {
180
        return $this->major . '.' . $this->minor;
181
    }
182
}
183