MapByTrait::mapBy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib\Traits;
7
8
use Zicht\Itertools\conversions;
9
use Zicht\Itertools\lib\MapByIterator;
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 null|string|\Closure $strategy
36
     * @return MapByIterator
37
     */
38 25
    public function mapBy($strategy)
39
    {
40 25
        if ($this instanceof \Iterator) {
41 24
            return new MapByIterator(
42 24
                conversions\mixed_to_value_getter($strategy),
0 ignored issues
show
Deprecated Code introduced by
The function Zicht\Itertools\conversi...mixed_to_value_getter() has been deprecated with message: Use \Zicht\Itertools\util\Conversions::mixedToClosure, will be removed in version 3.0

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
43 22
                $this
44
            );
45
        }
46
47 1
        return null;
48
    }
49
}
50