mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bd1e1e158 | ||
|
|
535ae65e5e | ||
|
|
60d7b03cd7 | ||
|
|
c20fa5941f | ||
|
|
a448d592d2 | ||
|
|
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
|
||||
|
||||
12
CHANGELOG.md
12
CHANGELOG.md
@@ -2,6 +2,18 @@
|
||||
|
||||
Common and useful classes, methods, exceptions etc.
|
||||
|
||||
# 0.1.2
|
||||
|
||||
1. Documentation > Value Objects
|
||||
2. Docker > improve performance
|
||||
3. Utilities > Reflection > setPropertyValue() method > sets value of given property
|
||||
|
||||
# 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+)
|
||||
|
||||
@@ -20,6 +20,7 @@ composer require meritoo/common-library
|
||||
2. [Collection of elements](docs/Collection-of-elements.md)
|
||||
3. [Exceptions](docs/Static-methods.md)
|
||||
4. [Static methods](docs/Static-methods.md)
|
||||
5. [Value Objects](docs/Value-Objects.md)
|
||||
|
||||
# Development
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
"name": "meritoo/common-library",
|
||||
"description": "Useful classes, methods, extensions etc.",
|
||||
"license": "MIT",
|
||||
"version": "0.1.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Meritoo.pl",
|
||||
|
||||
@@ -9,12 +9,12 @@ services:
|
||||
build:
|
||||
context: ./docker/config
|
||||
args:
|
||||
- TIMEZONE=$TIMEZONE
|
||||
- TIMEZONE=${TIMEZONE}
|
||||
volumes:
|
||||
- .:/project
|
||||
- .:/project:cached
|
||||
composer:
|
||||
image: ${DOCKER_CONTAINER_OWNER}/${DOCKER_CONTAINER_PROJECT}-php
|
||||
container_name: ${DOCKER_CONTAINER_OWNER}-${DOCKER_CONTAINER_PROJECT}-composer
|
||||
entrypoint: composer
|
||||
volumes:
|
||||
- .:/project
|
||||
- .:/project:cached
|
||||
|
||||
@@ -48,5 +48,6 @@ class MimeTypesTest extends BaseTestCase
|
||||
2. [Collection of elements](Collection-of-elements.md)
|
||||
3. [Exceptions](Exceptions.md)
|
||||
4. [Static methods](Static-methods.md)
|
||||
5. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -46,5 +46,6 @@ var_dump($simpleCollection->has('dolor')); // bool(true)
|
||||
2. [**Collection of elements**](Collection-of-elements.md)
|
||||
3. [Exceptions](Exceptions.md)
|
||||
4. [Static methods](Static-methods.md)
|
||||
5. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -57,5 +57,6 @@ class UnknownSimpleTypeException extends UnknownTypeException
|
||||
2. [Collection of elements](Collection-of-elements.md)
|
||||
3. [**Exceptions**](Exceptions.md)
|
||||
4. [Static methods](Static-methods.md)
|
||||
5. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -19,5 +19,6 @@ var_dump($firstElement); // string(5) "lorem"
|
||||
2. [Collection of elements](Collection-of-elements.md)
|
||||
3. [Exceptions](Exceptions.md)
|
||||
4. [**Static methods**](Static-methods.md)
|
||||
5. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
53
docs/Value-Objects.md
Normal file
53
docs/Value-Objects.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# Meritoo Common Library
|
||||
|
||||
Common and useful classes, methods, exceptions etc.
|
||||
|
||||
# Value Objects
|
||||
|
||||
Located in `Meritoo\Common\ValueObject` namespace.
|
||||
|
||||
### Version
|
||||
|
||||
##### Namespace
|
||||
|
||||
`Meritoo\Common\ValueObject\Version`
|
||||
|
||||
##### Info
|
||||
|
||||
Represents version of software. Contains 3 properties:
|
||||
1. `$majorPart` - the "major" part of version
|
||||
2. `$minorPart` - the "minor" part of version
|
||||
3. `$patchPart` - the "patch" part of version
|
||||
|
||||
##### New instance
|
||||
|
||||
New instance can be created using:
|
||||
|
||||
1. Constructor:
|
||||
|
||||
```php
|
||||
new Version(1, 0, 2);
|
||||
```
|
||||
|
||||
2. Static methods:
|
||||
1. `fromArray()` - creates new instance using given version as array
|
||||
|
||||
```php
|
||||
Version::fromArray([1, 0, 2]);
|
||||
```
|
||||
|
||||
2. `fromString()` - creates new instance using given version as string:
|
||||
|
||||
```php
|
||||
Version::fromString('1.0.2');
|
||||
```
|
||||
|
||||
# More
|
||||
|
||||
1. [Base test case (with common methods and data providers)](Base-test-case.md)
|
||||
2. [Collection of elements](Collection-of-elements.md)
|
||||
3. [Exceptions](Exceptions.md)
|
||||
4. [Static methods](Static-methods.md)
|
||||
5. [**Value Objects**](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
33
src/Exception/Reflection/NotExistingPropertyException.php
Normal file
33
src/Exception/Reflection/NotExistingPropertyException.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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\Exception\Reflection;
|
||||
|
||||
/**
|
||||
* An exception used while property does not exist in instance of class
|
||||
*
|
||||
* @author Meritoo <github@meritoo.pl>
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*/
|
||||
class NotExistingPropertyException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Creates exception
|
||||
*
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
* @return NotExistingPropertyException
|
||||
*/
|
||||
public static function create($object, $property)
|
||||
{
|
||||
$template = 'Property \'%s\' does not exist in instance of class \'%s\'. Did you use proper name of property?';
|
||||
$message = sprintf($template, $property, get_class($object));
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,8 @@ use Doctrine\Common\Util\Inflector;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use ReflectionObject;
|
||||
use ReflectionProperty;
|
||||
|
||||
/**
|
||||
* Useful reflection methods
|
||||
@@ -34,21 +30,20 @@ class Reflection
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @param bool $withoutInheritance (optional) If is set to true, only methods for given class are returned.
|
||||
* Otherwise - all methods, with inherited methods too.
|
||||
* @throws ReflectionException
|
||||
* @return array
|
||||
*/
|
||||
public static function getMethods($class, $withoutInheritance = false)
|
||||
{
|
||||
$effect = [];
|
||||
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$methods = $reflection->getMethods();
|
||||
|
||||
if (!empty($methods)) {
|
||||
$className = self::getClassName($class);
|
||||
|
||||
foreach ($methods as $method) {
|
||||
if ($method instanceof ReflectionMethod) {
|
||||
if ($method instanceof \ReflectionMethod) {
|
||||
if ($withoutInheritance && $className !== $method->class) {
|
||||
continue;
|
||||
}
|
||||
@@ -65,12 +60,11 @@ class Reflection
|
||||
* Returns constants of given class / object
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @throws ReflectionException
|
||||
* @return array
|
||||
*/
|
||||
public static function getConstants($class)
|
||||
{
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
return $reflection->getConstants();
|
||||
}
|
||||
@@ -80,7 +74,6 @@ class Reflection
|
||||
* Constants whose values are integers are considered only.
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @throws ReflectionException
|
||||
* @return int|null
|
||||
*/
|
||||
public static function getMaxNumberConstant($class)
|
||||
@@ -107,12 +100,11 @@ class Reflection
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @param string $method Name of the method to find
|
||||
* @throws ReflectionException
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMethod($class, $method)
|
||||
{
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
return $reflection->hasMethod($method);
|
||||
}
|
||||
@@ -122,12 +114,11 @@ class Reflection
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @param string $property Name of the property to find
|
||||
* @throws ReflectionException
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasProperty($class, $property)
|
||||
{
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
return $reflection->hasProperty($property);
|
||||
}
|
||||
@@ -137,12 +128,11 @@ class Reflection
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @param string $constant Name of the constant to find
|
||||
* @throws ReflectionException
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasConstant($class, $constant)
|
||||
{
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
return $reflection->hasConstant($constant);
|
||||
}
|
||||
@@ -152,12 +142,11 @@ class Reflection
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @param string $constant Name of the constant that contains a value
|
||||
* @throws ReflectionException
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getConstantValue($class, $constant)
|
||||
{
|
||||
$reflection = new ReflectionClass($class);
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
if (self::hasConstant($class, $constant)) {
|
||||
return $reflection->getConstant($constant);
|
||||
@@ -175,7 +164,6 @@ class Reflection
|
||||
* dot-separated, e.g. "invoice.user.email".
|
||||
* @param bool $force (optional) If is set to true, try to retrieve value even if the object doesn't have
|
||||
* property. Otherwise - not.
|
||||
* @throws ReflectionException
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getPropertyValue($object, $property, $force = false)
|
||||
@@ -197,9 +185,9 @@ class Reflection
|
||||
* Let's dig more and get proper value
|
||||
*
|
||||
* Required to avoid bug:
|
||||
* ReflectionObject::__construct() expects parameter 1 to be object, null given
|
||||
* \ReflectionObject::__construct() expects parameter 1 to be object, null given
|
||||
* (...)
|
||||
* 4. at ReflectionObject->__construct (null)
|
||||
* 4. at \ReflectionObject->__construct (null)
|
||||
* 5. at Reflection ::getPropertyValue (null, 'name', true)
|
||||
* 6. at ListService->getItemValue (object(Deal), 'project.name', '0')
|
||||
*
|
||||
@@ -224,17 +212,17 @@ class Reflection
|
||||
* Use \ReflectionObject class
|
||||
*/
|
||||
try {
|
||||
$reflectionProperty = new ReflectionProperty($className, $property);
|
||||
$reflectionProperty = new \ReflectionProperty($className, $property);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
} catch (ReflectionException $exception) {
|
||||
} catch (\ReflectionException $exception) {
|
||||
/*
|
||||
* 2nd try:
|
||||
* Look for the get / has / is methods
|
||||
*/
|
||||
$class = new ReflectionObject($object);
|
||||
$class = new \ReflectionObject($object);
|
||||
$valueFound = false;
|
||||
|
||||
if ($class->hasProperty($property) || $force) {
|
||||
if ($force || $class->hasProperty($property)) {
|
||||
$property = Inflector::classify($property);
|
||||
|
||||
$getterPrefixes = [
|
||||
@@ -247,7 +235,7 @@ class Reflection
|
||||
$getterName = sprintf('%s%s', $prefix, $property);
|
||||
|
||||
if ($class->hasMethod($getterName)) {
|
||||
$method = new ReflectionMethod($object, $getterName);
|
||||
$method = new \ReflectionMethod($object, $getterName);
|
||||
|
||||
/*
|
||||
* Getter is not accessible publicly?
|
||||
@@ -292,7 +280,6 @@ class Reflection
|
||||
* @param string $property Name of the property that contains a value
|
||||
* @param bool $force (optional) If is set to true, try to retrieve value even if the
|
||||
* object does not have property. Otherwise - not.
|
||||
* @throws ReflectionException
|
||||
* @return array
|
||||
*/
|
||||
public static function getPropertyValues($objects, $property, $force = false)
|
||||
@@ -395,13 +382,13 @@ class Reflection
|
||||
{
|
||||
$fullClassName = self::getClassName($source);
|
||||
|
||||
if (empty($fullClassName)) {
|
||||
if (null === $fullClassName || '' === $fullClassName) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$className = self::getClassName($source, true);
|
||||
|
||||
if ($className == $fullClassName) {
|
||||
if ($className === $fullClassName) {
|
||||
return $className;
|
||||
}
|
||||
|
||||
@@ -420,7 +407,7 @@ class Reflection
|
||||
$className = self::getClassName($source);
|
||||
$interfaces = class_implements($className);
|
||||
|
||||
return in_array($interface, $interfaces);
|
||||
return in_array($interface, $interfaces, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -438,7 +425,7 @@ class Reflection
|
||||
$parents = class_parents($childClassName);
|
||||
|
||||
if (is_array($parents) && 0 < count($parents)) {
|
||||
return in_array($parentClassName, $parents);
|
||||
return in_array($parentClassName, $parents, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -448,23 +435,22 @@ class Reflection
|
||||
* Returns given object properties
|
||||
*
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class
|
||||
* @param int $filter (optional) Filter of properties. Uses \ReflectionProperty class
|
||||
* constants. By default all properties are returned.
|
||||
* @param bool $includeParents (optional) If is set to true, properties of parent classes are
|
||||
* included (recursively). Otherwise - not.
|
||||
* @throws ReflectionException
|
||||
* @return array|ReflectionProperty
|
||||
* @return array|\ReflectionProperty
|
||||
*/
|
||||
public static function getProperties($source, $filter = null, $includeParents = false)
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$reflection = new ReflectionClass($className);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
|
||||
if (null === $filter) {
|
||||
$filter = ReflectionProperty::IS_PRIVATE
|
||||
+ ReflectionProperty::IS_PROTECTED
|
||||
+ ReflectionProperty::IS_PUBLIC
|
||||
+ ReflectionProperty::IS_STATIC;
|
||||
$filter = \ReflectionProperty::IS_PRIVATE
|
||||
+ \ReflectionProperty::IS_PROTECTED
|
||||
+ \ReflectionProperty::IS_PUBLIC
|
||||
+ \ReflectionProperty::IS_STATIC;
|
||||
}
|
||||
|
||||
$properties = $reflection->getProperties($filter);
|
||||
@@ -486,13 +472,12 @@ class Reflection
|
||||
* Returns a parent class or false if there is no parent class
|
||||
*
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @throws ReflectionException
|
||||
* @return ReflectionClass|bool
|
||||
* @return \ReflectionClass|bool
|
||||
*/
|
||||
public static function getParentClass($source)
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$reflection = new ReflectionClass($className);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
|
||||
return $reflection->getParentClass();
|
||||
}
|
||||
@@ -541,7 +526,7 @@ class Reflection
|
||||
*/
|
||||
$realClass = ClassUtils::getRealClass($oneClass);
|
||||
|
||||
if (in_array($realClass, $childClasses)) {
|
||||
if (in_array($realClass, $childClasses, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -586,14 +571,13 @@ class Reflection
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns property, the ReflectionProperty instance, of given object
|
||||
* Returns property, the \ReflectionProperty instance, of given object
|
||||
*
|
||||
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
||||
* @param string $property Name of the property
|
||||
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class constants.
|
||||
* @param int $filter (optional) Filter of properties. Uses \ReflectionProperty class constants.
|
||||
* By default all properties are allowed / processed.
|
||||
* @throws ReflectionException
|
||||
* @return null|ReflectionProperty
|
||||
* @return null|\ReflectionProperty
|
||||
*/
|
||||
public static function getProperty($class, $property, $filter = null)
|
||||
{
|
||||
@@ -601,9 +585,9 @@ class Reflection
|
||||
$properties = self::getProperties($className, $filter);
|
||||
|
||||
if (!empty($properties)) {
|
||||
/* @var $reflectionProperty ReflectionProperty */
|
||||
/* @var $reflectionProperty \ReflectionProperty */
|
||||
foreach ($properties as $reflectionProperty) {
|
||||
if ($reflectionProperty->getName() == $property) {
|
||||
if ($reflectionProperty->getName() === $property) {
|
||||
return $reflectionProperty;
|
||||
}
|
||||
}
|
||||
@@ -630,21 +614,21 @@ class Reflection
|
||||
/*
|
||||
* Oops, cannot resolve class
|
||||
*/
|
||||
if (empty($className)) {
|
||||
if (null === $className || '' === $className) {
|
||||
throw CannotResolveClassNameException::create($class);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, cannot resolve trait
|
||||
*/
|
||||
if (empty($traitName)) {
|
||||
if (null === $traitName || '' === $traitName) {
|
||||
throw new CannotResolveClassNameException($class, false);
|
||||
}
|
||||
|
||||
$reflection = new ReflectionClass($className);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$traitsNames = $reflection->getTraitNames();
|
||||
|
||||
$uses = in_array($traitName, $traitsNames);
|
||||
$uses = in_array($traitName, $traitsNames, true);
|
||||
|
||||
if (!$uses && $verifyParents) {
|
||||
$parentClassName = self::getParentClassName($className);
|
||||
@@ -662,13 +646,12 @@ class Reflection
|
||||
* If given class does not extend another, returns null.
|
||||
*
|
||||
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
||||
* @throws ReflectionException
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getParentClassName($class)
|
||||
{
|
||||
$className = self::getClassName($class);
|
||||
$reflection = new ReflectionClass($className);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$parentClass = $reflection->getParentClass();
|
||||
|
||||
if (null === $parentClass || false === $parentClass) {
|
||||
@@ -677,4 +660,36 @@ class Reflection
|
||||
|
||||
return $parentClass->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets value of given property
|
||||
*
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
* @param mixed $value Value of the property
|
||||
* @throws NotExistingPropertyException
|
||||
*/
|
||||
public static function setPropertyValue($object, $property, $value)
|
||||
{
|
||||
$reflectionProperty = self::getProperty($object, $property);
|
||||
|
||||
/*
|
||||
* Oops, property does not exist
|
||||
*/
|
||||
if (null === $reflectionProperty) {
|
||||
throw NotExistingPropertyException::create($object, $property);
|
||||
}
|
||||
|
||||
$notAccessible = $reflectionProperty->isPrivate() || $reflectionProperty->isProtected();
|
||||
|
||||
if ($notAccessible) {
|
||||
$reflectionProperty->setAccessible(true);
|
||||
}
|
||||
|
||||
$reflectionProperty->setValue($object, $value);
|
||||
|
||||
if ($notAccessible) {
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Generator;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\A;
|
||||
@@ -474,6 +475,37 @@ class ReflectionTest extends BaseTestCase
|
||||
static::assertEquals('name', $property->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
*
|
||||
* @dataProvider provideObjectAndNotExistingProperty
|
||||
*/
|
||||
public function testSetPropertyValueUsingNotExistingProperty($object, $property)
|
||||
{
|
||||
$this->setExpectedException(NotExistingPropertyException::class);
|
||||
|
||||
$object = new \stdClass();
|
||||
Reflection::setPropertyValue($object, 'test', 'test test test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
* @param mixed $value Value of the property
|
||||
*
|
||||
* @dataProvider provideObjectPropertyAndValue
|
||||
*/
|
||||
public function testSetPropertyValue($object, $property, $value)
|
||||
{
|
||||
$oldValue = Reflection::getPropertyValue($object, $property);
|
||||
Reflection::setPropertyValue($object, $property, $value);
|
||||
$newValue = Reflection::getPropertyValue($object, $property);
|
||||
|
||||
static::assertNotSame($oldValue, $value);
|
||||
static::assertSame($newValue, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid class and trait
|
||||
*
|
||||
@@ -501,4 +533,59 @@ class ReflectionTest extends BaseTestCase
|
||||
[],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides object and name of not existing property
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideObjectAndNotExistingProperty()
|
||||
{
|
||||
yield[
|
||||
new \stdClass(),
|
||||
'test',
|
||||
];
|
||||
|
||||
yield[
|
||||
new A(),
|
||||
'test',
|
||||
];
|
||||
|
||||
yield[
|
||||
new B(),
|
||||
'firstName',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides object, name of property and value of the property
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideObjectPropertyAndValue()
|
||||
{
|
||||
yield[
|
||||
new A(),
|
||||
'count',
|
||||
123,
|
||||
];
|
||||
|
||||
yield[
|
||||
new B(),
|
||||
'name',
|
||||
'test test',
|
||||
];
|
||||
|
||||
yield[
|
||||
new G(),
|
||||
'firstName',
|
||||
'Jane',
|
||||
];
|
||||
|
||||
yield[
|
||||
new G(),
|
||||
'lastName',
|
||||
'Smith',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
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