steevanb / version-comparator
Compare PHP version easier than with version_compare()
0.2.0
2021-10-23 22:57 UTC
Requires
- php: ^5.6 || ^7.0 || ^8.0
Requires (Dev)
- ext-simplexml: *
- phpunit/phpunit: 9.5.*
- steevanb/php-backtrace: 2.1.*
- symfony/var-dumper: 5.3.*
This package is auto-updated.
Last update: 2024-12-24 06:17:22 UTC
README
steevanb/version-comparator
Add classes to compare versions easier than with compare_version()
and PHP_VERSION
.
Installation
composer require steevanb/version-comparator ^0.2
Compare with current PHP version
You can compare current PHP version with 2 versions to know if current php version is between this 2 versions:
PhpVersionComparator::isBetween('8', '9'); // return true is PHP is >= 8.0.0 and < 9.0.0 PhpVersionComparator::isBetween('8.0', '8.1'); // return true is PHP is >= 8.0.0 and < 8.1.0 PhpVersionComparator::isBetween('8.0.0', '8.0.2'); // return true is PHP is >= 8.0.0 and < 8.0.2
Shortcuts for each PHP major and minor version
PhpVersionComparator
have a shortcut who call isBetween()
for each major and minor version:
// For PHP 5.3 to 5.6 PhpVersionComparator::isPhp5(); // return true is PHP is >= 5.0.0 and < 6.0.0 PhpVersionComparator::isPhp53(); // return true is PHP is >= 5.3.0 and < 5.4.0 PhpVersionComparator::isPhp54(); // return true is PHP is >= 5.4.0 and < 5.5.0 PhpVersionComparator::isPhp55(); // return true is PHP is >= 5.5.0 and < 5.6.0 PhpVersionComparator::isPhp56(); // return true is PHP is >= 5.6.0 and < 5.7.0 // For PHP 7.0 to 7.4 PhpVersionComparator::isPhp7(); // return true is PHP is >= 7.0.0 and < 8.0.0 PhpVersionComparator::isPhp70(); // return true is PHP is >= 7.0.0 and < 7.1.0 PhpVersionComparator::isPhp71(); // return true is PHP is >= 7.1.0 and < 7.2.0 PhpVersionComparator::isPhp72(); // return true is PHP is >= 7.2.0 and < 7.3.0 PhpVersionComparator::isPhp73(); // return true is PHP is >= 7.3.0 and < 7.4.0 PhpVersionComparator::isPhp74(); // return true is PHP is >= 7.4.0 and < 7.5.0 // For PHP 8.0 to latest 8.x PhpVersionComparator::isPhp8(); // return true is PHP is >= 8.0.0 and < 9.0.0 PhpVersionComparator::isPhp80(); // return true is PHP is >= 8.0.0 and < 8.1.0 PhpVersionComparator::isPhp81(); // return true is PHP is >= 8.1.0 and < 8.2.0
Exception instead of returning the result
If you want to throw an exception instead of returning the result,
each method have it's assert()
version who throw a VersionIsNotBetweenException
exception:
PhpVersionComparator::assertIsBetween('8', '9'); PhpVersionComparator::assertIsPhp5(); // etc