|
30 | 30 | - [Access functions](#access-functions) |
31 | 31 | - [with()](#with) |
32 | 32 | - [invoke_if()](#invoke_if) |
33 | | - - [invoke(), invoke_last(), invoke_first()](#invoke-invoke_last-invoke_first) |
| 33 | + - [invoke()](#invoke) |
| 34 | + - [invoke_first() & invoke_last()](#invoke_first--invoke_last) |
34 | 35 | - [invoker()](#invoker) |
35 | 36 | - [pluck()](#pluck) |
36 | 37 | - [pick()](#pick) |
@@ -564,22 +565,62 @@ $userId = invoke_if($user, 'getId', [], 0); |
564 | 565 | ``` |
565 | 566 |
|
566 | 567 |
|
567 | | -## invoke(), invoke_last(), invoke_first() |
| 568 | + |
| 569 | +## invoke() |
| 570 | + |
| 571 | +Invokes method `$methodName` on each object in the `$collection` and returns the results of the call. |
568 | 572 |
|
569 | 573 | ``array Functional\invoke(array|Traversable $collection, string $methodName[, array $methodArguments])`` |
570 | | -Invokes method `$methodName` on each object in the `$collection` and returns the results of the call |
| 574 | + |
| 575 | +```php |
| 576 | +<?php |
| 577 | +use function Functional\invoke; |
| 578 | + |
| 579 | +// calls addAttendee($user) on each object in $meetings array |
| 580 | +invoke($meetings, 'addAttendee', $user); |
| 581 | +``` |
| 582 | + |
| 583 | +## invoke_first() & invoke_last() |
| 584 | + |
| 585 | +Invokes method `$methodName` on the first or last object in the `$collection` containing a callable method named `$methodName` and returns the results of the call. |
571 | 586 |
|
572 | 587 | ``mixed Functional\invoke_first(array|Traversable $collection, string $methodName[, array $methodArguments])`` |
573 | | -Invokes method `$methodName` on the first object in the `$collection` containing a callable method named `$methodName` and returns the results of the call |
574 | 588 |
|
575 | 589 | ``mixed Functional\invoke_last(array|Traversable $collection, string $methodName[, array $methodArguments])`` |
576 | | -Invokes method `$methodName` on the last object in the `$collection` containing a callable method named `$methodName` and returns the results of the call |
577 | 590 |
|
| 591 | +```php |
| 592 | +<?php |
| 593 | +use function Functional\invoke_first; |
| 594 | +use function Functional\invoke_last; |
| 595 | + |
| 596 | +$meetings = [ |
| 597 | + new MandatoryEvent(), |
| 598 | + new MandatoryEvent(), |
| 599 | + new OptionalEvent(), |
| 600 | + new MandatoryEvent(), |
| 601 | + new OptionalEvent(), |
| 602 | + new MandatoryEvent(), |
| 603 | +]; |
| 604 | +// assuming only OptionalEvents can be delayed/changed... |
| 605 | + |
| 606 | +invoke_first($meetings, 'delayEvent', [30]); // calls delayEvent(30) on $meetings[2] |
| 607 | + |
| 608 | +invoke_last($meetings, 'changeRoom', ['Room 3']); // calls changeRoom('Room 3') on $meetings[4] |
| 609 | +``` |
578 | 610 |
|
579 | 611 | ## invoker() |
| 612 | +Returns a function that invokes method `$method` with arguments `$methodArguments` on the object. |
580 | 613 |
|
581 | 614 | ``callable Functional\invoker(string $method[, array $methodArguments])`` |
582 | | -Returns a function that invokes method `$method` with arguments `$methodArguments` on the object |
| 615 | + |
| 616 | +```php |
| 617 | +<?php |
| 618 | +use function Functional\invoker; |
| 619 | + |
| 620 | +$setLocationToMunich = invoker('updateLocation', ['Munich', 'Germany']); |
| 621 | + |
| 622 | +$setLocationToMunich($user); // calls $user->updateLocation('Munich', 'Germany') |
| 623 | +``` |
583 | 624 |
|
584 | 625 | ## pluck() |
585 | 626 | Fetch a single property from a collection of objects or arrays. |
@@ -1134,4 +1175,4 @@ repeat(function () { |
1134 | 1175 |
|
1135 | 1176 | A no-operation function, i.e. a function that does nothing. |
1136 | 1177 |
|
1137 | | -``void Functional\noop()`` |
| 1178 | +``void Functional\noop()`` |
0 commit comments