Passed
Push — master ( faf39f...21841e )
by Gerard van
43s
created

GroupByTrait   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 43
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 43
ccs 2
cts 2
cp 1
rs 10
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A groupBy() 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 GroupByTrait
12
{
13
    /**
14
     * Make an iterator that returns consecutive groups from this
15
     * iterable.  Generally, this iterable needs to already be sorted on
16
     * the same key function.
17
     *
18
     * When $strategy is a string, the key is obtained through one of
19
     * the following:
20
     * 1. $value->{$strategy}, when $value is an object and
21
     *    $strategy is an existing property,
22
     * 2. call $value->{$strategy}(), when $value is an object and
23
     *    $strategy is an existing method,
24
     * 3. $value[$strategy], when $value is an array and $strategy
25
     *    is an existing key,
26
     * 4. otherwise the key will default to null.
27
     *
28
     * Alternatively $strategy can be a closure.  In this case the
29
     * $strategy closure is called with each value in this iterable and the
30
     * key will be its return value.  $strategy is called with two
31
     * parameters: the value and the key of the iterable as the first and
32
     * second parameter, respectively.
33
     *
34
     * The operation of groupBy() is similar to the uniq filter in Unix.
35
     * It generates a break or new group every time the value of the key
36
     * function changes (which is why it is usually necessary to have
37
     * sorted the data using the same key function).  That behavior
38
     * differs from SQL's GROUP BY which aggregates common elements
39
     * regardless of their input order.
40
     *
41
     * > $list = [['type'=>'A', 'title'=>'one'], ['type'=>'A', 'title'=>'two'], ['type'=>'B', 'title'=>'three']]
42
     * > iter\iterable($list)->groupBy('type')
43
     * 'A'=>[['type'=>'A', 'title'=>'one'], ['type'=>'A', 'title'=>'two']] 'B'=>[['type'=>'B', 'title'=>'three']]
44
     *
45
     * @param null|string|\Closure $strategy
46
     * @param bool $sort
47
     * @return iter\lib\GroupbyIterator
48
     */
49 2
    public function groupBy($strategy, $sort = true)
50
    {
51 2
        return iter\groupBy($strategy, $this, $sort);
0 ignored issues
show
Deprecated Code introduced by
The function Zicht\Itertools\groupBy() has been deprecated with message: Please use group_by(...)->values() instead (when flatten true), 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...
52
    }
53
}
54