1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Boudewijn Schoon <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Itertools\lib\Traits; |
8
|
|
|
|
9
|
|
|
use Zicht\Itertools as iter; |
10
|
|
|
|
11
|
|
|
trait MapByTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Make an iterator returning values from this iterable and keys |
15
|
|
|
* from $STRATEGY. |
16
|
|
|
* |
17
|
|
|
* When $strategy is a string, the key is obtained through one of |
18
|
|
|
* the following: |
19
|
|
|
* 1. $value->{$strategy}, when $value is an object and |
20
|
|
|
* $strategy is an existing property, |
21
|
|
|
* 2. call $value->{$strategy}(), when $value is an object and |
22
|
|
|
* $strategy is an existing method, |
23
|
|
|
* 3. $value[$strategy], when $value is an array and $strategy |
24
|
|
|
* is an existing key, |
25
|
|
|
* 4. otherwise the key will default to null. |
26
|
|
|
* |
27
|
|
|
* Alternatively $strategy can be a closure. In this case the |
28
|
|
|
* $strategy closure is called with each value in $iterable and the |
29
|
|
|
* key will be its return value. |
30
|
|
|
* |
31
|
|
|
* > $list = [['id'=>1, 'title'=>'one'], ['id'=>2, 'title'=>'two']] |
32
|
|
|
* > iter\iterable($list)->mapBy('id') |
33
|
|
|
* 1=>['id'=>1, 'title'=>'one'] 2=>['id'=>2, 'title'=>'two'] |
34
|
|
|
* |
35
|
|
|
* @param string|\Closure $strategy |
36
|
|
|
* @return iter\lib\MapByIterator |
37
|
|
|
*/ |
38
|
1 |
|
public function mapBy($strategy) |
39
|
|
|
{ |
40
|
1 |
|
return iter\mapBy($strategy, $this); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|