diff --git a/.travis.yml b/.travis.yml index c710f3d..eff6130 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,13 +4,14 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 before_install: - sudo locale-gen de_DE.UTF-8 es_ES.UTF-8 en_GB.UTF-8 en_US.UTF-8 fr_FR.UTF-8 it_IT.UTF-8 pl_PL.UTF-8 ru_RU.UTF-8 - composer global require hirak/prestissimo install: - - travis_wait 30 composer install -vvv +- travis_wait 30 composer install script: - php ./vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md index ac3989e..03351e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Common and useful classes, methods, exceptions etc. +# 0.1.1 + +1. TravisCI > run using PHP 7.2 too +2. ValueObject > class Version > represents version of software +3. Move version of this package to `VERSION` file (from `composer.json` file) + # 0.1.0 1. Composer > support/require PHP 5.6+ (instead of 5.5.9+) diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..17e51c3 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.1 diff --git a/composer.json b/composer.json index 6dfa257..b1d2418 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,6 @@ "name": "meritoo/common-library", "description": "Useful classes, methods, extensions etc.", "license": "MIT", - "version": "0.1.0", "authors": [ { "name": "Meritoo.pl", diff --git a/src/ValueObject/Version.php b/src/ValueObject/Version.php new file mode 100644 index 0000000..8684da0 --- /dev/null +++ b/src/ValueObject/Version.php @@ -0,0 +1,179 @@ + + * @copyright Meritoo + */ +class Version +{ + /** + * The "major" part. + * Incremented when you make incompatible API changes. + * + * @var int + */ + private $majorPart; + + /** + * The "minor" part. + * Incremented when you add functionality in a backwards-compatible manner. + * + * @var int + */ + private $minorPart; + + /** + * The "patch" part. + * Incremented when you make backwards-compatible bug fixes. + * + * @var int + */ + private $patchPart; + + /** + * Class constructor + * + * @param int $majorPart The "major" part. Incremented when you make incompatible API changes. + * @param int $minorPart The "minor" part. Incremented when you add functionality in a backwards-compatible manner. + * @param int $patchPart The "patch" part. Incremented when you make backwards-compatible bug fixes. + */ + public function __construct($majorPart, $minorPart, $patchPart) + { + $this->majorPart = $majorPart; + $this->minorPart = $minorPart; + $this->patchPart = $patchPart; + } + + /** + * Returns the "major" part. + * Incremented when you make incompatible API changes. + * + * @return int + */ + public function getMajorPart() + { + return $this->majorPart; + } + + /** + * Returns the "minor" part. + * Incremented when you add functionality in a backwards-compatible manner. + * + * @return int + */ + public function getMinorPart() + { + return $this->minorPart; + } + + /** + * Returns the "patch" part. + * Incremented when you make backwards-compatible bug fixes. + * + * @return int + */ + public function getPatchPart() + { + return $this->patchPart; + } + + /** + * Returns string representation of instance of this class + * + * @return string + */ + public function __toString() + { + return sprintf('%d.%d.%d', $this->getMajorPart(), $this->getMinorPart(), $this->getPatchPart()); + } + + /** + * Returns new instance based on given version as string. + * Given version should contain 3 dot-separated integers, 1 per each part ("major", "minor" and "patch"). + * + * Examples: + * "1.0.2"; + * "10.4.0"; + * + * @param string $version The version + * @return Version|null + */ + public static function fromString($version) + { + $version = trim($version); + + /* + * No version provided? + * Nothing to do + */ + if (empty($version)) { + return null; + } + + $matches = []; + $pattern = '/^(\d+)\.(\d+)\.(\d+)$/'; // e.g. "1.0.2" + $matched = preg_match($pattern, $version, $matches); + + /* + * Incorrect version? + * Nothing to do + */ + if (0 === $matched || false === $matched) { + return null; + } + + $majorPart = (int)$matches[1]; + $minorPart = (int)$matches[2]; + $patchPart = (int)$matches[3]; + + return new static($majorPart, $minorPart, $patchPart); + } + + /** + * Returns new instance based on given version as array. + * Given version should contain 3 integers, 1 per each part ("major", "minor" and "patch"). + * + * Examples: + * [1, 0, 2]; + * [10, 4, 0]; + * + * @param array $version The version + * @return Version|null + */ + public static function fromArray(array $version) + { + /* + * No version provided? + * Nothing to do + */ + if (empty($version)) { + return null; + } + + $count = count($version); + + /* + * Incorrect version? + * Nothing to do + */ + if (3 !== $count) { + return null; + } + + $majorPart = (int)$version[0]; + $minorPart = (int)$version[1]; + $patchPart = (int)$version[2]; + + return new static($majorPart, $minorPart, $patchPart); + } +} diff --git a/tests/ValueObject/VersionTest.php b/tests/ValueObject/VersionTest.php new file mode 100644 index 0000000..fb0e783 --- /dev/null +++ b/tests/ValueObject/VersionTest.php @@ -0,0 +1,188 @@ + + * @copyright Meritoo + */ +class VersionTest extends BaseTestCase +{ + public function testConstructor() + { + static::assertConstructorVisibilityAndArguments(Version::class, OopVisibilityType::IS_PUBLIC, 3, 3); + } + + public function testNewInstance() + { + $version = new Version(1, 0, 2); + + static::assertInstanceOf(Version::class, $version); + static::assertSame(1, Reflection::getPropertyValue($version, 'majorPart')); + static::assertSame(0, Reflection::getPropertyValue($version, 'minorPart')); + static::assertSame(2, Reflection::getPropertyValue($version, 'patchPart')); + } + + /** + * @param Version $version The version + * @param string $expected Expected string + * + * @dataProvider provideConvertedToString + */ + public function testToString(Version $version, $expected) + { + static::assertSame($expected, (string)$version); + } + + /** + * @param string $version The version + * @param Version $expected (optional) Expected version + * + * @dataProvider provideAsString + */ + public function testFromString($version, Version $expected = null) + { + static::assertEquals($expected, Version::fromString($version)); + } + + /** + * @param array $version The version + * @param Version $expected (optional) Expected version + * + * @dataProvider provideAsArray + */ + public function testFromArray(array $version, Version $expected = null) + { + static::assertEquals($expected, Version::fromArray($version)); + } + + /** + * Provide instance of version and expected version converted to string + * + * @return Generator + */ + public function provideConvertedToString() + { + yield[ + new Version(0, 0, 0), + '0.0.0', + ]; + + yield[ + new Version(1, 0, 2), + '1.0.2', + ]; + + yield[ + new Version(10, 5, 41), + '10.5.41', + ]; + } + + /** + * Provide version as string and expected instance of version + * + * @return Generator + */ + public function provideAsString() + { + yield[ + '', + null, + ]; + + yield[ + '1.0', + null, + ]; + + yield[ + '10', + null, + ]; + + yield[ + '0.0.0', + new Version(0, 0, 0), + ]; + + yield[ + '1.0.2', + new Version(1, 0, 2), + ]; + + yield[ + '10.5.41', + new Version(10, 5, 41), + ]; + } + + /** + * Provide version as array and expected instance of version + * + * @return Generator + */ + public function provideAsArray() + { + yield[ + [], + null, + ]; + + yield[ + [ + 1, + 0, + ], + null, + ]; + + yield[ + [ + 10, + ], + null, + ]; + + yield[ + [ + 0, + 0, + 0, + ], + new Version(0, 0, 0), + ]; + + yield[ + [ + 1, + 0, + 2, + ], + new Version(1, 0, 2), + ]; + + yield[ + [ + 10, + 5, + 41, + ], + new Version(10, 5, 41), + ]; + } +}