diff --git a/README.md b/README.md index b8fe66f..1155ac4 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ composer require meritoo/common-library # Usage 1. [Base test case (with common methods and data providers)](docs/Base-test-case.md) -2. [Collection of elements](docs/Collection/Collection.md) +2. [Collection of elements](docs/Collection/BaseCollection.md) 3. [Exceptions](docs/Static-methods.md) 4. [Static methods](docs/Static-methods.md) 1. [Arrays](docs/Static-methods/Arrays.md) diff --git a/docs/Base-test-case.md b/docs/Base-test-case.md index 3f7ef40..05b7ce3 100644 --- a/docs/Base-test-case.md +++ b/docs/Base-test-case.md @@ -51,7 +51,7 @@ class MimeTypesTest extends BaseTestCase # More 1. [**Base test case (with common methods and data providers)**](Base-test-case.md) -2. [Collection of elements](Collection/Collection.md) +2. [Collection of elements](Collection/BaseCollection.md) 3. [Templates](Collection/Templates.md) 4. [Exceptions](Exceptions.md) 5. [Static methods](Static-methods.md) diff --git a/docs/Collection/Collection.md b/docs/Collection/BaseCollection.md similarity index 50% rename from docs/Collection/Collection.md rename to docs/Collection/BaseCollection.md index c2f8f61..63003b4 100644 --- a/docs/Collection/Collection.md +++ b/docs/Collection/BaseCollection.md @@ -2,15 +2,16 @@ Common and useful classes, methods, exceptions etc. -# Collection +# BaseCollection ### Namespace -`Meritoo\Common\Collection\Collection` +`Meritoo\Common\Collection\BaseCollection` ### Info -It's a set of some elements, e.g. objects. It's iterable and countable. Provides very useful methods. Some of them: +It's a set of some elements with the same type, e.g. objects. It's iterable and countable. Provides very useful +methods. Some of them: - `getFirst()` - returns the first element in the collection - `getLast()` - returns the last element in the collection - `isEmpty()` - returns information if collection is empty @@ -19,21 +20,49 @@ It's a set of some elements, e.g. objects. It's iterable and countable. Provides - `prepend($element)` - prepends given element (adds given element at the beginning of collection) - `remove($element)` - removes given element -Examples of usage below. +### Implementation -### An empty collection +You have to implement: ```php -use Meritoo\Common\Collection\Collection; - -$emptyCollection = new Collection(); -var_dump($emptyCollection->isEmpty()); // bool(true) +abstract protected function isValidType($element): bool; ``` -### Simple collection +This method verifies 1 element before it will be added to collection. Returns information if the element has valid, +expected type. + +Example (from `Meritoo\Common\Collection\Templates` class): ```php -use Meritoo\Common\Collection\Collection; +protected function isValidType($element): bool +{ + return $element instanceof Template; +} +``` + +### Methods to overwrite + +You can, if you wish, overwrite these methods: + +1. To prepare elements used to initialize the collection in your own way: + + ```php + protected function prepareElements(array $elements): array + ``` + +2. To validate type of elements in your own way: + + ```php + protected function getElementsWithValidType(array $elements): array + ``` + +### Examples of usage + +```php +use Meritoo\Common\Collection\StringCollection; + +$emptyCollection = new StringCollection(); +var_dump($emptyCollection->isEmpty()); // bool(true) $elements = [ 'lorem', @@ -42,14 +71,14 @@ $elements = [ 345 => 'sit', ]; -$simpleCollection = new Collection($elements); +$simpleCollection = new StringCollection($elements); var_dump($simpleCollection->has('dolor')); // bool(true) ``` # More 1. [Base test case (with common methods and data providers)](../Base-test-case.md) -2. [**Collection of elements**](Collection.md) +2. [**Collection of elements**](BaseCollection.md) 3. [Templates](Templates.md) 4. [Exceptions](../Exceptions.md) 5. [Static methods](../Static-methods.md) diff --git a/docs/Collection/Templates.md b/docs/Collection/Templates.md index c4fa36a..276b3d2 100644 --- a/docs/Collection/Templates.md +++ b/docs/Collection/Templates.md @@ -54,7 +54,7 @@ Throws an `Meritoo\Common\Exception\ValueObject\Template\TemplateNotFoundExcepti # More 1. [Base test case (with common methods and data providers)](../Base-test-case.md) -2. [Collection of elements](Collection.md) +2. [Collection of elements](BaseCollection.md) 3. [**Templates**](Templates.md) 4. [Exceptions](../Exceptions.md) 5. [Static methods](../Static-methods.md) diff --git a/docs/Exceptions.md b/docs/Exceptions.md index 49c38f2..1c70d23 100644 --- a/docs/Exceptions.md +++ b/docs/Exceptions.md @@ -54,7 +54,7 @@ class UnknownSimpleTypeException extends UnknownTypeException # More 1. [Base test case (with common methods and data providers)](Base-test-case.md) -2. [Collection of elements](Collection/Collection.md) +2. [Collection of elements](Collection/BaseCollection.md) 3. [Templates](Collection/Templates.md) 4. [**Exceptions**](Exceptions.md) 5. [Static methods](Static-methods.md) diff --git a/docs/Static-methods.md b/docs/Static-methods.md index 0b225e0..9cf5f65 100644 --- a/docs/Static-methods.md +++ b/docs/Static-methods.md @@ -16,7 +16,7 @@ var_dump($firstElement); // string(5) "lorem" # More 1. [Base test case (with common methods and data providers)](Base-test-case.md) -2. [Collection of elements](Collection/Collection.md) +2. [Collection of elements](Collection/BaseCollection.md) 3. [Templates](Collection/Templates.md) 4. [Exceptions](Exceptions.md) 5. [**Static methods**](Static-methods.md) diff --git a/docs/Static-methods/Arrays.md b/docs/Static-methods/Arrays.md index 7169a55..c50cd16 100644 --- a/docs/Static-methods/Arrays.md +++ b/docs/Static-methods/Arrays.md @@ -95,7 +95,7 @@ File: `src/Utilities/Arrays.php` # More 1. [Base test case (with common methods and data providers)](../Base-test-case.md) -2. [Collection of elements](../Collection/Collection.md) +2. [Collection of elements](../Collection/BaseCollection.md) 3. [Templates](../Collection/Templates.md) 4. [Exceptions](../Exceptions.md) 5. [Static methods](../Static-methods.md) diff --git a/docs/Static-methods/Regex.md b/docs/Static-methods/Regex.md index 1a4a48b..6c0d0c4 100644 --- a/docs/Static-methods/Regex.md +++ b/docs/Static-methods/Regex.md @@ -87,7 +87,7 @@ File: `src/Utilities/Regex.php` # More 1. [Base test case (with common methods and data providers)](../Base-test-case.md) -2. [Collection of elements](../Collection/Collection.md) +2. [Collection of elements](../Collection/BaseCollection.md) 3. [Templates](../Collection/Templates.md) 4. [Exceptions](../Exceptions.md) 5. [Static methods](../Static-methods.md) diff --git a/docs/Static-methods/Uri.md b/docs/Static-methods/Uri.md index 5808d31..0d64259 100644 --- a/docs/Static-methods/Uri.md +++ b/docs/Static-methods/Uri.md @@ -35,7 +35,7 @@ File: `src/Utilities/Uri.php` # More 1. [Base test case (with common methods and data providers)](../Base-test-case.md) -2. [Collection of elements](../Collection/Collection.md) +2. [Collection of elements](../Collection/BaseCollection.md) 3. [Templates](../Collection/Templates.md) 4. [Exceptions](../Exceptions.md) 5. [Static methods](../Static-methods.md) diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index 83184de..47777f2 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -338,7 +338,7 @@ $asString = (string)$version; // "1.0.2" # More 1. [Base test case (with common methods and data providers)](Base-test-case.md) -2. [Collection of elements](Collection/Collection.md) +2. [Collection of elements](Collection/BaseCollection.md) 3. [Templates](Collection/Templates.md) 4. [Exceptions](Exceptions.md) 5. [Static methods](Static-methods.md)