Completed
Branch TASK/11208/update-bundled-gate... (60edbd)
by
unknown
12:10
created

Version::olderThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\values;
4
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
use InvalidArgumentException;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit;
9
10
11
12
/**
13
 * Class Version
14
 * Value Object representing a valid PHP version string for an Event Espresso product
15
 *
16
 * @package EventEspresso\core\domain\values
17
 * @author  Brent Christensen
18
 * @since   4.9.51
19
 */
20
class Version
21
{
22
23
    const RELEASE_TYPE_RC    = 'rc';
24
25
    const RELEASE_TYPE_BETA  = 'beta';
26
27
    const RELEASE_TYPE_DECAF = 'decaf';
28
29
    const RELEASE_TYPE_PROD  = 'p';
30
31
    /**
32
     * @var int $major
33
     */
34
    private $major;
35
36
    /**
37
     * @var int $minor
38
     */
39
    private $minor;
40
41
    /**
42
     * @var int $patch
43
     */
44
    private $patch;
45
46
    /**
47
     * @var string $release
48
     */
49
    private $release;
50
51
    /**
52
     * @var int $build
53
     */
54
    private $build;
55
56
57
    /**
58
     * Version constructor.
59
     *
60
     * @param int    $major
61
     * @param int    $minor
62
     * @param int    $patch
63
     * @param string $release
64
     * @param int    $build
65
     * @throws InvalidDataTypeException
66
     * @throws InvalidArgumentException
67
     */
68
    public function __construct($major, $minor, $patch, $release = Version::RELEASE_TYPE_PROD, $build = 0)
69
    {
70
        $this->setMajor($major);
71
        $this->setMinor($minor);
72
        $this->setPatch($patch);
73
        $this->setRelease($release);
74
        $this->setBuild($build);
75
    }
76
77
78
    /**
79
     * @param string $version_string
80
     * @return Version
81
     * @throws InvalidArgumentException
82
     */
83
    public static function fromString($version_string)
84
    {
85
        // compare incoming version string against the lowest possible valid version
86
        if (version_compare($version_string, '0.0.1.dev.001', '<')) {
87
            throw new InvalidArgumentException(
88
                sprintf(
89
                    esc_html__('"%1$s" is not a valid version string', 'event_espresso'),
90
                    $version_string
91
                )
92
            );
93
        }
94
        // break apart incoming version string
95
        $version_parts = explode('.', $version_string);
96
        // verify that version string at least contains {major}.{minor}.{patch}
97
        if (count($version_parts) < 3) {
98
            throw new InvalidArgumentException(
99
                sprintf(
100
                    esc_html__(
101
                        'At minimum, a version string needs to be in a "{major}.{minor}.{patch}" format, therefore "%1$s" is not valid',
102
                        'event_espresso'
103
                    ),
104
                    $version_string
105
                )
106
            );
107
        }
108
        // add defaults for missing pieces
109
        $version_parts += array(0,0,0,'p',0);
110
        // reassign to individual variables
111
        list($major, $minor, $patch, $release, $build) = $version_parts;
112
        return new Version(
113
            (int) $major,
114
            (int) $minor,
115
            (int) $patch,
116
            $release,
117
            (int) $build
118
        );
119
    }
120
121
122
    /**
123
     * @return int
124
     */
125
    public function major()
126
    {
127
        return $this->major;
128
    }
129
130
131
    /**
132
     * @param int|string $major
133
     * @throws InvalidDataTypeException
134
     */
135 View Code Duplication
    private function setMajor($major)
136
    {
137
        if (! is_int($major)) {
138
            throw new InvalidDataTypeException(
139
                '$major',
140
                $major,
141
                'integer'
142
            );
143
        }
144
        $this->major = absint($major);
145
    }
146
147
148
    /**
149
     * @return int
150
     */
151
    public function minor()
152
    {
153
        return $this->minor;
154
    }
155
156
157
    /**
158
     * @param int|string $minor
159
     * @throws InvalidDataTypeException
160
     */
161 View Code Duplication
    private function setMinor($minor)
162
    {
163
        if (! is_int($minor)) {
164
            throw new InvalidDataTypeException(
165
                '$minor',
166
                $minor,
167
                'integer'
168
            );
169
        }
170
        $this->minor = absint($minor);
171
    }
172
173
174
    /**
175
     * @return int
176
     */
177
    public function patch()
178
    {
179
        return $this->patch;
180
    }
181
182
183
    /**
184
     * @param int|string $patch
185
     * @throws InvalidDataTypeException
186
     */
187 View Code Duplication
    private function setPatch($patch)
188
    {
189
        if (! is_int($patch)) {
190
            throw new InvalidDataTypeException(
191
                '$patch',
192
                $patch,
193
                'integer'
194
            );
195
        }
196
        $this->patch = absint($patch);
197
    }
198
199
200
    /**
201
     * @return string
202
     */
203
    public function release()
204
    {
205
        return $this->release;
206
    }
207
208
209
    /**
210
     * @param string $release
211
     * @throws InvalidArgumentException
212
     */
213
    private function setRelease($release)
214
    {
215
        $valid_release_types = array(
216
            Version::RELEASE_TYPE_RC,
217
            Version::RELEASE_TYPE_BETA,
218
            Version::RELEASE_TYPE_DECAF,
219
            Version::RELEASE_TYPE_PROD,
220
        );
221 View Code Duplication
        if (! in_array($release, $valid_release_types, true)) {
222
            throw new InvalidArgumentException(
223
                sprintf(
224
                    esc_html__(
225
                        '"%1$s" is not a valid release type. Please use one of the following values: %2$s',
226
                        'event_espresso'
227
                    ),
228
                    $release,
229
                    implode(', ', $valid_release_types)
230
                )
231
            );
232
        }
233
        $this->release = $release;
234
    }
235
236
237
    /**
238
     * @return int
239
     */
240
    public function build()
241
    {
242
        return $this->build;
243
    }
244
245
246
    /**
247
     * @param int|string $build
248
     * @throws InvalidDataTypeException
249
     */
250 View Code Duplication
    private function setBuild($build)
251
    {
252
        if (! is_int($build)) {
253
            throw new InvalidDataTypeException(
254
                '$build',
255
                $build,
256
                'integer'
257
            );
258
        }
259
        $this->build = absint($build);
260
    }
261
262
263
    /**
264
     * @param Version $other_version
265
     * @return int
266
     */
267
    public function compare(Version $other_version)
268
    {
269
        return version_compare((string) $this, (string) $other_version);
270
    }
271
272
273
    /**
274
     * @param Version $other_version
275
     * @return bool
276
     */
277
    public function equals(Version $other_version)
278
    {
279
        return version_compare((string) $this, (string) $other_version, '==');
280
    }
281
282
283
    /**
284
     * @param Version $other_version
285
     * @return bool
286
     */
287
    public function newerThan(Version $other_version)
288
    {
289
        return version_compare((string) $this, (string) $other_version, '>');
290
    }
291
292
293
    /**
294
     * @param Version $other_version
295
     * @return bool
296
     */
297
    public function olderThan(Version $other_version)
298
    {
299
        return version_compare((string) $this, (string) $other_version, '<');
300
    }
301
302
303
    /**
304
     * @return string
305
     */
306
    public function __toString()
307
    {
308
        $version_string = "{$this->major}.{$this->minor}.{$this->patch}.{$this->release}";
309
        if($this->release !== Version::RELEASE_TYPE_PROD && $this->release !== Version::RELEASE_TYPE_DECAF) {
310
            $version_string .= '.' . str_pad($this->build, 3, '0', STR_PAD_LEFT);
311
        }
312
        return $version_string;
313
    }
314
315
316
317
}
318
// Location: Version.php
319