Rename Collection class to BaseCollection. Add BaseCollection::isValidType() method to validate type of element before add it to collection. Add BaseCollection ::prepareElements() method to allow preparation of elements in custom way.

This commit is contained in:
Meritoo
2019-09-18 14:51:09 +02:00
parent b91606ada9
commit a7c39b26ba
17 changed files with 627 additions and 98 deletions

View File

@@ -2,6 +2,12 @@
Common and useful classes, methods, exceptions etc.
# 1.1.0
1. Rename Meritoo\Common\Collection\Collection class to Meritoo\Common\Collection\BaseCollection.
Add BaseCollection::isValidType() method to validate type of element before add it to collection.
Add BaseCollection ::prepareElements() method to allow preparation of elements in custom way.
# 1.0.6
1. Use `.env` instead of `.env.dist`

View File

@@ -1 +1 @@
1.0.6
1.1.0

View File

@@ -0,0 +1,81 @@
<?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\Collection;
use ArrayAccess;
use Countable;
use IteratorAggregate;
use Meritoo\Common\Traits\CollectionTrait;
/**
* Collection of elements with the same type
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggregate
{
use CollectionTrait;
/**
* Class constructor
*
* @param array $elements (optional) The elements of collection
*/
public function __construct(array $elements = [])
{
$validated = $this->getElementsWithValidType($elements);
$this->elements = $this->prepareElements($validated);
}
/**
* Prepares elements to initialize the collection.
* Feel free to override and prepare elements in your way.
*
* @param array $elements The elements of collection to prepare
* @return array
*/
protected function prepareElements(array $elements): array
{
return $elements;
}
/**
* Returns elements of collection with valid types
*
* @param array $elements The elements of collection to verify
* @return array
*/
protected function getElementsWithValidType(array $elements): array
{
if (empty($elements)) {
return [];
}
$result = [];
foreach ($elements as $index => $element) {
if (!$this->isValidType($element)) {
continue;
}
$result[$index] = $element;
}
return $result;
}
/**
* Returns information if given element has valid type
*
* @param mixed $element Element of collection
* @return bool
*/
abstract protected function isValidType($element): bool;
}

View File

@@ -1,36 +0,0 @@
<?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\Collection;
use ArrayAccess;
use Countable;
use IteratorAggregate;
use Meritoo\Common\Traits\CollectionTrait;
/**
* Collection of elements.
* It's a set of some elements, e.g. objects.
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class Collection implements Countable, ArrayAccess, IteratorAggregate
{
use CollectionTrait;
/**
* Class constructor
*
* @param array $elements (optional) The elements of collection
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
}

View File

@@ -0,0 +1,27 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Collection;
use DateTime;
/**
* Collection of DateTime instances
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class DateTimeCollection extends BaseCollection
{
protected function isValidType($element): bool
{
return $element instanceof DateTime;
}
}

View File

@@ -0,0 +1,25 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Collection;
/**
* Collection of integers
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class IntegerCollection extends BaseCollection
{
protected function isValidType($element): bool
{
return is_int($element);
}
}

View File

@@ -0,0 +1,25 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Collection;
/**
* Collection of strings
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class StringCollection extends BaseCollection
{
protected function isValidType($element): bool
{
return is_string($element);
}
}

View File

@@ -19,7 +19,7 @@ use Meritoo\Common\ValueObject\Template;
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class Templates extends Collection
class Templates extends BaseCollection
{
/**
* Finds and returns template with given index
@@ -61,4 +61,12 @@ class Templates extends Collection
return $result;
}
/**
* {@inheritdoc}
*/
protected function isValidType($element): bool
{
return $element instanceof Template;
}
}

View File

