Skip to content

Commit

Permalink
docs: update extending collection section with code example
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 13, 2022
1 parent c93f350 commit 0c1d439
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/pages/code/extending-collection-with-collectiondecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\CollectionDecorator;

final class FoobarCollection extends CollectionDecorator
{
public function toUpperCase(): FoobarCollection
{
return $this
->map(
static fn (string $letter): string => strtoupper($letter)
);
}
}

$collection = FoobarCollection::fromIterable(range('a', 'e'))
->toUpperCase();
8 changes: 8 additions & 0 deletions docs/pages/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ If you want to **extend** the Collection, you have multiple options.

1. You just create a ``callable`` and use :ref:`pages/api:pipe` method.
2. You use the Composition design pattern and create your own library class.
3. Use the `CollectionDecorator` abstract class available since version 7.0.0

Every classes of this library are ``final`` and then it is impossible to use
inheritance and use the original Collection class as parent of another one.
Expand All @@ -115,6 +116,13 @@ new operation ``foobar``.
.. literalinclude:: code/extending-collection.php
:language: php

Since version 7.0.0, we ship a `CollectionDecorator` abstract class which let
you create your own Collection class with all your customizations.

.. literalinclude:: code/extending-collection-with-collectiondecorator.php
:language: php


Manipulate strings
~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit 0c1d439

Please sign in to comment.