Skip to content

Commit a736d25

Browse files
mducharmlstrojny
authored andcommitted
Added examples for invoke functions. (#211)
Added examples for invoke(), invoke_first(), invoke_last(), & invoker() functions. (Since this is my first real PR on GitHub, please let me know if I missed anything or if any changes are needed!)
1 parent df0e516 commit a736d25

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

docs/functional-php.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
- [Access functions](#access-functions)
3131
- [with()](#with)
3232
- [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)
3435
- [invoker()](#invoker)
3536
- [pluck()](#pluck)
3637
- [pick()](#pick)
@@ -564,22 +565,62 @@ $userId = invoke_if($user, 'getId', [], 0);
564565
```
565566

566567

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.
568572

569573
``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.
571586

572587
``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
574588

575589
``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
577590

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+
```
578610

579611
## invoker()
612+
Returns a function that invokes method `$method` with arguments `$methodArguments` on the object.
580613

581614
``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+
```
583624

584625
## pluck()
585626
Fetch a single property from a collection of objects or arrays.
@@ -1134,4 +1175,4 @@ repeat(function () {
11341175

11351176
A no-operation function, i.e. a function that does nothing.
11361177

1137-
``void Functional\noop()``
1178+
``void Functional\noop()``

0 commit comments

Comments
 (0)