Whip_VersionRequirement.php
3.62 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/
/**
* A value object containing a version requirement for a component version.
*/
class Whip_VersionRequirement implements Whip_Requirement {
/**
* The component name.
*
* @var string
*/
private $component;
/**
* The component version.
*
* @var string
*/
private $version;
/**
* The operator to use when comparing version.
*
* @var string
*/
private $operator;
/**
* Whip_Requirement constructor.
*
* @param string $component The component name.
* @param string $version The component version.
* @param string $operator The operator to use when comparing version.
*/
public function __construct( $component, $version, $operator = '=' ) {
$this->validateParameters( $component, $version, $operator );
$this->component = $component;
$this->version = $version;
$this->operator = $operator;
}
/**
* Retrieves the component name defined for the requirement.
*
* @return string The component name.
*/
public function component() {
return $this->component;
}
/**
* Gets the components version defined for the requirement.
*
* @return string
*/
public function version() {
return $this->version;
}
/**
* Gets the operator to use when comparing version numbers.
*
* @return string The comparison operator.
*/
public function operator() {
return $this->operator;
}
/**
* Creates a new version requirement from a comparison string.
*
* @param string $component The component for this version requirement.
* @param string $comparisonString The comparison string for this version requirement.
*
* @return Whip_VersionRequirement The parsed version requirement.
*
* @throws Whip_InvalidVersionComparisonString When an invalid version comparison string is passed.
*/
public static function fromCompareString( $component, $comparisonString ) {
$matcher = '`
(
>=? # Matches >= and >.
|
<=? # Matches <= and <.
)
([^>=<\s]+) # Matches anything except >, <, =, and whitespace.
`x';
if ( ! preg_match( $matcher, $comparisonString, $match ) ) {
throw new Whip_InvalidVersionComparisonString( $comparisonString );
}
$version = $match[2];
$operator = $match[1];
return new Whip_VersionRequirement( $component, $version, $operator );
}
/**
* Validates the parameters passed to the requirement.
*
* @param string $component The component name.
* @param string $version The component version.
* @param string $operator The operator to use when comparing version.
*
* @throws Whip_EmptyProperty When any of the parameters is empty.
* @throws Whip_InvalidOperatorType When the $operator parameter is invalid.
* @throws Whip_InvalidType When any of the parameters is not of the expected type.
*/
private function validateParameters( $component, $version, $operator ) {
if ( empty( $component ) ) {
throw new Whip_EmptyProperty( 'Component' );
}
if ( ! is_string( $component ) ) {
throw new Whip_InvalidType( 'Component', $component, 'string' );
}
if ( empty( $version ) ) {
throw new Whip_EmptyProperty( 'Version' );
}
if ( ! is_string( $version ) ) {
throw new Whip_InvalidType( 'Version', $version, 'string' );
}
if ( empty( $operator ) ) {
throw new Whip_EmptyProperty( 'Operator' );
}
if ( ! is_string( $operator ) ) {
throw new Whip_InvalidType( 'Operator', $operator, 'string' );
}
$validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );
if ( ! in_array( $operator, $validOperators, true ) ) {
throw new Whip_InvalidOperatorType( $operator, $validOperators );
}
}
}