From 9f6af6b6a429a98c43fb5e0653d9f7d1df793e8f Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sat, 16 Mar 2019 12:37:35 +0100 Subject: [PATCH 1/4] Collection > create trait (to make it more flexible) --- CHANGELOG.md | 4 + VERSION | 2 +- src/Collection/Collection.php | 253 +----------------- src/Traits/Collection/ArrayAccessTrait.php | 67 +++++ src/Traits/Collection/CountableTrait.php | 26 ++ .../Collection/IteratorAggregateTrait.php | 28 ++ src/Traits/Collection/MainTrait.php | 200 ++++++++++++++ src/Traits/CollectionTrait.php | 28 ++ 8 files changed, 356 insertions(+), 252 deletions(-) create mode 100644 src/Traits/Collection/ArrayAccessTrait.php create mode 100644 src/Traits/Collection/CountableTrait.php create mode 100644 src/Traits/Collection/IteratorAggregateTrait.php create mode 100644 src/Traits/Collection/MainTrait.php create mode 100644 src/Traits/CollectionTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a44377d..51da9b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Common and useful classes, methods, exceptions etc. +# 0.1.7 + +1. Collection > create trait (to make it more flexible) + # 0.1.6 1. Arrays > refactoring & more tests diff --git a/VERSION b/VERSION index c946ee6..1180819 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.6 +0.1.7 diff --git a/src/Collection/Collection.php b/src/Collection/Collection.php index 0062c57..6206ce5 100644 --- a/src/Collection/Collection.php +++ b/src/Collection/Collection.php @@ -9,10 +9,9 @@ namespace Meritoo\Common\Collection; use ArrayAccess; -use ArrayIterator; use Countable; use IteratorAggregate; -use Meritoo\Common\Utilities\Arrays; +use Meritoo\Common\Traits\CollectionTrait; /** * Collection of elements. @@ -23,12 +22,7 @@ use Meritoo\Common\Utilities\Arrays; */ class Collection implements Countable, ArrayAccess, IteratorAggregate { - /** - * The elements of collection - * - * @var array - */ - private $elements; + use CollectionTrait; /** * Class constructor @@ -39,247 +33,4 @@ class Collection implements Countable, ArrayAccess, IteratorAggregate { $this->elements = $elements; } - - /** - * {@inheritdoc} - * Required by interface Countable - */ - public function count() - { - return count($this->elements); - } - - /** - * {@inheritdoc} - * Required by interface ArrayAccess - */ - public function offsetExists($offset) - { - return $this->exists($offset); - } - - /** - * {@inheritdoc} - * Required by interface ArrayAccess - */ - public function offsetGet($offset) - { - if ($this->exists($offset)) { - return $this->elements[$offset]; - } - - return null; - } - - /** - * {@inheritdoc} - * Required by interface ArrayAccess - */ - public function offsetSet($offset, $value) - { - $this->elements[$offset] = $value; - } - - /** - * {@inheritdoc} - * Required by interface ArrayAccess - */ - public function offsetUnset($offset) - { - if ($this->exists($offset)) { - unset($this->elements[$offset]); - } - } - - /** - * {@inheritdoc} - * Required by interface IteratorAggregate - */ - public function getIterator() - { - return new ArrayIterator($this->elements); - } - - /** - * Adds given element (at the end of collection) - * - * @param mixed $element The element to add - * @param mixed $index (optional) Index / key of the element - * @return $this - */ - public function add($element, $index = null) - { - if (null === $index || '' === $index) { - $this->elements[] = $element; - } else { - $this->elements[$index] = $element; - } - - return $this; - } - - /** - * Adds given elements (at the end of collection) - * - * @param array|Collection $elements The elements to add - * @param bool|false $useIndexes (optional) If is set to true, indexes of given elements will be used in - * this collection. Otherwise - not. - * @return $this - */ - public function addMultiple($elements, $useIndexes = false) - { - if (!empty($elements)) { - foreach ($elements as $index => $element) { - if ($useIndexes) { - $this->add($element, $index); - continue; - } - - $this->add($element); - } - } - - return $this; - } - - /** - * Prepends given element (adds given element at the beginning of collection) - * - * @param mixed $element The element to prepend - * @return $this - */ - public function prepend($element) - { - array_unshift($this->elements, $element); - - return $this; - } - - /** - * Removes given element - * - * @param mixed $element The element to remove - * @return $this - */ - public function remove($element) - { - if ($this->count() > 0) { - foreach ($this->elements as $index => $existing) { - if ($element === $existing) { - unset($this->elements[$index]); - break; - } - } - } - - return $this; - } - - /** - * Returns information if collection is empty - * - * @return bool - */ - public function isEmpty() - { - return empty($this->elements); - } - - /** - * Returns information if given element is first in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function isFirst($element) - { - return reset($this->elements) === $element; - } - - /** - * Returns information if given element is last in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function isLast($element) - { - return end($this->elements) === $element; - } - - /** - * Returns information if the collection has given element, iow. if given element exists in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function has($element) - { - $index = Arrays::getIndexOf($this->elements, $element); - - return null !== $index && false !== $index; - } - - /** - * Returns previous element for given element - * - * @param mixed $element The element to verify - * @return mixed|null - */ - public function getPrevious($element) - { - return Arrays::getPreviousElement($this->elements, $element); - } - - /** - * Returns next element for given element - * - * @param mixed $element The element to verify - * @return mixed|null - */ - public function getNext($element) - { - return Arrays::getNextElement($this->elements, $element); - } - - /** - * Returns the first element in the collection - * - * @return mixed - */ - public function getFirst() - { - return Arrays::getFirstElement($this->elements); - } - - /** - * Returns the last element in the collection - * - * @return mixed - */ - public function getLast() - { - return Arrays::getLastElement($this->elements); - } - - /** - * Returns representation of object as array - * - * @return array - */ - public function toArray() - { - return $this->elements; - } - - /** - * Returns information if element with given index/key exists - * - * @param string|int $index The index/key of element - * @return bool - */ - private function exists($index) - { - return isset($this->elements[$index]) || array_key_exists($index, $this->elements); - } } diff --git a/src/Traits/Collection/ArrayAccessTrait.php b/src/Traits/Collection/ArrayAccessTrait.php new file mode 100644 index 0000000..ddb8527 --- /dev/null +++ b/src/Traits/Collection/ArrayAccessTrait.php @@ -0,0 +1,67 @@ + + * @copyright Meritoo + */ +trait ArrayAccessTrait +{ + /** + * {@inheritdoc} + */ + public function offsetExists($offset) + { + return $this->exists($offset); + } + + /** + * {@inheritdoc} + */ + public function offsetGet($offset) + { + if ($this->exists($offset)) { + return $this->elements[$offset]; + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value) + { + $this->elements[$offset] = $value; + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset) + { + if ($this->exists($offset)) { + unset($this->elements[$offset]); + } + } + + /** + * Returns information if element with given index/key exists + * + * @param string|int $index The index/key of element + * @return bool + */ + private function exists($index) + { + return isset($this->elements[$index]) || array_key_exists($index, $this->elements); + } +} diff --git a/src/Traits/Collection/CountableTrait.php b/src/Traits/Collection/CountableTrait.php new file mode 100644 index 0000000..4579d40 --- /dev/null +++ b/src/Traits/Collection/CountableTrait.php @@ -0,0 +1,26 @@ + + * @copyright Meritoo + */ +trait CountableTrait +{ + /** + * {@inheritdoc} + */ + public function count() + { + return count($this->elements); + } +} diff --git a/src/Traits/Collection/IteratorAggregateTrait.php b/src/Traits/Collection/IteratorAggregateTrait.php new file mode 100644 index 0000000..9c814b1 --- /dev/null +++ b/src/Traits/Collection/IteratorAggregateTrait.php @@ -0,0 +1,28 @@ + + * @copyright Meritoo + */ +trait IteratorAggregateTrait +{ + /** + * {@inheritdoc} + */ + public function getIterator() + { + return new ArrayIterator($this->elements); + } +} diff --git a/src/Traits/Collection/MainTrait.php b/src/Traits/Collection/MainTrait.php new file mode 100644 index 0000000..8835a2d --- /dev/null +++ b/src/Traits/Collection/MainTrait.php @@ -0,0 +1,200 @@ + + * @copyright Meritoo + */ +trait MainTrait +{ + /** + * The elements of collection + * + * @var array + */ + private $elements; + + /** + * Adds given element (at the end of collection) + * + * @param mixed $element The element to add + * @param mixed $index (optional) Index / key of the element + * @return $this + */ + public function add($element, $index = null) + { + if (null === $index || '' === $index) { + $this->elements[] = $element; + } else { + $this->elements[$index] = $element; + } + + return $this; + } + + /** + * Adds given elements (at the end of collection) + * + * @param array|Collection $elements The elements to add + * @param bool|false $useIndexes (optional) If is set to true, indexes of given elements will be used in + * this collection. Otherwise - not. + * @return $this + */ + public function addMultiple($elements, $useIndexes = false) + { + if (!empty($elements)) { + foreach ($elements as $index => $element) { + if ($useIndexes) { + $this->add($element, $index); + continue; + } + + $this->add($element); + } + } + + return $this; + } + + /** + * Prepends given element (adds given element at the beginning of collection) + * + * @param mixed $element The element to prepend + * @return $this + */ + public function prepend($element) + { + array_unshift($this->elements, $element); + + return $this; + } + + /** + * Removes given element + * + * @param mixed $element The element to remove + * @return $this + */ + public function remove($element) + { + if ($this->count() > 0) { + foreach ($this->elements as $index => $existing) { + if ($element === $existing) { + unset($this->elements[$index]); + break; + } + } + } + + return $this; + } + + /** + * Returns information if collection is empty + * + * @return bool + */ + public function isEmpty() + { + return empty($this->elements); + } + + /** + * Returns information if given element is first in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function isFirst($element) + { + return reset($this->elements) === $element; + } + + /** + * Returns information if given element is last in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function isLast($element) + { + return end($this->elements) === $element; + } + + /** + * Returns information if the collection has given element, iow. if given element exists in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function has($element) + { + $index = Arrays::getIndexOf($this->elements, $element); + + return null !== $index && false !== $index; + } + + /** + * Returns previous element for given element + * + * @param mixed $element The element to verify + * @return mixed|null + */ + public function getPrevious($element) + { + return Arrays::getPreviousElement($this->elements, $element); + } + + /** + * Returns next element for given element + * + * @param mixed $element The element to verify + * @return mixed|null + */ + public function getNext($element) + { + return Arrays::getNextElement($this->elements, $element); + } + + /** + * Returns the first element in the collection + * + * @return mixed + */ + public function getFirst() + { + return Arrays::getFirstElement($this->elements); + } + + /** + * Returns the last element in the collection + * + * @return mixed + */ + public function getLast() + { + return Arrays::getLastElement($this->elements); + } + + /** + * Returns representation of object as array + * + * @return array + */ + public function toArray() + { + return $this->elements; + } +} diff --git a/src/Traits/CollectionTrait.php b/src/Traits/CollectionTrait.php new file mode 100644 index 0000000..8cb0cad --- /dev/null +++ b/src/Traits/CollectionTrait.php @@ -0,0 +1,28 @@ + + * @copyright Meritoo + */ +trait CollectionTrait +{ + use MainTrait; + use CountableTrait; + use ArrayAccessTrait; + use IteratorAggregateTrait; +} From eade6a25ad8d7a87a83ccf81ae1b1cfdd513c560 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sat, 16 Mar 2019 19:58:02 +0100 Subject: [PATCH 2/4] Collection > the getByIndex() method > returns element with given index --- src/Traits/Collection/MainTrait.php | 15 ++++++ tests/Collection/CollectionTest.php | 80 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/Traits/Collection/MainTrait.php b/src/Traits/Collection/MainTrait.php index 8835a2d..8c401fa 100644 --- a/src/Traits/Collection/MainTrait.php +++ b/src/Traits/Collection/MainTrait.php @@ -188,6 +188,21 @@ trait MainTrait return Arrays::getLastElement($this->elements); } + /** + * Returns element with given index + * + * @param mixed $index Index / key of the element + * @return mixed|null + */ + public function getByIndex($index) + { + if (isset($this->elements[$index])) { + return $this->elements[$index]; + } + + return null; + } + /** * Returns representation of object as array * diff --git a/tests/Collection/CollectionTest.php b/tests/Collection/CollectionTest.php index 5b789c7..37e9be2 100644 --- a/tests/Collection/CollectionTest.php +++ b/tests/Collection/CollectionTest.php @@ -327,6 +327,19 @@ class CollectionTest extends BaseTestCase static::assertMethodVisibilityAndArguments(Collection::class, 'exists', OopVisibilityType::IS_PRIVATE, 1, 1); } + /** + * @param string $description Description of test + * @param Collection $collection Collection to search for element with given index + * @param mixed $index Index / key of the element + * @param mixed $expected Expected element with given index + * + * @dataProvider provideElementGetByIndex + */ + public function testGetByIndex($description, Collection $collection, $index, $expected) + { + static::assertEquals($expected, $collection->getByIndex($index), $description); + } + /** * Provides element to add to collection * @@ -408,6 +421,73 @@ class CollectionTest extends BaseTestCase ]; } + public function provideElementGetByIndex() + { + yield[ + 'An empty collection and empty index', + new Collection(), + '', + null, + ]; + + yield[ + 'An empty collection and non-empty index', + new Collection(), + 'test', + null, + ]; + + yield[ + 'Non-empty collection and not existing index', + new Collection([ + 'lorem' => 'ipsum', + 'dolor' => 'sit', + ]), + 'test', + null, + ]; + + yield[ + 'Collection with existing index', + new Collection([ + 'lorem' => 'ipsum', + 'dolor' => 'sit', + ]), + 'lorem', + 'ipsum', + ]; + + yield[ + 'Collection with existing index (collection of arrays)', + new Collection([ + [ + 'lorem', + 'ipsum', + ], + [ + 'dolor', + 'sit', + ], + ]), + 0, + [ + 'lorem', + 'ipsum', + ], + ]; + + yield[ + 'Collection with existing index (collection of objects)', + new Collection([ + 'x' => new \DateTime(), + 'y' => new \DateTime('2001-01-01'), + 'z' => new \DateTime('yesterday'), + ]), + 'y', + new \DateTime('2001-01-01'), + ]; + } + /** * {@inheritdoc} */ From 56b058ca1d97a3412f62c50899e99ddd9c41c6c2 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sun, 24 Mar 2019 15:02:44 +0100 Subject: [PATCH 3/4] Size, e.g. of image --- CHANGELOG.md | 4 + VERSION | 2 +- docs/Value-Objects.md | 68 +- .../InvalidSizeDimensionsException.php | 33 + src/Utilities/Regex.php | 62 + src/ValueObject/Size.php | 245 ++++ .../InvalidSizeDimensionsExceptionTest.php | 67 + tests/Utilities/RegexTest.php | 207 ++++ tests/ValueObject/SizeTest.php | 1095 +++++++++++++++++ 9 files changed, 1781 insertions(+), 2 deletions(-) create mode 100644 src/Exception/ValueObject/InvalidSizeDimensionsException.php create mode 100644 src/ValueObject/Size.php create mode 100644 tests/Exception/ValueObject/InvalidSizeDimensionsExceptionTest.php create mode 100644 tests/ValueObject/SizeTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 51da9b1..475b1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Common and useful classes, methods, exceptions etc. +# 0.1.8 + +1. Size, e.g. of image + # 0.1.7 1. Collection > create trait (to make it more flexible) diff --git a/VERSION b/VERSION index 1180819..699c6c6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.7 +0.1.8 diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index e6a8c79..0d2f6ef 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -177,6 +177,68 @@ $human2 = new Human('John', 'Scott', 'john@scott.com', new \DateTime('2001-01-01 $asString2 = (string)$human2; // "John Scott " ``` +### Size + +##### Namespace + +`Meritoo\Common\ValueObject\Size` + +##### Info + +Size, e.g. of image. Contains properties: +1. `width` - the width +2. `height` - the height +3. `unit` - unit used when width or height should be returned with unit, default: `"px"` +4. `separator` - separator used when converting to string, default: `" x "` + +##### New instance + +New instance can be created using static methods: + +1. `fromArray()` - creates new instance from given array + + ```php + // Using default "px" unit + Size::fromArray([200, 100]); + + // With custom "mm" unit + Size::fromArray([200, 100], 'mm'); + ``` + +2. `fromString()` - creates new instance from given string + + ```php + // Using default "px" unit and default " x " separator + Size::fromString('200 x 100'); + + // With custom "mm" unit and " X " separator + Size::fromString('200 X 100', 'mm', ' X '); + ``` + +##### Methods + +Has: +- getters and setters for `width` and `height` properties. +- setter for `separator` property +- `toString()` and `toArray()` methods that returns size represented as string and array + +##### Conversion to string (using `__toString()` method) + +Instance of `Size` may be represented as string that contains width and height separated by separator (default: `" x "`). + +Example: + +```php +$size = Size::fromArray([200, 100]); + +// With default separator +$asString1 = (string)$size; // "200 x 100" + +// With custom separator +$size->setSeparator('X'); +$asString2 = (string)$size; // "200X100" +``` + ### Version ##### Namespace @@ -213,7 +275,11 @@ New instance can be created using: Version::fromString('1.0.2'); ``` -##### Conversion to string (the `__toString()` method) +##### Methods + +Has getters for each property: `getMajorPart()`, `getMinorPart()`, `getPatchPart()`. + +##### Conversion to string (using `__toString()` method) Instance of `Version` may be represented as string that contains all properties separated by `.` (`$majorPart`.`$minorPart`.`$patchPart`). diff --git a/src/Exception/ValueObject/InvalidSizeDimensionsException.php b/src/Exception/ValueObject/InvalidSizeDimensionsException.php new file mode 100644 index 0000000..8f922e1 --- /dev/null +++ b/src/Exception/ValueObject/InvalidSizeDimensionsException.php @@ -0,0 +1,33 @@ + + * @copyright Meritoo + */ +class InvalidSizeDimensionsException extends \Exception +{ + /** + * Creates exception + * + * @param int $width The width + * @param int $height The height + * @return InvalidSizeDimensionsException + */ + public static function create($width, $height) + { + $template = 'Dimensions of size should be positive, but they are not: %d, %d. Is there everything ok?'; + $message = sprintf($template, $width, $height); + + return new static($message); + } +} diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index 46d5b37..3f8f78b 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -40,6 +40,18 @@ class Regex 'color' => '/^[a-f0-9]{6}$/i', 'bundleName' => '/^(([A-Z]{1}[a-z0-9]+)((?2))*)(Bundle)$/', 'binaryValue' => '/[^\x20-\x7E\t\r\n]/', + + /* + * Matches: + * - "200x125" + * - "200 x 125" + * - "200 x 125" + * - " 200 x 125" + * - " 200 x 125 " + * + * Contains "%s" that should be replaced with separator used to split width and height. + */ + 'size' => '/^[\ ]*(\d+)[\ ]*%s[\ ]*(\d+)[\ ]*$/', ]; /** @@ -920,6 +932,56 @@ class Regex return (bool)preg_match($pattern, $value); } + /** + * Returns pattern used to validate / verify size + * + * @param string $separator (optional) Separator used to split width and height. Default: " x ". + * @return string + */ + public static function getSizePattern($separator = ' x ') + { + $escapeMe = [ + '/', + '|', + '.', + '(', + ')', + '[', + ']', + ]; + + $cleanSeparator = trim($separator); + + if (in_array($cleanSeparator, $escapeMe, true)) { + // I have to escape special character of regular expression that may be used as separator + $separator = str_replace($cleanSeparator, '\\' . $cleanSeparator, $separator); + } + + return sprintf(self::$patterns['size'], $separator); + } + + /** + * Returns information if given value is a size value + * + * @param string $value Value to verify + * @param string $separator (optional) Separator used to split width and height. Default: " x ". + * @return bool + */ + public static function isSizeValue($value, $separator = ' x ') + { + /* + * Not a string? + * Nothing to do + */ + if (!is_string($value)) { + return false; + } + + $pattern = self::getSizePattern($separator); + + return (bool)preg_match($pattern, $value); + } + /** * Returns slug for given value * diff --git a/src/ValueObject/Size.php b/src/ValueObject/Size.php new file mode 100644 index 0000000..1336ca1 --- /dev/null +++ b/src/ValueObject/Size.php @@ -0,0 +1,245 @@ + + * @copyright Meritoo + */ +class Size +{ + /** + * The width + * + * @var int + */ + protected $width; + + /** + * The height + * + * @var int + */ + protected $height; + + /** + * Unit used when width or height should be returned with unit + * + * @var string + */ + protected $unit; + + /** + * Separator used when converting to string + * + * @var string + */ + protected $separator = ' x '; + + /** + * Class constructor + * + * @param int $width (optional) The width + * @param int $height (optional) The height + * @param string $unit (optional) Unit used when width or height should be returned with unit. Default: "px". + * + * @throws InvalidSizeDimensionsException + */ + private function __construct($width = null, $height = null, $unit = 'px') + { + $width = (int)$width; + $height = (int)$height; + + if ($width < 0 || $height < 0) { + throw new InvalidSizeDimensionsException($width, $height); + } + + $this + ->setWidth($width) + ->setHeight($height) + ; + + $this->unit = $unit; + } + + /** + * Returns string representation of instance of this class in human readable format, e.g. '200 x 100' + * + * @return string + */ + public function __toString() + { + return $this->toString(); + } + + /** + * Sets separator used when converting to string + * + * @param string $separator The separator + * @return Size + */ + public function setSeparator($separator) + { + $this->separator = $separator; + + return $this; + } + + /** + * Returns the width + * + * @param bool $withUnit (optional) If is set to true, width is returned with unit ("px"). Otherwise - without + * (default behaviour). + * @return int|string + */ + public function getWidth($withUnit = false) + { + if ($withUnit) { + return sprintf('%d %s', $this->width, $this->unit); + } + + return $this->width; + } + + /** + * Sets the width + * + * @param int|string $width The width + * @return Size + */ + public function setWidth($width) + { + $this->width = (int)$width; + + return $this; + } + + /** + * Returns the height + * + * @param bool $withUnit (optional) If is set to true, height is returned with unit ("px"). Otherwise - without + * (default behaviour). + * @return int|string + */ + public function getHeight($withUnit = false) + { + if ($withUnit) { + return sprintf('%d %s', $this->height, $this->unit); + } + + return $this->height; + } + + /** + * Sets the height + * + * @param int $height The height + * @return Size + */ + public function setHeight($height) + { + $this->height = (int)$height; + + return $this; + } + + /** + * Returns string representation of instance of this class, e.g. '200 x 100' or '200x100' + * + * @param bool $withUnit (optional) If is set to true, width and height are returned with unit ("px"). Otherwise + * - without (default behaviour). + * @return string + */ + public function toString($withUnit = false) + { + $width = $this->getWidth($withUnit); + $height = $this->getHeight($withUnit); + + return sprintf('%s%s%s', $width, $this->separator, $height); + } + + /** + * Returns instance of this class as an array. + * Values of the array are width and height, eg. [800, 600] or ['800px', '600px']. + * + * @param bool $withUnits (optional) If is set to true, width and height are returned with unit ("px"). Otherwise + * - without (default behaviour). + * @return array + */ + public function toArray($withUnits = false) + { + return [ + $this->getWidth($withUnits), + $this->getHeight($withUnits), + ]; + } + + /** + * Creates new instance from given string + * + * @param string $size The size represented as string (width and height separated by given separator) + * @param string $unit (optional) Unit used when width or height should be returned with unit. Default: "px". + * @param string $separator (optional) Separator used to split width and height. Default: " x ". + * @return Size|null + */ + public static function fromString($size, $unit = 'px', $separator = ' x ') + { + if (is_string($size)) { + $matches = []; + $pattern = Regex::getSizePattern($separator); + + if ((bool)preg_match($pattern, $size, $matches)) { + $width = (int)$matches[1]; + $height = (int)$matches[2]; + $sizeObject = new self($width, $height, $unit); + + return $sizeObject->setSeparator($separator); + } + } + + return null; + } + + /** + * Creates new instance from given array + * + * The array should contain 2 elements: width and height. + * Examples: ['800', '600'], [800, 600]. + * + * @param array $array The size represented as array + * @param string $unit (optional) Unit used when width or height should be returned with unit. Default: "px". + * @return Size|null + */ + public static function fromArray(array $array, $unit = 'px') + { + // Requirements for given array: + // - indexes "0" and "1" + // - should contains exactly 2 elements + if ( + array_key_exists(0, $array) + && array_key_exists(1, $array) + && 2 === count($array) + ) { + list($width, $height) = $array; + + return new self($width, $height, $unit); + } + + return null; + } +} diff --git a/tests/Exception/ValueObject/InvalidSizeDimensionsExceptionTest.php b/tests/Exception/ValueObject/InvalidSizeDimensionsExceptionTest.php new file mode 100644 index 0000000..ece3449 --- /dev/null +++ b/tests/Exception/ValueObject/InvalidSizeDimensionsExceptionTest.php @@ -0,0 +1,67 @@ + + * @copyright Meritoo + */ +class InvalidSizeDimensionsExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments( + InvalidSizeDimensionsException::class, + OopVisibilityType::IS_PUBLIC, + 3 + ); + } + + /** + * @param int $width The width + * @param int $height The height + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideWidthAndHeight + */ + public function testCreate($width, $height, $expectedMessage) + { + $exception = InvalidSizeDimensionsException::create($width, $height); + static::assertSame($expectedMessage, $exception->getMessage()); + } + + public function provideWidthAndHeight() + { + $template = 'Dimensions of size should be positive, but they are not: %d, %d. Is there everything ok?'; + + yield[ + 0, + 0, + sprintf($template, 0, 0), + ]; + + yield[ + -1, + -1, + sprintf($template, -1, -1), + ]; + + yield[ + 200, + 100, + sprintf($template, 200, 100), + ]; + } +} diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index be0b29e..c1fb3bc 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -624,6 +624,28 @@ class RegexTest extends BaseTestCase self::assertEquals($expected, Regex::getValidColorHexValue($color)); } + /** + * @param mixed $emptyValue Empty value, e.g. "" + * @dataProvider provideEmptyValue + */ + public static function testIsSizeValueUsingEmptyValue($emptyValue) + { + self::assertFalse(Regex::isSizeValue($emptyValue)); + } + + /** + * @param string $description Description of test + * @param string $value Value to verify + * @param string $separator Separator used to split width and height + * @param bool $expected Expected result of verification + * + * @dataProvider provideSizeToVerify + */ + public function testIsSizeValue($description, $value, $separator, $expected) + { + self::assertEquals($expected, Regex::isSizeValue($value, $separator), $description); + } + /** * @param string $value Value that should be transformed to slug * @param string $expected Expected slug @@ -1788,6 +1810,191 @@ class RegexTest extends BaseTestCase ]; } + public function provideSizeToVerify() + { + yield[ + 'One number only', + 200, + ' x ', + false, + ]; + + yield[ + 'One number only as string', + '200', + ' x ', + false, + ]; + + yield[ + 'The " " as invalid separator', + '200 100', + ' x ', + false, + ]; + + yield[ + 'The "|" as separator (invalid separator)', + '200 | 100', + ' x ', + false, + ]; + + yield[ + 'The "|" as invalid separator and no spaces around separator', + '200|100', + ' x ', + false, + ]; + + yield[ + 'The "X" as invalid separator', + '200 X 100', + ' x ', + false, + ]; + + yield[ + 'Simple, valid size', + '200 x 100', + ' x ', + true, + ]; + + yield[ + 'Too much spaces at the right of separator', + '200 x 100', + ' x ', + true, + ]; + + yield[ + 'Too much spaces at the left of separator', + '200 x 100', + ' x ', + true, + ]; + + yield[ + 'Too much spaces around separator', + '200 x 100', + ' x ', + true, + ]; + + yield[ + 'Too much spaces before width', + ' 200 x 100', + ' x ', + true, + ]; + + yield[ + 'Too much spaces after height', + '200 x 100 ', + ' x ', + true, + ]; + + yield[ + 'Too much spaces before width and after height', + ' 200 x 100 ', + ' x ', + true, + ]; + + yield[ + 'Too much spaces everywhere (1st)', + ' 200 x 100 ', + ' x ', + true, + ]; + + yield[ + 'Too much spaces everywhere (2nd)', + ' 200 x 100 ', + ' x ', + true, + ]; + + yield[ + 'Too much spaces everywhere (3rd)', + ' 200 x 100 ', + ' x ', + true, + ]; + + yield[ + 'The " X " as custom separator', + '200 X 100', + ' X ', + true, + ]; + + yield[ + 'The "|" as custom separator', + '200|100', + '|', + true, + ]; + + yield[ + 'The " | " as custom separator', + '200 | 100', + ' | ', + true, + ]; + + yield[ + 'The "::" as custom separator', + '200::100', + '::', + true, + ]; + + yield[ + 'The " :: " as custom separator', + '200 :: 100', + ' :: ', + true, + ]; + + yield[ + 'The "." as custom separator', + '200.100', + '.', + true, + ]; + + yield[ + 'The " . " as custom separator', + '200 . 100', + ' . ', + true, + ]; + + yield[ + 'The "/" as custom separator', + '200/100', + '/', + true, + ]; + + yield[ + 'The " / " as custom separator', + '200 / 100', + ' / ', + true, + ]; + + yield[ + 'The " : " as custom separator and too much spaces everywhere', + ' 200 : 100 ', + ' : ', + true, + ]; + } + /** * {@inheritdoc} */ diff --git a/tests/ValueObject/SizeTest.php b/tests/ValueObject/SizeTest.php new file mode 100644 index 0000000..b3dbbbd --- /dev/null +++ b/tests/ValueObject/SizeTest.php @@ -0,0 +1,1095 @@ + + * @copyright Meritoo + */ +class SizeTest extends BaseTestCase +{ + public function testConstructor() + { + static::assertConstructorVisibilityAndArguments( + Size::class, + OopVisibilityType::IS_PRIVATE, + 3 + ); + } + + /** + * @param string $description Description of test + * @param Size|null $size Size to convert + * @param string $expected Expected result + * + * @dataProvider provideSizeForConvertingToString + */ + public function test__toString($description, $size, $expected) + { + static::assertEquals($expected, (string)$size, $description); + } + + public function testSetSeparator() + { + $size = Size::fromArray([ + 200, + 100, + ]); + + static::assertInstanceOf(Size::class, $size->setSeparator(' / ')); + static::assertSame('200 / 100', $size->toString()); + } + + /** + * @param string $description Description of test + * @param Size $size Size to get width + * @param bool $withUnit If is set to true, width is returned with unit ("px"). Otherwise - without. + * @param string|int $expected Expected width + * + * @dataProvider provideSizeToGetWidth + */ + public function testGetWidth($description, Size $size, $withUnit, $expected) + { + static::assertSame($expected, $size->getWidth($withUnit), $description); + } + + /** + * @param string $description Description of test + * @param Size $size Size to set width + * @param int|string $width The width + * @param string|int $expected Expected width + * + * @dataProvider provideSizeToSetWidth + */ + public function testSetWidth($description, Size $size, $width, $expected) + { + $result = $size->setWidth($width); + + static::assertInstanceOf(Size::class, $result, $description); + static::assertSame($expected, $size->getWidth(), $description); + } + + /** + * @param string $description Description of test + * @param Size $size Size to get width + * @param bool $withUnit If is set to true, width is returned with unit ("px"). Otherwise - without. + * @param string|int $expected Expected width + * + * @dataProvider provideSizeToGetHeight + */ + public function testGetHeight($description, Size $size, $withUnit, $expected) + { + static::assertSame($expected, $size->getHeight($withUnit), $description); + } + + /** + * @param string $description Description of test + * @param Size $size Size to set height + * @param int|string $height The height + * @param string|int $expected Expected height + * + * @dataProvider provideSizeToSetHeight + */ + public function testSetHeight($description, Size $size, $height, $expected) + { + $result = $size->setHeight($height); + + static::assertInstanceOf(Size::class, $result, $description); + static::assertSame($expected, $size->getHeight(), $description); + } + + /** + * @param string $description Description of test + * @param Size $size Size to convert + * @param bool $withUnit If is set to true, width and height are returned with unit ("px"). Otherwise - + * without. + * @param string $expected Expected result + * + * @dataProvider provideSizeForToString + */ + public function testToString($description, Size $size, $withUnit, $expected) + { + static::assertSame($expected, $size->toString($withUnit), $description); + } + + /** + * @param array $size Invalid size + * @dataProvider provideInvalidSizeAsArray + */ + public function testFromArrayUsingInvalidSizeAsArray(array $size) + { + $this->setExpectedException(InvalidSizeDimensionsException::class); + Size::fromArray($size); + } + + /** + * @param string $description Description of test + * @param array $size The size represented as array + * @param string $unit Unit used when width or height should be returned with unit + * @param Size $expected Expected result + * + * @dataProvider provideSizeForFromArray + */ + public function testFromArray($description, array $size, $unit, $expected) + { + static::assertEquals($expected, Size::fromArray($size, $unit), $description); + } + + /** + * @param string $description Description of test + * @param Size $size Size to convert + * @param bool $withUnit If is set to true, width and height are returned with unit ("px"). Otherwise - + * without. + * @param array $expected Expected result + * + * @dataProvider provideSizeForToArray + */ + public function testToArray($description, Size $size, $withUnit, array $expected) + { + static::assertSame($expected, $size->toArray($withUnit), $description); + } + + /** + * @param string $description Description of test + * @param string $size The size represented as string (width and height separated by "x") + * @param string $unit Unit used when width or height should be returned with unit + * @param string $separator Separator used to split width and height + * @param Size|null $expected Expected result + * + * @dataProvider provideSizeForFromString + */ + public function testFromString($description, $size, $unit, $separator, $expected) + { + static::assertEquals($expected, Size::fromString($size, $unit, $separator), $description); + } + + /** + * @param mixed $emptySize Empty value, e.g. "" + * @dataProvider provideEmptyValue + */ + public function testFromStringUsingEmptyValue($emptySize) + { + static::assertNull(Size::fromString($emptySize)); + } + + public function provideSizeForConvertingToString() + { + yield[ + 'Created using an empty array', + Size::fromArray([]), + '', + ]; + + yield[ + 'Created using an empty string', + Size::fromString(''), + '', + ]; + + yield[ + 'Created using an array with integers', + Size::fromArray([ + 200, + 100, + ]), + '200 x 100', + ]; + + yield[ + 'Created using an array with strings', + Size::fromArray([ + '200', + '100', + ]), + '200 x 100', + ]; + + yield[ + 'Created using simple string', + Size::fromString('200x100', '', 'x'), + '200x100', + ]; + + yield[ + 'Created using string with too much spaces everywhere', + Size::fromString(' 200 x 100 '), + '200 x 100', + ]; + } + + /** + * Provides invalid size (as an array) + * + * @return \Generator + */ + public function provideInvalidSizeAsArray() + { + yield[ + [ + 10, + -1, + ], + ]; + + yield[ + [ + -1, + 10, + ], + ]; + + yield[ + [ + -1, + -1, + ], + ]; + } + + public function provideSizeToGetWidth() + { + yield[ + 'Created using an array with integers', + Size::fromArray([ + 200, + 100, + ]), + false, + 200, + ]; + + yield[ + 'Created using an array with integers (with unit)', + Size::fromArray([ + 200, + 100, + ]), + true, + '200 px', + ]; + + yield[ + 'Created using an array with strings', + Size::fromArray([ + '200', + '100', + ]), + false, + 200, + ]; + + yield[ + 'Created using an array with strings (with unit)', + Size::fromArray([ + '200', + '100', + ]), + true, + '200 px', + ]; + + yield[ + 'Created using simple string', + Size::fromString('200 x 100'), + false, + 200, + ]; + + yield[ + 'Created using simple string (with unit)', + Size::fromString('200 x 100'), + true, + '200 px', + ]; + + yield[ + 'Created using simple string and custom separator', + Size::fromString('200 X 100', '', ' X '), + false, + 200, + ]; + + yield[ + 'Created using simple string, custom separator and custom unit (with unit)', + Size::fromString('200 : 100', 'mm', ' : '), + true, + '200 mm', + ]; + + yield[ + 'Created using string with too much spaces everywhere', + Size::fromString(' 200 x 100 '), + false, + 200, + ]; + + yield[ + 'Created using string with too much spaces everywhere (with unit)', + Size::fromString(' 200 x 100 '), + true, + '200 px', + ]; + } + + public function provideSizeToGetHeight() + { + yield[ + 'Created using an array with integers', + Size::fromArray([ + 200, + 100, + ]), + false, + 100, + ]; + + yield[ + 'Created using an array with integers (with unit)', + Size::fromArray([ + 200, + 100, + ]), + true, + '100 px', + ]; + + yield[ + 'Created using an array with strings', + Size::fromArray([ + '200', + '100', + ]), + false, + 100, + ]; + + yield[ + 'Created using an array with strings (with unit)', + Size::fromArray([ + '200', + '100', + ]), + true, + '100 px', + ]; + + yield[ + 'Created using simple string', + Size::fromString('200 x 100'), + false, + 100, + ]; + + yield[ + 'Created using simple string (with unit)', + Size::fromString('200 x 100'), + true, + '100 px', + ]; + + yield[ + 'Created using simple string and custom separator', + Size::fromString('200 X 100', '', ' X '), + false, + 100, + ]; + + yield[ + 'Created using simple string, custom separator and custom unit (with unit)', + Size::fromString('200 : 100', 'mm', ' : '), + true, + '100 mm', + ]; + + yield[ + 'Created using string with too much spaces everywhere', + Size::fromString(' 200 x 100 '), + false, + 100, + ]; + + yield[ + 'Created using string with too much spaces everywhere (with unit)', + Size::fromString(' 200 x 100 '), + true, + '100 px', + ]; + } + + public function provideSizeToSetWidth() + { + yield[ + 'Null as width', + Size::fromArray([ + 200, + 100, + ]), + null, + 0, + ]; + + yield[ + 'An empty string', + Size::fromArray([ + 200, + 100, + ]), + '', + 0, + ]; + + yield[ + 'Negative value', + Size::fromArray([ + 200, + 100, + ]), + -1, + -1, + ]; + + yield[ + 'Negative value as string', + Size::fromArray([ + 200, + 100, + ]), + '-1', + -1, + ]; + + yield[ + '0 as width', + Size::fromArray([ + 200, + 100, + ]), + 0, + 0, + ]; + + yield[ + 'Positive value', + Size::fromArray([ + 200, + 100, + ]), + 300, + 300, + ]; + + yield[ + 'Positive value as string', + Size::fromArray([ + 200, + 100, + ]), + '300', + 300, + ]; + } + + public function provideSizeToSetHeight() + { + yield[ + 'Null as height', + Size::fromArray([ + 200, + 100, + ]), + null, + 0, + ]; + + yield[ + 'An empty string', + Size::fromArray([ + 200, + 100, + ]), + '', + 0, + ]; + + yield[ + 'Negative value', + Size::fromArray([ + 200, + 100, + ]), + -1, + -1, + ]; + + yield[ + 'Negative value as string', + Size::fromArray([ + 200, + 100, + ]), + '-1', + -1, + ]; + + yield[ + '0 as height', + Size::fromArray([ + 200, + 100, + ]), + 0, + 0, + ]; + + yield[ + 'Positive value', + Size::fromArray([ + 200, + 100, + ]), + 300, + 300, + ]; + + yield[ + 'Positive value as string', + Size::fromArray([ + 200, + 100, + ]), + '300', + 300, + ]; + } + + public function provideSizeForToString() + { + yield[ + 'With unknown dimensions', + Size::fromArray([ + null, + null, + ]), + false, + '0 x 0', + ]; + + yield[ + 'With unknown dimensions (converting with unit)', + Size::fromArray([ + null, + null, + ]), + true, + '0 px x 0 px', + ]; + + yield[ + 'Created using an array with integers', + Size::fromArray([ + 200, + 100, + ]), + false, + '200 x 100', + ]; + + yield[ + 'Created using an array with integers (converting with unit)', + Size::fromArray([ + 200, + 100, + ]), + true, + '200 px x 100 px', + ]; + + yield[ + 'Created using an array with strings', + Size::fromArray([ + '200', + '100', + ]), + false, + '200 x 100', + ]; + + yield[ + 'Created using an array with strings (converting with unit)', + Size::fromArray([ + '200', + '100', + ]), + true, + '200 px x 100 px', + ]; + + yield[ + 'Created using simple string', + Size::fromString('200 x 100'), + false, + '200 x 100', + ]; + + yield[ + 'Created using simple string', + Size::fromString('200 x 100'), + false, + '200 x 100', + ]; + + yield[ + 'Created using simple string and custom separator', + Size::fromString('200 X 100', '', ' X '), + false, + '200 X 100', + ]; + + yield[ + 'Created using simple string, custom separator and custom unit (with unit)', + Size::fromString('200 : 100', 'mm', ' : '), + true, + '200 mm : 100 mm', + ]; + + yield[ + 'Created using simple string (converting with unit)', + Size::fromString('200 x 100'), + true, + '200 px x 100 px', + ]; + + yield[ + 'Created using string with too much spaces everywhere', + Size::fromString(' 200 x 100 '), + false, + '200 x 100', + ]; + + yield[ + 'Created using string with too much spaces everywhere (converting with unit)', + Size::fromString(' 200 x 100 '), + true, + '200 px x 100 px', + ]; + } + + public function provideSizeForToArray() + { + yield[ + 'Created using an array with integers', + Size::fromArray([ + 200, + 100, + ]), + false, + [ + 200, + 100, + ], + ]; + + yield[ + 'Created using an array with integers (converting with unit)', + Size::fromArray([ + 200, + 100, + ]), + true, + [ + '200 px', + '100 px', + ], + ]; + + yield[ + 'Created using an array with strings', + Size::fromArray([ + '200', + '100', + ]), + false, + [ + 200, + 100, + ], + ]; + + yield[ + 'Created using an array with strings (converting with unit)', + Size::fromArray([ + '200', + '100', + ]), + true, + [ + '200 px', + '100 px', + ], + ]; + + yield[ + 'Created using simple string', + Size::fromString('200 x 100'), + false, + [ + 200, + 100, + ], + ]; + + yield[ + 'Created using simple string and custom separator', + Size::fromString('200 X 100', '', ' X '), + false, + [ + 200, + 100, + ], + ]; + + yield[ + 'Created using simple string, custom separator and custom unit (with unit)', + Size::fromString('200 : 100', 'mm', ' : '), + true, + [ + '200 mm', + '100 mm', + ], + ]; + + yield[ + 'Created using simple string (converting with unit)', + Size::fromString('200 x 100'), + true, + [ + '200 px', + '100 px', + ], + ]; + + yield[ + 'Created using string with too much spaces everywhere', + Size::fromString(' 200 x 100 '), + false, + [ + 200, + 100, + ], + ]; + + yield[ + 'Created using string with too much spaces everywhere (converting with unit)', + Size::fromString(' 200 x 100 '), + true, + [ + '200 px', + '100 px', + ], + ]; + } + + public function provideSizeForFromString() + { + yield[ + 'One number only', + 200, + '', + ' x ', + null, + ]; + + yield[ + 'One number only as string', + '200', + '', + ' x ', + null, + ]; + + yield[ + 'The " " as invalid separator', + '200 100', + '', + ' x ', + null, + ]; + + yield[ + 'The "|" as separator (invalid separator)', + '200 | 100', + '', + ' x ', + null, + ]; + + yield[ + 'The "|" as invalid separator and no spaces around separator', + '200|100', + '', + ' x ', + null, + ]; + + yield[ + 'The "X" as invalid separator', + '200 X 100', + '', + ' x ', + null, + ]; + + yield[ + 'Simple, valid size', + '200 x 100', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Simple, valid size using custom separator', + '200 X 100', + 'px', + ' X ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' X '), + ]; + + yield[ + 'Too much spaces at the right of separator', + '200 x 100', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces at the left of separator', + '200 x 100', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces around separator', + '200 x 100', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces before width (1st)', + ' 200 x 100', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces before width (2nd) and custom separator', + ' 200 X 100', + 'px', + ' X ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' X '), + ]; + + yield[ + 'Too much spaces after height (1st)', + '200 x 100 ', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces after height (2nd) and custom separator', + '200 X 100 ', + 'px', + ' X ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' X '), + ]; + + yield[ + 'Too much spaces before width and after height (1st)', + ' 200 x 100 ', + 'km', + ' x ', + Size::fromArray( + [ + 200, + 100, + ], + 'km' + ), + ]; + + yield[ + 'Too much spaces before width and after height (2nd) and custom separator', + ' 200 X 100 ', + 'px', + ' X ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' X '), + ]; + + yield[ + 'Too much spaces everywhere (1st)', + ' 200 x 100 ', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces everywhere (2nd) and custom separator', + ' 200 X 100 ', + 'px', + ' X ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' X '), + ]; + + yield[ + 'Too much spaces everywhere (3rd)', + ' 200 x 100 ', + 'px', + ' x ', + Size::fromArray([ + 200, + 100, + ]), + ]; + + yield[ + 'Too much spaces everywhere (4th) and custom separator', + ' 200 : 100 ', + 'px', + ' : ', + Size::fromArray([ + 200, + 100, + ])->setSeparator(' : '), + ]; + + yield[ + 'Too much spaces everywhere (5th)', + ' 200 x 100 ', + 'mm', + ' x ', + Size::fromArray( + [ + 200, + 100, + ], + 'mm' + ), + ]; + } + + public function provideSizeForFromArray() + { + yield[ + 'An empty array', + [], + '', + null, + ]; + + yield[ + 'One number only', + [ + 200, + ], + '', + null, + ]; + + yield[ + 'One number only as string', + [ + '200', + ], + '', + null, + ]; + + yield[ + '0 as dimensions', + [ + 0, + 0, + ], + 'px', + Size::fromString('0 x 0'), + ]; + + yield[ + 'Simple, valid size', + [ + 200, + 100, + ], + 'px', + Size::fromString('200 x 100'), + ]; + + yield[ + 'Simple, valid size (using strings)', + [ + '200', + '100', + ], + 'mm', + Size::fromString('200 x 100', 'mm'), + ]; + } +} From 5022efb9a3f843748e67a8fc2fe20d298d9cf8ca Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sun, 24 Mar 2019 22:19:45 +0100 Subject: [PATCH 4/4] Minor refactoring --- src/Utilities/Arrays.php | 9 ++++----- src/ValueObject/Version.php | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Utilities/Arrays.php b/src/Utilities/Arrays.php index 8149741..f994950 100644 --- a/src/Utilities/Arrays.php +++ b/src/Utilities/Arrays.php @@ -397,8 +397,8 @@ class Arrays $counter = 0; $arrayCount = count($array); - $array = self::quoteStrings($array); - $isMultiDimensional = self::isMultiDimensional($array); + $arrayPrepared = self::quoteStrings($array); + $isMultiDimensional = self::isMultiDimensional($arrayPrepared); /* * Name of the variable was not provided and it's a multi dimensional array? @@ -419,7 +419,7 @@ class Arrays $result .= ');'; } - foreach ($array as $index => $value) { + foreach ($arrayPrepared as $index => $value) { ++$counter; if (is_array($value)) { @@ -1483,12 +1483,11 @@ class Arrays } $effect[$key] = $diff; - + } elseif ($value !== $array2[$key]) { /* * Value is different than in 2nd array? * OKay, I've got difference */ - } elseif ($value !== $array2[$key]) { $effect[$key] = $value; } } else { diff --git a/src/ValueObject/Version.php b/src/ValueObject/Version.php index 688dae7..55e7cb4 100644 --- a/src/ValueObject/Version.php +++ b/src/ValueObject/Version.php @@ -22,7 +22,7 @@ class Version * * @var int */ - private $majorPart; + protected $majorPart; /** * The "minor" part. @@ -30,7 +30,7 @@ class Version * * @var int */ - private $minorPart; + protected $minorPart; /** * The "patch" part. @@ -38,7 +38,7 @@ class Version * * @var int */ - private $patchPart; + protected $patchPart; /** * Class constructor