mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e53273fb32 | ||
|
|
cc30ad8d9e | ||
|
|
9f08a2aaaf | ||
|
|
b49605a26c | ||
|
|
8441c82356 |
@@ -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
|
||||
|
||||
@@ -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+)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
"name": "meritoo/common-library",
|
||||
"description": "Useful classes, methods, extensions etc.",
|
||||
"license": "MIT",
|
||||
"version": "0.1.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Meritoo.pl",
|
||||
|
||||
179
src/ValueObject/Version.php
Normal file
179
src/ValueObject/Version.php
Normal 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);
|
||||
}
|
||||
}
|
||||
188
tests/ValueObject/VersionTest.php
Normal file
188
tests/ValueObject/VersionTest.php
Normal 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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user