Passed
Pull Request — master (#19)
by
unknown
03:59
created

MapByTrait   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 2
cts 2
cp 1
rs 10
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mapBy() 0 4 1
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