Implement methods of CollectionInterface directly in BaseCollection class (instead of traits)

This commit is contained in:
Meritoo
2021-04-04 22:59:59 +02:00
parent a4d24a788b
commit d5542b60e3
10 changed files with 238 additions and 447 deletions

View File

@@ -10,8 +10,9 @@ declare(strict_types=1);
namespace Meritoo\Common\Collection;
use ArrayIterator;
use Meritoo\Common\Contract\Collection\CollectionInterface;
use Meritoo\Common\Traits\CollectionTrait;
use Meritoo\Common\Utilities\Arrays;
/**
* Collection of elements with the same type
@@ -21,7 +22,12 @@ use Meritoo\Common\Traits\CollectionTrait;
*/
abstract class BaseCollection implements CollectionInterface
{
use CollectionTrait;
/**
* The elements of collection
*
* @var array
*/
private $elements;
/**
* Class constructor
@@ -34,6 +40,225 @@ abstract class BaseCollection implements CollectionInterface
$this->elements = $this->prepareElements($validated);
}
/**
* Returns representation of object as array
*
* @return array
*/
public function toArray(): array
{
return $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
*/
public function add($element, $index = null): void
{
if (!$this->isValidType($element)) {
return;
}
if (null === $index) {
$this->elements[] = $element;
return;
}
$this->elements[$index] = $element;
}
/**
* Adds given elements (at the end of collection)
*
* @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 this collection. Otherwise - not.
*/
public function addMultiple($elements, bool $useIndexes = false): void
{
if (empty($elements)) {
return;
}
foreach ($elements as $index => $element) {
if ($useIndexes) {
$this->add($element, $index);
continue;
}
$this->add($element);
}
}
/**
* Prepends given element (adds given element at the beginning of collection)
*
* @param mixed $element The element to prepend
*/
public function prepend($element): void
{
array_unshift($this->elements, $element);
}
/**
* Removes given element
*
* @param mixed $element The element to remove
*/
public function remove($element): void
{
if (0 === $this->count()) {
return;
}
foreach ($this->elements as $index => $existing) {
if ($element === $existing) {
unset($this->elements[$index]);
break;
}
}
}
/**
* Returns previous element for given element
*
* @param mixed $element The element to verify
* @return null|mixed
*/
public function getPrevious($element)
{
return Arrays::getPreviousElement($this->elements, $element);
}
/**
* Returns next element for given element
*
* @param mixed $element The element to verify
* @return null|mixed
*/
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 null|mixed
*/
public function getByIndex($index)
{
return $this->elements[$index] ?? null;
}
/**
* Returns information if collection is empty
*
* @return bool
*/
public function isEmpty(): bool
{
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): bool
{
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): bool
{
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): bool
{
$index = Arrays::getIndexOf($this->elements, $element);
return null !== $index && false !== $index;
}
public function count(): int
{
return count($this->elements);
}
public function offsetExists($offset): bool
{
return $this->exists($offset);
}
public function offsetGet($offset)
{
if ($this->exists($offset)) {
return $this->elements[$offset];
}
return null;
}
public function offsetSet($offset, $value): void
{
$this->elements[$offset] = $value;
}
public function offsetUnset($offset): void
{
if ($this->exists($offset)) {
unset($this->elements[$offset]);
}
}
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->elements);
}
/**
* Prepares elements to initialize the collection.
* Feel free to override and prepare elements in your way.
@@ -78,4 +303,15 @@ abstract class BaseCollection implements CollectionInterface
return $result;
}
/**
* Returns information if element with given index/key exists
*
* @param int|string $index The index/key of element
* @return bool
*/
private function exists($index): bool
{
return isset($this->elements[$index]) || array_key_exists($index, $this->elements);
}
}

View File

