Interfaces of different types of collections. May be used to build specific collections.

This commit is contained in:
Meritoo
2022-09-10 18:06:25 +02:00
parent d4cb732096
commit 08a2d0878f
10 changed files with 344 additions and 87 deletions

View File

@@ -27,12 +27,12 @@ abstract class BaseCollection implements CollectionInterface
* *
* @var array * @var array
*/ */
private $elements; private array $elements;
/** /**
* Class constructor * Class constructor
* *
* @param array $elements (optional) The elements of collection * @param array $elements (optional) Elements of collection
*/ */
public function __construct(array $elements = []) public function __construct(array $elements = [])
{ {
@@ -41,10 +41,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Adds given element (at the end of collection) * {@inheritdoc}
*
* @param mixed $element The element to add
* @param mixed $index (optional) Index / key of the element
*/ */
public function add($element, $index = null): void public function add($element, $index = null): void
{ {
@@ -62,11 +59,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Adds given elements (at the end of collection) * {@inheritdoc}
*
* @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 public function addMultiple($elements, bool $useIndexes = false): void
{ {
@@ -87,21 +80,32 @@ abstract class BaseCollection implements CollectionInterface
} }
} }
/**
* {@inheritdoc}
*/
public function append($element): void
{
$this->elements[] = $element;
}
/**
* {@inheritdoc}
*/
public function clear(): void public function clear(): void
{ {
$this->elements = []; $this->elements = [];
} }
/**
* {@inheritdoc}
*/
public function count(): int public function count(): int
{ {
return count($this->elements); return count($this->elements);
} }
/** /**
* Returns element with given index * {@inheritdoc}
*
* @param mixed $index Index / key of the element
* @return null|mixed
*/ */
public function getByIndex($index) public function getByIndex($index)
{ {
@@ -109,24 +113,23 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns the first element in the collection * {@inheritdoc}
*
* @return mixed
*/ */
public function getFirst() public function getFirst()
{ {
return Arrays::getFirstElement($this->elements); return Arrays::getFirstElement($this->elements);
} }
/**
* {@inheritdoc}
*/
public function getIterator(): ArrayIterator public function getIterator(): ArrayIterator
{ {
return new ArrayIterator($this->elements); return new ArrayIterator($this->elements);
} }
/** /**
* Returns the last element in the collection * {@inheritdoc}
*
* @return mixed
*/ */
public function getLast() public function getLast()
{ {
@@ -134,10 +137,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns next element for given element * {@inheritdoc}
*
* @param mixed $element The element to verify
* @return null|mixed
*/ */
public function getNext($element) public function getNext($element)
{ {
@@ -145,10 +145,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns previous element for given element * {@inheritdoc}
*
* @param mixed $element The element to verify
* @return null|mixed
*/ */
public function getPrevious($element) public function getPrevious($element)
{ {
@@ -156,10 +153,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns information if the collection has given element, iow. if given element exists in the collection * {@inheritdoc}
*
* @param mixed $element The element to verify
* @return bool
*/ */
public function has($element): bool public function has($element): bool
{ {
@@ -169,9 +163,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns information if collection is empty * {@inheritdoc}
*
* @return bool
*/ */
public function isEmpty(): bool public function isEmpty(): bool
{ {
@@ -179,10 +171,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns information if given element is first in the collection * {@inheritdoc}
*
* @param mixed $element The element to verify
* @return bool
*/ */
public function isFirst($element): bool public function isFirst($element): bool
{ {
@@ -190,17 +179,17 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns information if given element is last in the collection * {@inheritdoc}
*
* @param mixed $element The element to verify
* @return bool
*/ */
public function isLast($element): bool public function isLast($element): bool
{ {
return end($this->elements) === $element; return end($this->elements) === $element;
} }
public function limit(int $max, int $offset = 0): CollectionInterface /**
* {@inheritdoc}
*/
public function limit(int $max, int $offset = 0): self
{ {
$result = clone $this; $result = clone $this;
@@ -230,11 +219,17 @@ abstract class BaseCollection implements CollectionInterface
return $result; return $result;
} }
/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool public function offsetExists($offset): bool
{ {
return $this->exists($offset); return $this->exists($offset);
} }
/**
* {@inheritdoc}
*/
public function offsetGet($offset) public function offsetGet($offset)
{ {
if ($this->exists($offset)) { if ($this->exists($offset)) {
@@ -244,11 +239,17 @@ abstract class BaseCollection implements CollectionInterface
return null; return null;
} }
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value): void public function offsetSet($offset, $value): void
{ {
$this->elements[$offset] = $value; $this->elements[$offset] = $value;
} }
/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void public function offsetUnset($offset): void
{ {
if ($this->exists($offset)) { if ($this->exists($offset)) {
@@ -257,9 +258,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Prepends given element (adds given element at the beginning of collection) * {@inheritdoc}
*
* @param mixed $element The element to prepend
*/ */
public function prepend($element): void public function prepend($element): void
{ {
@@ -267,9 +266,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Removes given element * {@inheritdoc}
*
* @param mixed $element The element to remove
*/ */
public function remove($element): void public function remove($element): void
{ {
@@ -287,9 +284,7 @@ abstract class BaseCollection implements CollectionInterface
} }
/** /**
* Returns representation of object as array * {@inheritdoc}
*
* @return array
*/ */
public function toArray(): array public function toArray(): array
{ {

View File

@@ -0,0 +1,55 @@
<?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;
/**
* Contract for collection that may add elements
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface AddableCollectionInterface
{
/**
* 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 void
*/
public function add($element, $index = null): void;
/**
* 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.
* @return void
*/
public function addMultiple(array $elements, bool $useIndexes = false): void;
/**
* Appends given element (adds given element at the end of collection)
*
* @param mixed $element The element to add at the end
* @return void
*/
public function append($element): void;
/**
* Prepends given element (adds given element at the beginning of collection)
*
* @param mixed $element The element to add at the beginning
* @return void
*/
public function prepend($element): void;
}

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\Contract\Collection;
/**
* Contract for collection that may be cleared (all its elements may be removed)
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface ClearableCollectionInterface
{
/**
* Removes all elements of the collection
*
* @return void
*/
public function clear(): void;
}

View File

@@ -11,46 +11,22 @@ declare(strict_types=1);
namespace Meritoo\Common\Contract\Collection; namespace Meritoo\Common\Contract\Collection;
use ArrayAccess; use ArrayAccess;
use Countable;
use IteratorAggregate; use IteratorAggregate;
/** /**
* Interface/Contract of collection of elements with the same type * Contract for collection of elements with the same type
* *
* @author Meritoo <github@meritoo.pl> * @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl> * @copyright Meritoo <http://www.meritoo.pl>
*/ */
interface CollectionInterface extends Countable, ArrayAccess, IteratorAggregate interface CollectionInterface extends ArrayAccess, IteratorAggregate, AddableCollectionInterface,
RemovableCollectionInterface, CountableCollectionInterface, ClearableCollectionInterface,
GettableCollectionInterface, VerifiableCollectionInterface, ReducibleCollectionInterface
{ {
public function add($element, $index = null): void; /**
* Returns representation of object as array
public function addMultiple($elements, bool $useIndexes = false): void; *
* @return array
public function clear(): void; */
public function getByIndex($index);
public function getFirst();
public function getLast();
public function getNext($element);
public function getPrevious($element);
public function has($element): bool;
public function isEmpty(): bool;
public function isFirst($element): bool;
public function isLast($element): bool;
public function limit(int $max, int $offset = 0): self;
public function prepend($element): void;
public function remove($element): void;
public function toArray(): array; public function toArray(): array;
} }

View File

@@ -0,0 +1,23 @@
<?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 Countable;
/**
* Contract for collection that may count its elements
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface CountableCollectionInterface extends Countable
{
}

View File

@@ -0,0 +1,58 @@
<?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;
/**
* Contract for collection that returns first, last element etc.
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface GettableCollectionInterface
{
/**
* Returns element with given index
*
* @param mixed $index Index / key of element to return
* @return mixed
*/
public function getByIndex($index);
/**
* Returns first element
*
* @return mixed
*/
public function getFirst();
/**
* Returns last element
*
* @return mixed
*/
public function getLast();
/**
* Returns element next after given element
*
* @param mixed $element The element whose next element should be returned
* @return mixed
*/
public function getNext($element);
/**
* Returns element preceding given element
*
* @param mixed $element The element whose previous element should be returned
* @return mixed
*/
public function getPrevious($element);
}

View File

@@ -0,0 +1,29 @@
<?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;
/**
* Contract for collection that may reduce its elements
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface ReducibleCollectionInterface
{
/**
* Returns new instance of this collection with limited elements
*
* @param int $max Maximum elements to return
* @param int $offset (optional) Position of element from which limitation should start
* @return $this
*/
public function limit(int $max, int $offset = 0): self;
}

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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Contract\Collection;
/**
* Contract for collection that may remove elements
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface RemovableCollectionInterface
{
/**
* Removes given element
*
* @param mixed $element The element to remove
* @return void
*/
public function remove($element): void;
}

View File

@@ -0,0 +1,51 @@
<?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;
/**
* Contract for collection that may verify its elements
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
interface VerifiableCollectionInterface
{
/**
* Returns information if given element exists in collection
*
* @param mixed $element The element to verify
* @return bool
*/
public function has($element): bool;
/**
* Returns information if collection is empty (has not any element)
*
* @return bool
*/
public function isEmpty(): bool;
/**
* Returns information if given element is the first element in collection
*
* @param mixed $element The element to verify
* @return bool
*/
public function isFirst($element): bool;
/**
* Returns information if given element is the last element in collection
*
* @param mixed $element The element to verify
* @return bool
*/
public function isLast($element): bool;
}

View File

@@ -721,6 +721,21 @@ class BaseCollectionTest extends BaseTestCase
static::assertSame(5, $this->simpleCollection->count()); static::assertSame(5, $this->simpleCollection->count());
} }
public function testAppend(): void
{
$this->emptyCollection->append('lorem-ipsum');
static::assertFalse($this->emptyCollection->isEmpty());
static::assertSame(1, $this->emptyCollection->count());
static::assertSame('lorem-ipsum', $this->emptyCollection[0]);
$this->simpleCollection->append('lorem-ipsum');
static::assertFalse($this->simpleCollection->isEmpty());
static::assertSame(8, $this->simpleCollection->count());
static::assertSame('lorem-ipsum', $this->simpleCollection[347]);
}
public function testPrepend() public function testPrepend()
{ {
$this->emptyCollection->prepend('lorem-ipsum'); $this->emptyCollection->prepend('lorem-ipsum');