ValueObject > class Version > represents version of software

This commit is contained in:
Meritoo
2018-07-02 20:07:33 +02:00
parent b49605a26c
commit 9f08a2aaaf
3 changed files with 368 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ Common and useful classes, methods, exceptions etc.
# 0.1.1 # 0.1.1
1. TravisCI > run using PHP 7.2 too 1. TravisCI > run using PHP 7.2 too
2. ValueObject > class Version > represents version of software
# 0.1.0 # 0.1.0

179
src/ValueObject/Version.php Normal file
View File

@@ -0,0 +1,179 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\ValueObject;
/**
* Version of software
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -0,0 +1,188 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Test\Common\ValueObject;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\Reflection;
use Meritoo\Common\ValueObject\Version;
/**
* Test case for the version of software
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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),
];
}
}