mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-13 01:51:50 +01:00
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:
81
src/Collection/BaseCollection.php
Normal file
81
src/Collection/BaseCollection.php
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
27
src/Collection/DateTimeCollection.php
Normal file
27
src/Collection/DateTimeCollection.php
Normal 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;
|
||||
}
|
||||
}
|
||||
25
src/Collection/IntegerCollection.php
Normal file
25
src/Collection/IntegerCollection.php
Normal 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);
|
||||
}
|
||||
}
|
||||
25
src/Collection/StringCollection.php
Normal file
25
src/Collection/StringCollection.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,9 +42,9 @@ 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
|
||||
* this collection. Otherwise - not.
|
||||
* @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
|
||||
{
|
||||
|
||||
@@ -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,11 +207,11 @@ 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
|
||||
* 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
|
||||
* object does not have property. Otherwise - not.
|
||||
* @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
|
||||
* object does not have property. Otherwise - not.
|
||||
* @return array
|
||||
*/
|
||||
public static function getPropertyValues($objects, string $property, bool $force = false): array
|
||||
@@ -224,7 +224,7 @@ class Reflection
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($objects instanceof Collection) {
|
||||
if ($objects instanceof BaseCollection) {
|
||||
$objects = $objects->toArray();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user