-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add a note on how to extend a Collection.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
include __DIR__ . '/../../../vendor/autoload.php'; | ||
|
||
use loophp\collection\Collection; | ||
use loophp\collection\Contract\Collection as CollectionInterface; | ||
use loophp\collection\Operation\AbstractOperation; | ||
use loophp\collection\Operation\Append; | ||
|
||
interface FoobarCollectionInterface extends CollectionInterface | ||
{ | ||
public function foobar(): FoobarCollectionInterface; | ||
} | ||
|
||
final class Foobar extends AbstractOperation | ||
{ | ||
public function __invoke(): Closure | ||
{ | ||
return static function (Iterator $iterator): Generator { | ||
foreach ($iterator as $key => $value) { | ||
yield 'foo' => 'bar'; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
final class FoobarCollection implements FoobarCollectionInterface | ||
{ | ||
private CollectionInterface $collection; | ||
|
||
public function __construct(callable $callable, ...$parameters) | ||
{ | ||
$this->collection = new Collection($callable, ...$parameters); | ||
} | ||
|
||
public function append(...$items): FoobarCollectionInterface | ||
{ | ||
return new self(Append::of()(...$items), $this->collection); | ||
} | ||
|
||
public function foobar(): FoobarCollectionInterface | ||
{ | ||
return new self(Foobar::of(), $this->collection); | ||
} | ||
|
||
// This example is intentionally incomplete. | ||
// For the sake of brevity, I did not added all the remaining | ||
// methods to satisfy the FoobarCollectionInterface. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters