17 Commits
0.1.6 ... 0.2.0

Author SHA1 Message Date
PeterMarynczak
542a4a9405 Merge pull request #4 from wiosna-dev/feature/WZUW-265
[WZUW-265] Updated doctrine/orm package version
2023-09-08 11:53:07 +02:00
Piotr Marynczak
3d3fe767c4 [WZUW-265] Updated doctrine/orm package version 2023-09-07 16:37:05 +02:00
PeterMarynczak
00867c5091 Merge pull request #3 from wiosna-dev/feature/WZUW-265
[WZUW-265] Removed phploc package
2023-09-07 15:01:43 +02:00
Piotr Marynczak
14235b58ba [WZUW-265] Removed phploc package 2023-09-07 14:54:02 +02:00
xevolic
140acd1eb3 Merge pull request #2 from wiosna-dev/fix/UW-2934_Add_support_for_PHP_8.2
[UW-2934] chore(deps): enabled gedmo/doctrine-extensions in version 3.11 or higher
2023-03-09 15:23:55 +01:00
Tomasz Kuter
4eaca747e7 [UW-2934] chore(deps): enabled gedmo/doctrine-extensions in version 3.11 or higher 2023-02-14 20:06:29 +01:00
Krzysztof Nizioł
28a11d611c Merge branch 'master' of github.com:meritoo/common-library 2019-03-25 09:23:57 +01:00
Meritoo
5022efb9a3 Minor refactoring 2019-03-24 22:19:45 +01:00
Meritoo
56b058ca1d Size, e.g. of image 2019-03-24 22:15:54 +01:00
Meritoo
eade6a25ad Collection > the getByIndex() method > returns element with given index 2019-03-16 19:58:02 +01:00
Meritoo
9f6af6b6a4 Collection > create trait (to make it more flexible) 2019-03-16 12:37:35 +01:00
Krzysztof Nizioł
df36e050e7 Merge branch 'master' of github.com:meritoo/common-library 2019-03-05 10:21:44 +01:00
Krzysztof Nizioł
ddd558a7d4 Merge remote-tracking branch 'meritoo/master' 2019-02-23 13:29:37 +01:00
Krzysztof Nizioł
e31af27c01 Merge branch 'master' of github.com:meritoo/common-library
# Conflicts:
#	README.md
2018-09-06 22:01:00 +02:00
Krzysztof Niziol
3bcda8e906 composer.json - update name of this package (name of vendor, actually) 2017-11-08 14:52:19 +01:00
Krzysztof Niziol
1641c50d1d composer.json - update name of this package 2017-11-07 16:34:49 +01:00
Krzysztof Niziol
afbbdfe437 composer.json - update name of this package 2017-11-07 15:24:51 +01:00
21 changed files with 2256 additions and 276 deletions

View File

@@ -2,6 +2,14 @@
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)
# 0.1.6
1. Arrays > refactoring & more tests

View File

@@ -6,10 +6,22 @@ Common and useful classes, methods, exceptions etc.
# Installation
In your `composer.json` add address of repository into `repositories` section:
```json
"repositories": [
(...)
{
"type": "vcs",
"url": "https://github.com/wiosna-dev/common-library"
}
]
```
Run [Composer](https://getcomposer.org) to install this package in your project:
```bash
composer require meritoo/common-library
composer require wiosna-dev/common-library
```
> [How to install Composer?](https://getcomposer.org/download)

View File

@@ -1 +1 @@
0.1.6
0.1.8

View File

@@ -1,5 +1,5 @@
{
"name": "meritoo/common-library",
"name": "wiosna-dev/common-library",
"description": "Useful classes, methods, extensions etc.",
"license": "MIT",
"authors": [
@@ -10,20 +10,19 @@
}
],
"require": {
"php": ">=5.6",
"ext-dom": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"ext-simplexml": "*",
"php": ">=5.6",
"ext-intl": "*",
"ext-pcre": "*",
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4"
"doctrine/orm": "^2.16",
"gedmo/doctrine-extensions": "^2.4 || ^3.11"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.2",
"pdepend/pdepend": "^2.5",
"phploc/phploc": "^2.1",
"phpmd/phpmd": "^2.6",
"phpunit/phpunit": "^4.8",
"sebastian/phpcpd": "^2.0",

View File

@@ -177,6 +177,68 @@ $human2 = new Human('John', 'Scott', 'john@scott.com', new \DateTime('2001-01-01
$asString2 = (string)$human2; // "John Scott <john@scott.com>"
```
### 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`).

View File

@@ -45,8 +45,7 @@
depends="check:cs,
check:md,
check:cpd,
check:depend,
check:loc"
check:depend"
/>
<!-- Test target -->
@@ -91,13 +90,6 @@
</phpdepend>
</target>
<!-- Measure the size and analyzing the structure of a project -->
<target name="check:loc" depends="build:prepare">
<phploc reportType="txt" reportName="phploc" reportDirectory="${dir.reports}">
<fileset refid="sourcecode"/>
</phploc>
</target>
<!-- PHPUnit tests -->
<target name="test:phpunit" depends="build:prepare">
<exec command="${tests.phpunit.command}" passthru="true"/>

View File

@@ -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);
}
}

View 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\ValueObject;
/**
* An exception used while dimensions of size, passed to the instance of Size class, are invalid
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -0,0 +1,67 @@
<?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\Traits\Collection;
/**
* Trait for the Collection required by ArrayAccess interface
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Traits\Collection;
/**
* Trait for the Collection required by Countable interface
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait CountableTrait
{
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->elements);
}
}

View File

@@ -0,0 +1,28 @@
<?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\Traits\Collection;
use ArrayIterator;
/**
* Trait for the Collection required by IteratorAggregate interface
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait IteratorAggregateTrait
{
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->elements);
}
}

View File

@@ -0,0 +1,215 @@
<?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\Traits\Collection;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Utilities\Arrays;
/**
* Main trait for the Collection
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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 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
*
* @return array
*/
public function toArray()
{
return $this->elements;
}
}

View File

@@ -0,0 +1,28 @@
<?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\Traits;
use Meritoo\Common\Traits\Collection\ArrayAccessTrait;
use Meritoo\Common\Traits\Collection\CountableTrait;
use Meritoo\Common\Traits\Collection\IteratorAggregateTrait;
use Meritoo\Common\Traits\Collection\MainTrait;
/**
* Trait for the Collection
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait CollectionTrait
{
use MainTrait;
use CountableTrait;
use ArrayAccessTrait;
use IteratorAggregateTrait;
}

View File

@@ -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 {

View File

@@ -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
*

245
src/ValueObject/Size.php Normal file
View File

@@ -0,0 +1,245 @@
<?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;
use Meritoo\Common\Exception\ValueObject\InvalidSizeDimensionsException;
use Meritoo\Common\Utilities\Regex;
/**
* Size, e.g. of image
*
* Instance of this class may be created using static methods:
* - fromString()
* - fromArray()
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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;
}
}

View File

@@ -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

View File

@@ -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}
*/

View File

@@ -0,0 +1,67 @@
<?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\Exception\ValueObject;
use Meritoo\Common\Exception\ValueObject\InvalidSizeDimensionsException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of an exception used while dimensions of size, passed to the instance of Size class, are invalid
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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),
];
}
}

View File

@@ -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}
*/

File diff suppressed because it is too large Load Diff