@@ -1,65 +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\Traits\Collection;
use Meritoo\Common\Contract\Collection\CollectionInterface;
/**
* Trait for the Collection with add*() methods
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait AddTrait
{
/**
* Adds given element (at the end of collection)
*
* @param mixed $element The element to add
* @param mixed $index (optional) Index / key of the element
*/
public function add($element, $index = null): void
{
if (!$this->isValidType($element)) {
return;
}
if (null === $index) {
$this->elements[] = $element;
return;
}
$this->elements[$index] = $element;
}
/**
* Adds given elements (at the end of collection)
*
* @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 this collection. Otherwise - not.
*/
public function addMultiple($elements, bool $useIndexes = false): void
{
if (empty($elements)) {
return;
}
foreach ($elements as $index => $element) {
if ($useIndexes) {
$this->add($element, $index);
continue;
}
$this->add($element);
}
}
}

View File

@@ -1,67 +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\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): bool
{
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): void
{
$this->elements[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void
{
if ($this->exists($offset)) {
unset($this->elements[$offset]);
}
}
/**
* Returns information if element with given index/key exists
*
* @param int|string $index The index/key of element
* @return bool
*/
private function exists($index): bool
{
return isset($this->elements[$index]) || array_key_exists($index, $this->elements);
}
}

View File

@@ -1,26 +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\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(): int
{
return count($this->elements);
}
}

View File

@@ -1,73 +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\Traits\Collection;
use Meritoo\Common\Utilities\Arrays;
/**
* Trait for the Collection with getters
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait GetTrait
{
/**
* Returns previous element for given element
*
* @param mixed $element The element to verify
* @return null|mixed
*/
public function getPrevious($element)
{
return Arrays::getPreviousElement($this->elements, $element);
}
/**
* Returns next element for given element
*
* @param mixed $element The element to verify
* @return null|mixed
*/
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 null|mixed
*/
public function getByIndex($index)
{
return $this->elements[$index] ?? null;
}
}

View File

@@ -1,28 +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\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(): ArrayIterator
{
return new ArrayIterator($this->elements);
}
}

View File

@@ -1,35 +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\Traits\Collection;
/**
* 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;
/**
* Returns representation of object as array
*
* @return array
*/
public function toArray(): array
{
return $this->elements;
}
}

View File

@@ -1,48 +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\Traits\Collection;
/**
* Trait for the Collection with methods that modify collection
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait ModifyTrait
{
/**
* Prepends given element (adds given element at the beginning of collection)
*
* @param mixed $element The element to prepend
*/
public function prepend($element): void
{
array_unshift($this->elements, $element);
}
/**
* Removes given element
*
* @param mixed $element The element to remove
*/
public function remove($element): void
{
if (0 === $this->count()) {
return;
}
foreach ($this->elements as $index => $existing) {
if ($element === $existing) {
unset($this->elements[$index]);
break;
}
}
}
}

View File

@@ -1,65 +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\Traits\Collection;
use Meritoo\Common\Utilities\Arrays;
/**
* Trait for the Collection with methods that verify collection
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait VerifyTrait
{
/**
* Returns information if collection is empty
*
* @return bool
*/
public function isEmpty(): bool
{
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): bool
{
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): bool
{
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): bool
{
$index = Arrays::getIndexOf($this->elements, $element);
return null !== $index && false !== $index;
}
}

View File

@@ -1,38 +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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Traits;
use Meritoo\Common\Traits\Collection\AddTrait;
use Meritoo\Common\Traits\Collection\ArrayAccessTrait;
use Meritoo\Common\Traits\Collection\CountableTrait;
use Meritoo\Common\Traits\Collection\GetTrait;
use Meritoo\Common\Traits\Collection\IteratorAggregateTrait;
use Meritoo\Common\Traits\Collection\MainTrait;
use Meritoo\Common\Traits\Collection\ModifyTrait;
use Meritoo\Common\Traits\Collection\VerifyTrait;
/**
* Trait for the Collection
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
trait CollectionTrait
{
use MainTrait;
use AddTrait;
use ModifyTrait;
use GetTrait;
use VerifyTrait;
use CountableTrait;
use ArrayAccessTrait;
use IteratorAggregateTrait;
}