mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Create and implement CollectionInterface as contract of all collections (e.g. based on the BaseCollection class)
This commit is contained in:
@@ -5,6 +5,7 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
# 1.1.3
|
# 1.1.3
|
||||||
|
|
||||||
1. Move `Renderable` class: `Meritoo\Common` -> `Meritoo\Common\Contract`
|
1. Move `Renderable` class: `Meritoo\Common` -> `Meritoo\Common\Contract`
|
||||||
|
2. Create and implement `CollectionInterface` as contract of all collections (e.g. based on the `BaseCollection` class)
|
||||||
|
|
||||||
# 1.1.2
|
# 1.1.2
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,11 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Meritoo\Common\Collection;
|
namespace Meritoo\Common\Collection;
|
||||||
|
|
||||||
use ArrayAccess;
|
use Meritoo\Common\Contract\Collection\CollectionInterface;
|
||||||
use Countable;
|
|
||||||
use IteratorAggregate;
|
|
||||||
use Meritoo\Common\Traits\CollectionTrait;
|
use Meritoo\Common\Traits\CollectionTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,7 +19,7 @@ use Meritoo\Common\Traits\CollectionTrait;
|
|||||||
* @author Meritoo <github@meritoo.pl>
|
* @author Meritoo <github@meritoo.pl>
|
||||||
* @copyright Meritoo <http://www.meritoo.pl>
|
* @copyright Meritoo <http://www.meritoo.pl>
|
||||||
*/
|
*/
|
||||||
abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggregate
|
abstract class BaseCollection implements CollectionInterface
|
||||||
{
|
{
|
||||||
use CollectionTrait;
|
use CollectionTrait;
|
||||||
|
|
||||||
|
|||||||
52
src/Contract/Collection/CollectionInterface.php
Normal file
52
src/Contract/Collection/CollectionInterface.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?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\Contract\Collection;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
|
use Countable;
|
||||||
|
use IteratorAggregate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface/Contract of collection of elements with the same type
|
||||||
|
*
|
||||||
|
* @author Meritoo <github@meritoo.pl>
|
||||||
|
* @copyright Meritoo <http://www.meritoo.pl>
|
||||||
|
*/
|
||||||
|
interface CollectionInterface extends Countable, ArrayAccess, IteratorAggregate
|
||||||
|
{
|
||||||
|
public function toArray(): array;
|
||||||
|
|
||||||
|
public function add($element, $index = null): void;
|
||||||
|
|
||||||
|
public function addMultiple($elements, bool $useIndexes = false): void;
|
||||||
|
|
||||||
|
public function prepend($element): void;
|
||||||
|
|
||||||
|
public function remove($element): void;
|
||||||
|
|
||||||
|
public function getPrevious($element);
|
||||||
|
|
||||||
|
public function getNext($element);
|
||||||
|
|
||||||
|
public function getFirst();
|
||||||
|
|
||||||
|
public function getLast();
|
||||||
|
|
||||||
|
public function getByIndex($index);
|
||||||
|
|
||||||
|
public function isEmpty(): bool;
|
||||||
|
|
||||||
|
public function isFirst($element): bool;
|
||||||
|
|
||||||
|
public function isLast($element): bool;
|
||||||
|
|
||||||
|
public function has($element): bool;
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ namespace Meritoo\Common\Contract\Renderable;
|
|||||||
use Meritoo\Common\Collection\Templates;
|
use Meritoo\Common\Collection\Templates;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Something that may be rendered
|
* Interface/Contract of something that may be rendered
|
||||||
*
|
*
|
||||||
* @author Meritoo <github@meritoo.pl>
|
* @author Meritoo <github@meritoo.pl>
|
||||||
* @copyright Meritoo <http://www.meritoo.pl>
|
* @copyright Meritoo <http://www.meritoo.pl>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
namespace Meritoo\Common\Traits\Collection;
|
namespace Meritoo\Common\Traits\Collection;
|
||||||
|
|
||||||
use Meritoo\Common\Collection\BaseCollection;
|
use Meritoo\Common\Contract\Collection\CollectionInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait for the Collection with add*() methods
|
* Trait for the Collection with add*() methods
|
||||||
@@ -42,9 +42,9 @@ trait AddTrait
|
|||||||
/**
|
/**
|
||||||
* Adds given elements (at the end of collection)
|
* Adds given elements (at the end of collection)
|
||||||
*
|
*
|
||||||
* @param array|BaseCollection $elements The elements to add
|
* @param array|CollectionInterface $elements The elements to add
|
||||||
* @param bool $useIndexes (optional) If is set to true, indexes of given elements will be used in
|
* @param bool $useIndexes (optional) If is set to true, indexes of given elements will be
|
||||||
* this collection. Otherwise - not.
|
* used in this collection. Otherwise - not.
|
||||||
*/
|
*/
|
||||||
public function addMultiple($elements, bool $useIndexes = false): void
|
public function addMultiple($elements, bool $useIndexes = false): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Meritoo\Common\Traits;
|
namespace Meritoo\Common\Traits;
|
||||||
|
|
||||||
use Meritoo\Common\Traits\Collection\AddTrait;
|
use Meritoo\Common\Traits\Collection\AddTrait;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Meritoo\Common\Utilities;
|
|||||||
|
|
||||||
use Doctrine\Inflector\InflectorFactory;
|
use Doctrine\Inflector\InflectorFactory;
|
||||||
use Doctrine\Persistence\Proxy;
|
use Doctrine\Persistence\Proxy;
|
||||||
use Meritoo\Common\Collection\BaseCollection;
|
use Meritoo\Common\Contract\Collection\CollectionInterface;
|
||||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||||
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
||||||
@@ -207,11 +207,11 @@ class Reflection
|
|||||||
* Returns values of given property for given objects.
|
* Returns values of given property for given objects.
|
||||||
* Looks for proper getter for the property.
|
* Looks for proper getter for the property.
|
||||||
*
|
*
|
||||||
* @param array|BaseCollection|object $objects The objects that should contain given property. It may be also one
|
* @param array|CollectionInterface|object $objects The objects that should contain given property. It may be also
|
||||||
* object.
|
* one object.
|
||||||
* @param string $property Name of the property that contains a value
|
* @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
|
* @param bool $force (optional) If is set to true, try to retrieve value even if
|
||||||
* object does not have property. Otherwise - not.
|
* the object does not have property. Otherwise - not.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function getPropertyValues($objects, string $property, bool $force = false): array
|
public static function getPropertyValues($objects, string $property, bool $force = false): array
|
||||||
@@ -224,7 +224,7 @@ class Reflection
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($objects instanceof BaseCollection) {
|
if ($objects instanceof CollectionInterface) {
|
||||||
$objects = $objects->toArray();
|
$objects = $objects->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use Generator;
|
|||||||
use Meritoo\Common\Collection\BaseCollection;
|
use Meritoo\Common\Collection\BaseCollection;
|
||||||
use Meritoo\Common\Collection\DateTimeCollection;
|
use Meritoo\Common\Collection\DateTimeCollection;
|
||||||
use Meritoo\Common\Collection\StringCollection;
|
use Meritoo\Common\Collection\StringCollection;
|
||||||
|
use Meritoo\Common\Contract\Collection\CollectionInterface;
|
||||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\Test\Common\Collection\BaseCollection\FirstNamesCollection;
|
use Meritoo\Test\Common\Collection\BaseCollection\FirstNamesCollection;
|
||||||
@@ -139,15 +140,19 @@ class BaseCollectionTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $element The element to add
|
* @param mixed $element The element to add
|
||||||
* @param int $expectedCount Expected count of elements in collection
|
* @param int $expectedCount Expected count of elements in collection
|
||||||
* @param int $expectedIndex Expected index of added element in collection
|
* @param int $expectedIndex Expected index of added element in collection
|
||||||
* @param BaseCollection $collection The collection
|
* @param CollectionInterface $collection The collection
|
||||||
*
|
*
|
||||||
* @dataProvider provideElementToAdd
|
* @dataProvider provideElementToAdd
|
||||||
*/
|
*/
|
||||||
public function testAddWithoutIndex($element, $expectedCount, $expectedIndex, BaseCollection $collection)
|
public function testAddWithoutIndex(
|
||||||
{
|
$element,
|
||||||
|
int $expectedCount,
|
||||||
|
int $expectedIndex,
|
||||||
|
CollectionInterface $collection
|
||||||
|
) {
|
||||||
$collection->add($element);
|
$collection->add($element);
|
||||||
|
|
||||||
static::assertTrue($collection->has($element));
|
static::assertTrue($collection->has($element));
|
||||||
@@ -156,15 +161,15 @@ class BaseCollectionTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $element The element to add
|
* @param mixed $element The element to add
|
||||||
* @param mixed $index Index of element to add
|
* @param mixed $index Index of element to add
|
||||||
* @param int $expectedCount Expected count of elements in collection
|
* @param int $expectedCount Expected count of elements in collection
|
||||||
* @param int $expectedIndex Expected index of added element in collection
|
* @param int $expectedIndex Expected index of added element in collection
|
||||||
* @param BaseCollection $collection The collection
|
* @param CollectionInterface $collection The collection
|
||||||
*
|
*
|
||||||
* @dataProvider provideElementToAddWithIndex
|
* @dataProvider provideElementToAddWithIndex
|
||||||
*/
|
*/
|
||||||
public function testAddWithIndex($element, $index, $expectedCount, $expectedIndex, BaseCollection $collection)
|
public function testAddWithIndex($element, $index, $expectedCount, $expectedIndex, CollectionInterface $collection)
|
||||||
{
|
{
|
||||||
$collection->add($element, $index);
|
$collection->add($element, $index);
|
||||||
|
|
||||||
@@ -340,14 +345,14 @@ class BaseCollectionTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $description Description of test
|
* @param string $description Description of test
|
||||||
* @param BaseCollection $collection Collection to search for element with given index
|
* @param CollectionInterface $collection Collection to search for element with given index
|
||||||
* @param mixed $index Index / key of the element
|
* @param mixed $index Index / key of the element
|
||||||
* @param mixed $expected Expected element with given index
|
* @param mixed $expected Expected element with given index
|
||||||
*
|
*
|
||||||
* @dataProvider provideElementGetByIndex
|
* @dataProvider provideElementGetByIndex
|
||||||
*/
|
*/
|
||||||
public function testGetByIndex($description, BaseCollection $collection, $index, $expected)
|
public function testGetByIndex($description, CollectionInterface $collection, $index, $expected)
|
||||||
{
|
{
|
||||||
static::assertEquals($expected, $collection->getByIndex($index), $description);
|
static::assertEquals($expected, $collection->getByIndex($index), $description);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user