mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Update documentation of BaseCollection class
This commit is contained in:
@@ -22,7 +22,7 @@ composer require meritoo/common-library
|
|||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](docs/Base-test-case.md)
|
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)
|
3. [Exceptions](docs/Static-methods.md)
|
||||||
4. [Static methods](docs/Static-methods.md)
|
4. [Static methods](docs/Static-methods.md)
|
||||||
1. [Arrays](docs/Static-methods/Arrays.md)
|
1. [Arrays](docs/Static-methods/Arrays.md)
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class MimeTypesTest extends BaseTestCase
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [**Base test case (with common methods and data providers)**](Base-test-case.md)
|
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)
|
3. [Templates](Collection/Templates.md)
|
||||||
4. [Exceptions](Exceptions.md)
|
4. [Exceptions](Exceptions.md)
|
||||||
5. [Static methods](Static-methods.md)
|
5. [Static methods](Static-methods.md)
|
||||||
|
|||||||
@@ -2,15 +2,16 @@
|
|||||||
|
|
||||||
Common and useful classes, methods, exceptions etc.
|
Common and useful classes, methods, exceptions etc.
|
||||||
|
|
||||||
# Collection
|
# BaseCollection
|
||||||
|
|
||||||
### Namespace
|
### Namespace
|
||||||
|
|
||||||
`Meritoo\Common\Collection\Collection`
|
`Meritoo\Common\Collection\BaseCollection`
|
||||||
|
|
||||||
### Info
|
### 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
|
- `getFirst()` - returns the first element in the collection
|
||||||
- `getLast()` - returns the last element in the collection
|
- `getLast()` - returns the last element in the collection
|
||||||
- `isEmpty()` - returns information if collection is empty
|
- `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)
|
- `prepend($element)` - prepends given element (adds given element at the beginning of collection)
|
||||||
- `remove($element)` - removes given element
|
- `remove($element)` - removes given element
|
||||||
|
|
||||||
Examples of usage below.
|
### Implementation
|
||||||
|
|
||||||
### An empty collection
|
You have to implement:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use Meritoo\Common\Collection\Collection;
|
abstract protected function isValidType($element): bool;
|
||||||
|
|
||||||
$emptyCollection = new Collection();
|
|
||||||
var_dump($emptyCollection->isEmpty()); // bool(true)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 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
|
```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 = [
|
$elements = [
|
||||||
'lorem',
|
'lorem',
|
||||||
@@ -42,14 +71,14 @@ $elements = [
|
|||||||
345 => 'sit',
|
345 => 'sit',
|
||||||
];
|
];
|
||||||
|
|
||||||
$simpleCollection = new Collection($elements);
|
$simpleCollection = new StringCollection($elements);
|
||||||
var_dump($simpleCollection->has('dolor')); // bool(true)
|
var_dump($simpleCollection->has('dolor')); // bool(true)
|
||||||
```
|
```
|
||||||
|
|
||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
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)
|
3. [Templates](Templates.md)
|
||||||
4. [Exceptions](../Exceptions.md)
|
4. [Exceptions](../Exceptions.md)
|
||||||
5. [Static methods](../Static-methods.md)
|
5. [Static methods](../Static-methods.md)
|
||||||
@@ -54,7 +54,7 @@ Throws an `Meritoo\Common\Exception\ValueObject\Template\TemplateNotFoundExcepti
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
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)
|
3. [**Templates**](Templates.md)
|
||||||
4. [Exceptions](../Exceptions.md)
|
4. [Exceptions](../Exceptions.md)
|
||||||
5. [Static methods](../Static-methods.md)
|
5. [Static methods](../Static-methods.md)
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ class UnknownSimpleTypeException extends UnknownTypeException
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](Base-test-case.md)
|
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)
|
3. [Templates](Collection/Templates.md)
|
||||||
4. [**Exceptions**](Exceptions.md)
|
4. [**Exceptions**](Exceptions.md)
|
||||||
5. [Static methods](Static-methods.md)
|
5. [Static methods](Static-methods.md)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ var_dump($firstElement); // string(5) "lorem"
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](Base-test-case.md)
|
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)
|
3. [Templates](Collection/Templates.md)
|
||||||
4. [Exceptions](Exceptions.md)
|
4. [Exceptions](Exceptions.md)
|
||||||
5. [**Static methods**](Static-methods.md)
|
5. [**Static methods**](Static-methods.md)
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ File: `src/Utilities/Arrays.php`
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
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)
|
3. [Templates](../Collection/Templates.md)
|
||||||
4. [Exceptions](../Exceptions.md)
|
4. [Exceptions](../Exceptions.md)
|
||||||
5. [Static methods](../Static-methods.md)
|
5. [Static methods](../Static-methods.md)
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ File: `src/Utilities/Regex.php`
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
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)
|
3. [Templates](../Collection/Templates.md)
|
||||||
4. [Exceptions](../Exceptions.md)
|
4. [Exceptions](../Exceptions.md)
|
||||||
5. [Static methods](../Static-methods.md)
|
5. [Static methods](../Static-methods.md)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ File: `src/Utilities/Uri.php`
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
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)
|
3. [Templates](../Collection/Templates.md)
|
||||||
4. [Exceptions](../Exceptions.md)
|
4. [Exceptions](../Exceptions.md)
|
||||||
5. [Static methods](../Static-methods.md)
|
5. [Static methods](../Static-methods.md)
|
||||||
|
|||||||
@@ -338,7 +338,7 @@ $asString = (string)$version; // "1.0.2"
|
|||||||
# More
|
# More
|
||||||
|
|
||||||
1. [Base test case (with common methods and data providers)](Base-test-case.md)
|
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)
|
3. [Templates](Collection/Templates.md)
|
||||||
4. [Exceptions](Exceptions.md)
|
4. [Exceptions](Exceptions.md)
|
||||||
5. [Static methods](Static-methods.md)
|
5. [Static methods](Static-methods.md)
|
||||||
|
|||||||
Reference in New Issue
Block a user