@@ -8,7 +8,7 @@
namespace Meritoo\Common\Traits\Collection;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Collection\BaseCollection;
/**
* Trait for the Collection with add*() methods
@@ -26,6 +26,10 @@ trait AddTrait
*/
public function add($element, $index = null): void
{
if (!$this->isValidType($element)) {
return;
}
if (null === $index || '' === $index) {
$this->elements[] = $element;
@@ -38,8 +42,8 @@ trait AddTrait
/**
* 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
* @param array|BaseCollection $elements The elements to add
* @param bool $useIndexes (optional) If is set to true, indexes of given elements will be used in
* this collection. Otherwise - not.
*/
public function addMultiple($elements, bool $useIndexes = false): void

View File

@@ -10,7 +10,7 @@ namespace Meritoo\Common\Utilities;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Common\Persistence\Proxy;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Collection\BaseCollection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
@@ -207,7 +207,7 @@ class Reflection
* Returns values of given property for given objects.
* Looks for proper getter for the property.
*
* @param array|Collection|object $objects The objects that should contain given property. It may be also one
* @param array|BaseCollection|object $objects The objects that should contain given property. It may be also one
* object.
* @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
@@ -224,7 +224,7 @@ class Reflection
return [];
}
if ($objects instanceof Collection) {
if ($objects instanceof BaseCollection) {
$objects = $objects->toArray();
}

View File

@@ -10,9 +10,12 @@ namespace Meritoo\Test\Common\Collection;
use ArrayIterator;
use Generator;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Collection\BaseCollection;
use Meritoo\Common\Collection\DateTimeCollection;
use Meritoo\Common\Collection\StringCollection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Test\Common\Collection\Collection\ArrayCollection;
use ReflectionClass;
/**
@@ -22,21 +25,21 @@ use ReflectionClass;
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @covers \Meritoo\Common\Collection\Collection
* @covers \Meritoo\Common\Collection\BaseCollection
*/
class CollectionTest extends BaseTestCase
class BaseCollectionTest extends BaseTestCase
{
/**
* An empty collection
*
* @var Collection
* @var StringCollection
*/
private $emptyCollection;
/**
* Simple collection
*
* @var Collection
* @var StringCollection
*/
private $simpleCollection;
@@ -138,11 +141,11 @@ class CollectionTest extends BaseTestCase
* @param mixed $element The element to add
* @param int $expectedCount Expected count of elements in collection
* @param int $expectedIndex Expected index of added element in collection
* @param Collection $collection The collection
* @param BaseCollection $collection The collection
*
* @dataProvider provideElementToAdd
*/
public function testAddWithoutIndex($element, $expectedCount, $expectedIndex, Collection $collection)
public function testAddWithoutIndex($element, $expectedCount, $expectedIndex, BaseCollection $collection)
{
$collection->add($element);
@@ -156,11 +159,11 @@ class CollectionTest extends BaseTestCase
* @param mixed $index Index of element to add
* @param int $expectedCount Expected count of elements in collection
* @param int $expectedIndex Expected index of added element in collection
* @param Collection $collection The collection
* @param BaseCollection $collection The collection
*
* @dataProvider provideElementToAddWithIndex
*/
public function testAddWithIndex($element, $index, $expectedCount, $expectedIndex, Collection $collection)
public function testAddWithIndex($element, $index, $expectedCount, $expectedIndex, BaseCollection $collection)
{
$collection->add($element, $index);
@@ -328,7 +331,7 @@ class CollectionTest extends BaseTestCase
public function testExistsVisibilityAndArguments()
{
$reflectionClass = new ReflectionClass(Collection::class);
$reflectionClass = new ReflectionClass(BaseCollection::class);
$method = $reflectionClass->getMethod('exists');
static::assertMethodVisibility($method, OopVisibilityType::IS_PRIVATE);
@@ -337,17 +340,30 @@ class CollectionTest extends BaseTestCase
/**
* @param string $description Description of test
* @param Collection $collection Collection to search for element with given index
* @param BaseCollection $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)
public function testGetByIndex($description, BaseCollection $collection, $index, $expected)
{
static::assertEquals($expected, $collection->getByIndex($index), $description);
}
/**
* @param string $description
* @param array $elements
* @param array $expected
*
* @dataProvider provideElementsToValidateType
*/
public function testGetElementsWithValidType(string $description, array $elements, array $expected): void
{
$collection = new ArrayCollection($elements);
static::assertSame($expected, $collection->toArray(), $description);
}
/**
* Provides element to add to collection
*
@@ -359,14 +375,14 @@ class CollectionTest extends BaseTestCase
'This is test 1',
1,
0,
new Collection(),
new StringCollection(),
];
yield[
'This is test 2',
2,
1,
new Collection([
new StringCollection([
'I am 1st',
]),
];
@@ -375,7 +391,7 @@ class CollectionTest extends BaseTestCase
'This is test 3',
3,
2,
new Collection([
new StringCollection([
'I am 1st',
'I am 2nd',
]),
@@ -394,7 +410,7 @@ class CollectionTest extends BaseTestCase
'test1',
1,
'test1',
new Collection(),
new StringCollection(),
];
yield[
@@ -402,7 +418,7 @@ class CollectionTest extends BaseTestCase
'test2',
2,
'test2',
new Collection([
new StringCollection([
'test1' => 'I am 1st',
]),
];
@@ -412,7 +428,7 @@ class CollectionTest extends BaseTestCase
null,
3,
0,
new Collection([
new StringCollection([
'test1' => 'I am 1st',
'test2' => 'I am 2nd',
]),
@@ -423,7 +439,7 @@ class CollectionTest extends BaseTestCase
'',
4,
1,
new Collection([
new StringCollection([
'test1' => 'I am 1st',
'test2' => 'I am 2nd',
'I am 3rd',
@@ -435,7 +451,7 @@ class CollectionTest extends BaseTestCase
'test5',
5,
'test5',
new Collection([
new StringCollection([
'test1' => 'I am 1st',
'test2' => 'I am 2nd',
2 => 'I am 3rd',
@@ -448,7 +464,7 @@ class CollectionTest extends BaseTestCase
'test2',
4,
'test2',
new Collection([
new StringCollection([
'test1' => 'I am 1st',
'test2' => 'I am 2nd',
2 => 'I am 3rd',
@@ -461,21 +477,21 @@ class CollectionTest extends BaseTestCase
{
yield[
'An empty collection and empty index',
new Collection(),
new StringCollection(),
'',
null,
];
yield[
'An empty collection and non-empty index',
new Collection(),
new StringCollection(),
'test',
null,
];
yield[
'Non-empty collection and not existing index',
new Collection([
new StringCollection([
'lorem' => 'ipsum',
'dolor' => 'sit',
]),
@@ -485,7 +501,7 @@ class CollectionTest extends BaseTestCase
yield[
'Collection with existing index',
new Collection([
new StringCollection([
'lorem' => 'ipsum',
'dolor' => 'sit',
]),
@@ -495,7 +511,7 @@ class CollectionTest extends BaseTestCase
yield[
'Collection with existing index (collection of arrays)',
new Collection([
new ArrayCollection([
[
'lorem',
'ipsum',
@@ -514,7 +530,7 @@ class CollectionTest extends BaseTestCase
yield[
'Collection with existing index (collection of objects)',
new Collection([
new DateTimeCollection([
'x' => new \DateTime(),
'y' => new \DateTime('2001-01-01'),
'z' => new \DateTime('yesterday'),
@@ -524,6 +540,56 @@ class CollectionTest extends BaseTestCase
];
}
public function provideElementsToValidateType(): ?Generator
{
yield[
'An empty array',
[],
[],
];
yield[
'Valid elements only',
[
[],
[
'123',
456,
],
],
[
[],
[
'123',
456,
],
],
];
yield[
'Mixed elements',
[
1,
'test',
'',
[],
234,
'test',
[
'123',
456,
],
],
[
3 => [],
6 => [
'123',
456,
],
],
];
}
/**
* {@inheritdoc}
*/
@@ -538,7 +604,7 @@ class CollectionTest extends BaseTestCase
345 => 'sit',
];
$this->emptyCollection = new Collection();
$this->simpleCollection = new Collection($this->simpleElements);
$this->emptyCollection = new StringCollection();
$this->simpleCollection = new StringCollection($this->simpleElements);
}
}

View File

@@ -0,0 +1,30 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Test\Common\Collection\Collection;
use Meritoo\Common\Collection\BaseCollection;
/**
* Collection of arrays
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @coversNothing
*/
class ArrayCollection extends BaseCollection
{
protected function isValidType($element): bool
{
return is_array($element);
}
}

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Meritoo\Test\Common\Collection;
use DateTime;
use Generator;
use Meritoo\Common\Collection\DateTimeCollection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of the collection of DateTime instances
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @covers \Meritoo\Common\Collection\DateTimeCollection
*/
class DateTimeCollectionTest extends BaseTestCase
{
public function testConstructor(): void
{
static::assertConstructorVisibilityAndArguments(
DateTimeCollection::class,
OopVisibilityType::IS_PUBLIC,
1
);
}
/**
* @param string $description
* @param array $elements
* @param array $expectedElements
*
* @dataProvider provideDifferentTypesOfElements
*/
public function testCreateWithDifferentTypesOfElements(
string $description,
array $elements,
array $expectedElements
): void {
$collection = new DateTimeCollection($elements);
static::assertEquals($expectedElements, $collection->toArray(), $description);
}
public function provideDifferentTypesOfElements(): ?Generator
{
yield[
'An empty array',
[],
[],
];
yield[
'Valid elements only',
[
new DateTime('2001-01-01'),
new DateTime('2001-01-02'),
],
[
new DateTime('2001-01-01'),
new DateTime('2001-01-02'),
],
];
yield[
'Mixed elements',
[
1,
'test',
new DateTime('2001-01-01'),
'',
[],
234,
new DateTime('2001-01-02'),
],
[
2 => new DateTime('2001-01-01'),
6 => new DateTime('2001-01-02'),
],
];
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Meritoo\Test\Common\Collection;
use Meritoo\Common\Collection\IntegerCollection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of the collection of integers
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @covers \Meritoo\Common\Collection\IntegerCollection
*/
class IntegerCollectionTest extends BaseTestCase
{
public function testConstructor(): void
{
static::assertConstructorVisibilityAndArguments(
IntegerCollection::class,
OopVisibilityType::IS_PUBLIC,
1
);
}
/**
* @param string $description
* @param array $elements
* @param array $expectedElements
*
* @dataProvider provideDifferentTypesOfElements
*/
public function testCreateWithDifferentTypesOfElements(
string $description,
array $elements,
array $expectedElements
): void {
$collection = new IntegerCollection($elements);
static::assertSame($expectedElements, $collection->toArray(), $description);
}
public function provideDifferentTypesOfElements(): ?\Generator
{
yield[
'An empty array',
[],
[],
];
yield[
'Valid elements only',
[
1,
2,
3,
],
[
1,
2,
3,
],
];
yield[
'Mixed elements',
[
1,
'test',
'',
[],
234,
'test',
],
[
0 => 1,
4 => 234,
],
];
}
}

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Meritoo\Test\Common\Collection;
use Meritoo\Common\Collection\StringCollection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of the collection of strings
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @covers \Meritoo\Common\Collection\StringCollection
*/
class StringCollectionTest extends BaseTestCase
{
public function testConstructor(): void
{
static::assertConstructorVisibilityAndArguments(
StringCollection::class,
OopVisibilityType::IS_PUBLIC,
1
);
}
/**
* @param string $description
* @param array $elements
* @param array $expectedElements
*
* @dataProvider provideDifferentTypesOfElements
*/
public function testCreateWithDifferentTypesOfElements(
string $description,
array $elements,
array $expectedElements
): void {
$collection = new StringCollection($elements);
static::assertSame($expectedElements, $collection->toArray(), $description);
}
public function provideDifferentTypesOfElements(): ?\Generator
{
yield[
'An empty array',
[],
[],
];
yield[
'Valid elements only',
[
'1',
'test',
'',
],
[
'1',
'test',
'',
],
];
yield[
'Mixed elements',
[
1,
'test',
'',
[],
234,
'test',
],
[
1 => 'test',
2 => '',
5 => 'test',
],
];
}
}

View File

@@ -0,0 +1,34 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Test\Common\Utilities\Reflection;
use Meritoo\Common\Collection\BaseCollection;
/**
* Collection of objects
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @coversNothing
*/
class ObjectsCollection extends BaseCollection
{
protected function isValidType($element): bool
{
return $element instanceof A
|| $element instanceof B
|| $element instanceof C
|| $element instanceof D
|| $element instanceof F;
}
}

View File

@@ -10,7 +10,8 @@ namespace Meritoo\Test\Common\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Collection\BaseCollection;
use Meritoo\Common\Collection\Templates;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
@@ -27,6 +28,7 @@ use Meritoo\Test\Common\Utilities\Reflection\G;
use Meritoo\Test\Common\Utilities\Reflection\H;
use Meritoo\Test\Common\Utilities\Reflection\I;
use Meritoo\Test\Common\Utilities\Reflection\J;
use Meritoo\Test\Common\Utilities\Reflection\ObjectsCollection;
use ReflectionProperty;
use stdClass;
@@ -83,13 +85,13 @@ class ReflectionTest extends BaseTestCase
public function testGetClassWhileNamespaceContainsClassName(): void
{
self::assertEquals(
Collection::class,
Reflection::getClassName(Collection::class)
BaseCollection::class,
Reflection::getClassName(BaseCollection::class)
);
self::assertEquals(
'Collection',
Reflection::getClassName(Collection::class, true)
'BaseCollection',
Reflection::getClassName(BaseCollection::class, true)
);
}
@@ -118,7 +120,7 @@ class ReflectionTest extends BaseTestCase
{
self::assertEquals(
'Meritoo\Common\Collection',
Reflection::getClassNamespace(Collection::class)
Reflection::getClassNamespace(BaseCollection::class)
);
}
@@ -320,7 +322,7 @@ class ReflectionTest extends BaseTestCase
public function testGetPropertyValuesFromEmptySource(): void
{
self::assertEquals([], Reflection::getPropertyValues([], 'something'));
self::assertEquals([], Reflection::getPropertyValues(new Collection(), 'something'));
self::assertEquals([], Reflection::getPropertyValues(new Templates(), 'something'));
}
public function testGetPropertyValuesOfNotExistingPropertyFromSingleObject(): void
@@ -344,7 +346,7 @@ class ReflectionTest extends BaseTestCase
self::assertEquals([], Reflection::getPropertyValues($objects, 'something'));
self::assertEquals([], Reflection::getPropertyValues($objects, 'something', true));
$collection = new Collection($objects);
$collection = new ObjectsCollection($objects);
self::assertEquals([], Reflection::getPropertyValues($collection, 'something'));
self::assertEquals([], Reflection::getPropertyValues($collection, 'something', true));
@@ -373,7 +375,7 @@ class ReflectionTest extends BaseTestCase
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'city'));
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'city', true));
$collection = new Collection($objects);
$collection = new ObjectsCollection($objects);
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'city'));
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'city', true));
@@ -408,7 +410,7 @@ class ReflectionTest extends BaseTestCase
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'g.firstName'));
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'g.firstName', true));
$collection = new Collection($objects);
$collection = new ObjectsCollection($objects);
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'g.firstName'));
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'g.firstName', true));