1010 */
1111class SemanticVersion
1212{
13- private static $ REGEX = '/^(?<major>0|[1-9]\d*)(\.(?<minor>0|[1-9]\d*))?(\.(?<patch>0|[1-9]\d*))?(\-(?<prerel>[0-9A-Za-z\-\.]+))?(\+(?<build>[0-9A-Za-z\-\.]+))?$/ ' ;
13+ private static $ REGEX = '/^(?<major>0|[1-9]\d*)(\.(?<minor>0|[1-9]\d*))?(\.(?<patch>0|[1-9]\d*))?(\-(?<prerel>[0-9A-Za-z\-\.]+))?(\+(?<build>[0-9A-Za-z\-\.]+))?$/ ' ;
1414
15- /** @var int */
15+ /** @var int */
1616 public $ major ;
1717 /** @var int */
1818 public $ minor ;
@@ -25,11 +25,11 @@ class SemanticVersion
2525
2626 public function __construct ($ major , $ minor , $ patch , $ prerelease , $ build )
2727 {
28- $ this ->major = $ major ;
29- $ this ->minor = $ minor ;
30- $ this ->patch = $ patch ;
31- $ this ->prerelease = $ prerelease ;
32- $ this ->build = $ build ;
28+ $ this ->major = $ major ;
29+ $ this ->minor = $ minor ;
30+ $ this ->patch = $ patch ;
31+ $ this ->prerelease = $ prerelease ;
32+ $ this ->build = $ build ;
3333 }
3434
3535 /**
@@ -41,18 +41,18 @@ public function __construct($major, $minor, $patch, $prerelease, $build)
4141 */
4242 public static function parse ($ input , $ loose = false )
4343 {
44- if (!preg_match (self ::$ REGEX , $ input , $ matches )) {
45- throw new \InvalidArgumentException ("not a valid semantic version " );
46- }
47- $ major = intval ($ matches ['major ' ]);
48- if (!$ loose && (!array_key_exists ('minor ' , $ matches ) || !array_key_exists ('patch ' , $ matches ))) {
49- throw new \InvalidArgumentException ("not a valid semantic version: minor and patch versions are required " );
50- }
51- $ minor = array_key_exists ('minor ' , $ matches ) ? intval ($ matches ['minor ' ]) : 0 ;
52- $ patch = array_key_exists ('patch ' , $ matches ) ? intval ($ matches ['patch ' ]) : 0 ;
53- $ prerelease = array_key_exists ('prerel ' , $ matches ) ? $ matches ['prerel ' ] : '' ;
54- $ build = array_key_exists ('build ' , $ matches ) ? $ matches ['build ' ] : '' ;
55- return new SemanticVersion ($ major , $ minor , $ patch , $ prerelease , $ build );
44+ if (!preg_match (self ::$ REGEX , $ input , $ matches )) {
45+ throw new \InvalidArgumentException ("not a valid semantic version " );
46+ }
47+ $ major = intval ($ matches ['major ' ]);
48+ if (!$ loose && (!array_key_exists ('minor ' , $ matches ) || !array_key_exists ('patch ' , $ matches ))) {
49+ throw new \InvalidArgumentException ("not a valid semantic version: minor and patch versions are required " );
50+ }
51+ $ minor = array_key_exists ('minor ' , $ matches ) ? intval ($ matches ['minor ' ]) : 0 ;
52+ $ patch = array_key_exists ('patch ' , $ matches ) ? intval ($ matches ['patch ' ]) : 0 ;
53+ $ prerelease = array_key_exists ('prerel ' , $ matches ) ? $ matches ['prerel ' ] : '' ;
54+ $ build = array_key_exists ('build ' , $ matches ) ? $ matches ['build ' ] : '' ;
55+ return new SemanticVersion ($ major , $ minor , $ patch , $ prerelease , $ build );
5656 }
5757
5858 /**
@@ -63,59 +63,59 @@ public static function parse($input, $loose = false)
6363 */
6464 public function comparePrecedence ($ other )
6565 {
66- if ($ this ->major != $ other ->major ) {
67- return ($ this ->major < $ other ->major ) ? -1 : 1 ;
68- }
69- if ($ this ->minor != $ other ->minor ) {
70- return ($ this ->minor < $ other ->minor ) ? -1 : 1 ;
71- }
72- if ($ this ->patch != $ other ->patch ) {
73- return ($ this ->patch < $ other ->patch ) ? -1 : 1 ;
74- }
75- if ($ this ->prerelease != $ other ->prerelease ) {
76- // *no* prerelease component always has a higher precedence than *any* prerelease component
77- if ($ this ->prerelease == '' ) {
78- return 1 ;
79- }
80- if ($ other ->prerelease == '' ) {
81- return -1 ;
82- }
83- return self ::compareIdentifiers (explode ('. ' , $ this ->prerelease ), explode ('. ' , $ other ->prerelease ));
84- }
85- // build metadata is always ignored in precedence comparison
86- return 0 ;
66+ if ($ this ->major != $ other ->major ) {
67+ return ($ this ->major < $ other ->major ) ? -1 : 1 ;
68+ }
69+ if ($ this ->minor != $ other ->minor ) {
70+ return ($ this ->minor < $ other ->minor ) ? -1 : 1 ;
71+ }
72+ if ($ this ->patch != $ other ->patch ) {
73+ return ($ this ->patch < $ other ->patch ) ? -1 : 1 ;
74+ }
75+ if ($ this ->prerelease != $ other ->prerelease ) {
76+ // *no* prerelease component always has a higher precedence than *any* prerelease component
77+ if ($ this ->prerelease == '' ) {
78+ return 1 ;
79+ }
80+ if ($ other ->prerelease == '' ) {
81+ return -1 ;
82+ }
83+ return self ::compareIdentifiers (explode ('. ' , $ this ->prerelease ), explode ('. ' , $ other ->prerelease ));
84+ }
85+ // build metadata is always ignored in precedence comparison
86+ return 0 ;
8787 }
8888
8989 private static function compareIdentifiers ($ ids1 , $ ids2 )
9090 {
91- for ($ i = 0 ; ; $ i ++) {
92- if ($ i >= count ($ ids1 )) {
93- // x.y is always less than x.y.z
94- return ($ i >= count ($ ids2 )) ? 0 : -1 ;
95- }
96- if ($ i >= count ($ ids2 )) {
97- return 1 ;
98- }
99- $ v1 = $ ids1 [$ i ];
100- $ v2 = $ ids2 [$ i ];
101- // each sub-identifier is compared numerically if both are numeric; if both are non-numeric,
102- // they're compared as strings; otherwise, the numeric one is the lesser one
103- $ isNum1 = is_numeric ($ v1 );
104- $ isNum2 = is_numeric ($ v2 );
105- if ($ isNum1 && $ isNum2 ) {
106- $ n1 = intval ($ v1 );
107- $ n2 = intval ($ v2 );
108- $ d = ($ n1 == $ n2 ) ? 0 : (($ n1 < $ n2 ) ? -1 : 1 );
109- } else {
110- if ($ isNum1 || $ isNum2 ) {
111- $ d = $ isNum1 ? -1 : 1 ;
112- } else {
113- $ d = ($ v1 == $ v2 ) ? 0 : (($ v1 < $ v2 ) ? -1 : 1 );
114- }
115- }
116- if ($ d != 0 ) {
117- return $ d ;
118- }
119- }
91+ for ($ i = 0 ; ; $ i ++) {
92+ if ($ i >= count ($ ids1 )) {
93+ // x.y is always less than x.y.z
94+ return ($ i >= count ($ ids2 )) ? 0 : -1 ;
95+ }
96+ if ($ i >= count ($ ids2 )) {
97+ return 1 ;
98+ }
99+ $ v1 = $ ids1 [$ i ];
100+ $ v2 = $ ids2 [$ i ];
101+ // each sub-identifier is compared numerically if both are numeric; if both are non-numeric,
102+ // they're compared as strings; otherwise, the numeric one is the lesser one
103+ $ isNum1 = is_numeric ($ v1 );
104+ $ isNum2 = is_numeric ($ v2 );
105+ if ($ isNum1 && $ isNum2 ) {
106+ $ n1 = intval ($ v1 );
107+ $ n2 = intval ($ v2 );
108+ $ d = ($ n1 == $ n2 ) ? 0 : (($ n1 < $ n2 ) ? -1 : 1 );
109+ } else {
110+ if ($ isNum1 || $ isNum2 ) {
111+ $ d = $ isNum1 ? -1 : 1 ;
112+ } else {
113+ $ d = ($ v1 == $ v2 ) ? 0 : (($ v1 < $ v2 ) ? -1 : 1 );
114+ }
115+ }
116+ if ($ d != 0 ) {
117+ return $ d ;
118+ }
119+ }
120120 }
121121}
0 commit comments