Skip to content

Commit

Permalink
Added IterablesCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
rotexdegba committed May 17, 2022
1 parent 65f6b1f commit 0ddb363
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ in this package:
* **[Arrays Collections](docs/ArraysCollection.md):** a collection that only stores items that are arrays (i.e. items for which is_array is true)
* **[Callables Collections](docs/CallablesCollections.md):** a collection that only stores items that are callables (i.e. items for which is_callable is true)
* **[Objects Collections](docs/ObjectsCollections.md):** a collection that only stores items that are objects (i.e. items for which is_object is true)
* **[Iterables Collections](docs/IterablesCollection.md):** a collection that only accepts
items that are objects for which [\is_iterable](https://www.php.net/manual/en/function.is-iterable) returns true. This type of collection does not accept [arrays](https://www.php.net/manual/en/language.types.array.php) which are also iterables but not objects. Use [Arrays Collections](docs/ArraysCollection.md) to store arrays or you could create a new collection class that accepts all iterables (both arrays & every other type for which **is_iterable** returns **true**)
* **[Non-Array Iterables Collections](docs/NonArrayIterablesCollection.md):** a collection that only accepts items that are objects for which [\is_iterable](https://www.php.net/manual/en/function.is-iterable) returns true. This type of collection does not accept [arrays](https://www.php.net/manual/en/language.types.array.php) which are also iterables but not objects. Use [Arrays Collections](docs/ArraysCollection.md) to store arrays or you could create a new collection class that accepts all iterables (both arrays & every other type for which **is_iterable** returns **true**)
* **[Specific Objects Collections](docs/SpecificObjectsCollection.md):** a collection that only stores items that are instances of a specified class or any of its sub-classes
* **[Resources Collections](docs/ResourcesCollections.md):** a collection that only stores items that are resources (i.e. items for which is_resource is true)
* **[Scalars Collections](docs/ScalarsCollections.md):** a collection that only stores items that are scalars (i.e. items for which is_scalar is true)
Expand Down Expand Up @@ -218,6 +217,7 @@ or its sub-classes in a collection and don't want to have to create a custom col
* [Arrays Collections](docs/ArraysCollection.md): a collection that can only contain [arrays](http://php.net/manual/en/language.types.array.php)
* [Callables Collections](docs/CallablesCollections.md): a collection that can only contain [callables](http://php.net/manual/en/language.types.callable.php)
* [Objects Collections](docs/ObjectsCollections.md): a collection that can only contain [objects](http://php.net/manual/en/language.types.object.php) (any kind of object)
* [Non-Array Iterables Collections](docs/NonArrayIterablesCollection.md): a collection that can only contain items that are objects (but not [arrays](https://www.php.net/manual/en/language.types.array.php)) for which [\is_iterable](https://www.php.net/manual/en/function.is-iterable) returns true
* [Specific Objects Collections](docs/SpecificObjectsCollection.md): a collection that can either contain only instances of a specified class or any of its sub-classes or any type of [object](http://php.net/manual/en/language.types.object.php) (just like [Objects Collections](docs/ObjectsCollections.md)) if no class is specified
* [Resources Collections](docs/ResourcesCollections.md): a collection that can only contain [resources](http://php.net/manual/en/language.types.resource.php)
* [Scalars Collections](docs/ScalarsCollections.md): a collection that can only scalar values. I.e. any of [booleans](http://php.net/manual/en/language.types.boolean.php), [floats](http://php.net/manual/en/language.types.float.php), [integers](http://php.net/manual/en/language.types.integer.php) or [strings](http://php.net/manual/en/language.types.string.php). It accepts any mix of scalars, e.g. ints, booleans, floats and strings can all be present in an instance of this type of collection.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Iterables Collection
# Non-Array Iterables Collection

`\VersatileCollections\IterablesCollection` is a Collection class that only accepts
`\VersatileCollections\NonArrayIterablesCollection` is a Collection class that only accepts
items that are objects for which [\is_iterable](https://www.php.net/manual/en/function.is-iterable) returns true.

>NOTE: this collection will not accept items that are arrays (because an array is not an object, even though it is an iterable).
Expand All @@ -9,7 +9,7 @@ Example Usage:

```php

$collection = new \VersatileCollections\IterablesCollection(
$collection = new \VersatileCollections\NonArrayIterablesCollection(
new \ArrayObject(),
new \SplDoublyLinkedList(),
new \SplStack(),
Expand All @@ -23,7 +23,7 @@ Example Usage:

// OR

$collection = new \VersatileCollections\IterablesCollection(
$collection = new \VersatileCollections\NonArrayIterablesCollection(
new \ArrayObject(),
new \SplDoublyLinkedList(),
new \SplStack(),
Expand All @@ -37,7 +37,7 @@ Example Usage:

// OR

$collection = new \VersatileCollections\IterablesCollection();
$collection = new \VersatileCollections\NonArrayIterablesCollection();
$collection[] = new ArrayObject();
$collection[] = new SplDoublyLinkedList();
$collection[] = new SplStack();
Expand All @@ -49,6 +49,6 @@ Example Usage:
$collection[] = new SplObjectStorage();
```

Use this type of collection if you want to only store objects that are iterable.
Use this type of collection if you want to only store objects (except php [arrays](https://www.php.net/manual/en/language.types.array.php)) that are iterable.

You can even extend it and add other features in your extended class.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author Rotimi Ade
*/
class IterablesCollection extends ObjectsCollection
class NonArrayIterablesCollection extends ObjectsCollection
{
public function __construct(iterable ...$iterableObjects)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
declare(strict_types=1);
use \VersatileCollections\IterablesCollection;
use \VersatileCollections\NonArrayIterablesCollection;

/**
* Description of IterablesCollection
* Description of NonArrayIterablesCollectionTest
*
* @author rotimi
*/
class IterablesCollectionTest extends \PHPUnit\Framework\TestCase {
class NonArrayIterablesCollectionTest extends \PHPUnit\Framework\TestCase {

protected function setUp(): void {

Expand All @@ -16,7 +16,7 @@ protected function setUp(): void {

public function testThatOnlyIterableObjectsCanBeInjectedIntoCollection() {

$collection = new IterablesCollection(
$collection = new NonArrayIterablesCollection(
new \ArrayObject(),
new \SplDoublyLinkedList(),
new \SplStack(),
Expand All @@ -30,7 +30,7 @@ public function testThatOnlyIterableObjectsCanBeInjectedIntoCollection() {
);
$this->assertEquals($collection->count(), 10);

$collection2 = IterablesCollection::makeNew([
$collection2 = NonArrayIterablesCollection::makeNew([
new \ArrayObject(),
new \SplDoublyLinkedList(),
new \SplStack(),
Expand All @@ -44,7 +44,7 @@ public function testThatOnlyIterableObjectsCanBeInjectedIntoCollection() {
]);
$this->assertEquals($collection2->count(), 10);

$collection3 = new \VersatileCollections\IterablesCollection();
$collection3 = new \VersatileCollections\NonArrayIterablesCollection();
$this->assertEquals($collection3->count(), 0);
$collection3[] = new \SplPriorityQueue();
$collection3['r'] = new \SplObjectStorage();
Expand Down

0 comments on commit 0ddb363

Please sign in to comment.