generated from ergebnis/php-cs-fixer-config-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPhpVersion.php
118 lines (100 loc) · 2.64 KB
/
PhpVersion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
/**
* Copyright (c) 2019-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/
namespace Ergebnis\PhpCsFixer\Config;
final class PhpVersion
{
private PhpVersion\Major $major;
private PhpVersion\Minor $minor;
private PhpVersion\Patch $patch;
private function __construct(
PhpVersion\Major $major,
PhpVersion\Minor $minor,
PhpVersion\Patch $patch
) {
$this->major = $major;
$this->minor = $minor;
$this->patch = $patch;
}
public static function create(
PhpVersion\Major $major,
PhpVersion\Minor $minor,
PhpVersion\Patch $patch
): self {
return new self(
$major,
$minor,
$patch,
);
}
public static function current(): self
{
return new self(
PhpVersion\Major::fromInt(\PHP_MAJOR_VERSION),
PhpVersion\Minor::fromInt(\PHP_MINOR_VERSION),
PhpVersion\Patch::fromInt(\PHP_RELEASE_VERSION),
);
}
/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be greater than or equal to 0, but %d is not.',
$value,
));
}
$major = \intdiv(
$value,
10_000,
);
$minor = \intdiv(
$value - $major * 10_000,
100,
);
$patch = $value - $major * 10_000 - $minor * 100;
return new self(
PhpVersion\Major::fromInt($major),
PhpVersion\Minor::fromInt($minor),
PhpVersion\Patch::fromInt($patch),
);
}
public function major(): PhpVersion\Major
{
return $this->major;
}
public function minor(): PhpVersion\Minor
{
return $this->minor;
}
public function patch(): PhpVersion\Patch
{
return $this->patch;
}
public function toInt(): int
{
return $this->major->toInt() * 10_000 + $this->minor->toInt() * 100 + $this->patch->toInt();
}
public function toString(): string
{
return \sprintf(
'%d.%d.%d',
$this->major->toInt(),
$this->minor->toInt(),
$this->patch->toInt(),
);
}
public function isSmallerThan(self $other): bool
{
return $this->toInt() < $other->toInt();
}
}