diff --git a/src/Collection/BaseCollection.php b/src/Collection/BaseCollection.php index 293df6d..6ae692c 100644 --- a/src/Collection/BaseCollection.php +++ b/src/Collection/BaseCollection.php @@ -46,13 +46,21 @@ abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggrega return $elements; } + /** + * Returns information if given element has valid type + * + * @param mixed $element Element of collection + * @return bool + */ + abstract protected function isValidType($element): bool; + /** * Returns elements of collection with valid types * * @param array $elements The elements of collection to verify * @return array */ - protected function getElementsWithValidType(array $elements): array + private function getElementsWithValidType(array $elements): array { if (empty($elements)) { return []; @@ -70,12 +78,4 @@ abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggrega return $result; } - - /** - * Returns information if given element has valid type - * - * @param mixed $element Element of collection - * @return bool - */ - abstract protected function isValidType($element): bool; } diff --git a/tests/Collection/Collection/ArrayCollection.php b/tests/Collection/BaseCollection/FirstNamesCollection.php similarity index 51% rename from tests/Collection/Collection/ArrayCollection.php rename to tests/Collection/BaseCollection/FirstNamesCollection.php index 2ad9627..1dc7c56 100644 --- a/tests/Collection/Collection/ArrayCollection.php +++ b/tests/Collection/BaseCollection/FirstNamesCollection.php @@ -8,12 +8,12 @@ declare(strict_types=1); -namespace Meritoo\Test\Common\Collection\Collection; +namespace Meritoo\Test\Common\Collection\BaseCollection; use Meritoo\Common\Collection\BaseCollection; /** - * Collection of arrays + * Collection of first names * * @author Meritoo * @copyright Meritoo @@ -21,10 +21,22 @@ use Meritoo\Common\Collection\BaseCollection; * @internal * @coversNothing */ -class ArrayCollection extends BaseCollection +class FirstNamesCollection extends BaseCollection { protected function isValidType($element): bool { - return is_array($element); + return $element instanceof User; + } + + protected function prepareElements(array $elements): array + { + $result = []; + + /** @var User $element */ + foreach ($elements as $element) { + $result[] = $element->getFirstName(); + } + + return $result; } } diff --git a/tests/Collection/BaseCollection/User.php b/tests/Collection/BaseCollection/User.php new file mode 100644 index 0000000..642ef56 --- /dev/null +++ b/tests/Collection/BaseCollection/User.php @@ -0,0 +1,45 @@ + + * @copyright Meritoo + * + * @internal + * @coversNothing + */ +final class User +{ + /** @var string */ + private $firstName; + + /** @var string */ + private $lastName; + + public function __construct(string $firstName, string $lastName) + { + $this->firstName = $firstName; + $this->lastName = $lastName; + } + + public function getFirstName(): string + { + return $this->firstName; + } + + public function getLastName(): string + { + return $this->lastName; + } +} diff --git a/tests/Collection/BaseCollectionTest.php b/tests/Collection/BaseCollectionTest.php index 0f7a482..1883e8b 100644 --- a/tests/Collection/BaseCollectionTest.php +++ b/tests/Collection/BaseCollectionTest.php @@ -15,7 +15,8 @@ use Meritoo\Common\Collection\DateTimeCollection; use Meritoo\Common\Collection\StringCollection; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; -use Meritoo\Test\Common\Collection\Collection\ArrayCollection; +use Meritoo\Test\Common\Collection\BaseCollection\FirstNamesCollection; +use Meritoo\Test\Common\Collection\BaseCollection\User; use ReflectionClass; /** @@ -360,7 +361,7 @@ class BaseCollectionTest extends BaseTestCase */ public function testGetElementsWithValidType(string $description, array $elements, array $expected): void { - $collection = new ArrayCollection($elements); + $collection = new FirstNamesCollection($elements); static::assertSame($expected, $collection->toArray(), $description); } @@ -511,21 +512,12 @@ class BaseCollectionTest extends BaseTestCase yield[ 'Collection with existing index (collection of arrays)', - new ArrayCollection([ - [ - 'lorem', - 'ipsum', - ], - [ - 'dolor', - 'sit', - ], + new FirstNamesCollection([ + new User('John', 'Scott'), + new User('Jane', 'Brown'), ]), 0, - [ - 'lorem', - 'ipsum', - ], + 'John', ]; yield[ @@ -538,6 +530,16 @@ class BaseCollectionTest extends BaseTestCase 'y', new \DateTime('2001-01-01'), ]; + + yield[ + 'Collection with first names', + new FirstNamesCollection([ + new User('John', 'Scott'), + new User('Jane', 'Brown'), + ]), + 1, + 'Jane', + ]; } public function provideElementsToValidateType(): ?Generator @@ -551,18 +553,12 @@ class BaseCollectionTest extends BaseTestCase yield[ 'Valid elements only', [ - [], - [ - '123', - 456, - ], + new User('John', 'Scott'), + new User('Jane', 'Brown'), ], [ - [], - [ - '123', - 456, - ], + 'John', + 'Jane', ], ]; @@ -572,20 +568,15 @@ class BaseCollectionTest extends BaseTestCase 1, 'test', '', + new User('John', 'Scott'), [], 234, 'test', - [ - '123', - 456, - ], + new User('Jane', 'Brown'), ], [ - 3 => [], - 6 => [ - '123', - 456, - ], + 'John', + 'Jane', ], ]; } diff --git a/tests/Collection/DateTimeCollectionTest.php b/tests/Collection/DateTimeCollectionTest.php index f8cdd1c..ea6fd48 100644 --- a/tests/Collection/DateTimeCollectionTest.php +++ b/tests/Collection/DateTimeCollectionTest.php @@ -1,5 +1,11 @@