Passed
Push — master ( a16e7b...0b226c )
by Arnold
07:52
created

MappingTrait::project()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved\IteratorPipeline\Traits;
6
7
use Improved as i;
8
use Improved\Iterator\CombineIterator;
9
10
/**
11
 * Mapping and projection methods for iterator pipeline
12
 */
13
trait MappingTrait
14
{
15
    /**
16
     * Define the next step via a callback that returns an array or Traversable object.
17
     *
18
     * @param callable $callback
19
     * @param mixed    ...$args
20
     * @return static
21
     */
22
    abstract public function then(callable $callback, ...$args);
23
24
25
    /**
26
     * Map each element to a value using a callback function.
27
     *
28
     * @param callable $callback
29
     * @return static
30
     */
31 3
    public function map(callable $callback)
32
    {
33 3
        return $this->then(i\iterable_map, $callback);
34
    }
35
36
    /**
37
     * Map the key of each element to a new key using a callback function.
38
     *
39
     * @param callable $callback
40
     * @return static
41
     */
42 1
    public function mapKeys(callable $callback)
43
    {
44 1
        return $this->then(i\iterable_map_keys, $callback);
45
    }
46
47
    /**
48
     * Apply a callback to each element of an iterator.
49
     * Any value returned by the callback is ignored.
50
     *
51
     * @param callable $callback
52
     * @return static
53
     */
54 3
    public function apply(callable $callback)
55
    {
56 3
        return $this->then(i\iterable_apply, $callback);
57
    }
58
59
    /**
60
     * Divide iterable into chunks of specified size.
61
     *
62
     * @param int $size
63
     * @return static
64
     */
65 1
    public function chunk(int $size)
66
    {
67 1
        return $this->then(i\iterable_chunk, $size);
68
    }
69
70
    /**
71
     * Group elements of an iterator, with the group name as key and an array of elements as value.
72
     *
73
     * @param callable $grouping
74
     * @return static
75
     */
76 1
    public function group(callable $grouping)
77
    {
78 1
        return $this->then(i\iterable_group, $grouping);
79
    }
80
81
    /**
82
     * Walk through all sub-iterables and concatenate them.
83
     *
84
     * @return static
85
     */
86 1
    public function flatten()
87
    {
88 1
        return $this->then(i\iterable_flatten);
89
    }
90
91
92
    /**
93
     * Return the values from a single column / property.
94
     * Create key/value pairs by specifying the key.
95
     *
96
     * @param string|int|null $valueColumn  null to keep values
97
     * @param string|int|null $keyColumn    null to keep keys
98
     * @return static
99
     */
100 2
    public function column($valueColumn, $keyColumn = null)
101
    {
102 2
        return $this->then(i\iterable_column, $valueColumn, $keyColumn);
103
    }
104
105
    /**
106
     * Project each element of an iterator to an associated (or numeric) array.
107
     * Each element should be an array or object.
108
     *
109
     * @param array $mapping  [new key => old key, ...]
110
     * @return static
111
     */
112 1
    public function project(array $mapping)
113
    {
114 1
        return $this->then(i\iterable_project, $mapping);
115
    }
116
117
    /**
118
     * Reshape each element of an iterator, adding or removing properties or keys.
119
     *
120
     * @param array $columns  [key => bool|string|int, ...]
121
     * @return static
122
     */
123 1
    public function reshape(array $columns)
124
    {
125 1
        return $this->then(i\iterable_reshape, $columns);
126
    }
127
128
129
    /**
130
     * Keep the values, drop the keys. The keys become an incremental number.
131
     *
132
     * @return static
133
     */
134 8
    public function values()
135
    {
136 8
        return $this->then(i\iterable_values);
137
    }
138
139
    /**
140
     * Use the keys as values. The keys become an incremental number.
141
     *
142
     * @return static
143
     */
144 1
    public function keys()
145
    {
146 1
        return $this->then(i\iterable_keys);
147
    }
148
149
    /**
150
     * Use another iterator as keys and the current iterator as values.
151
     *
152
     * @param iterable $keys
153
     * @return static
154
     */
155 4
    public function setKeys(iterable $keys)
156
    {
157
        $combine = function ($values, $keys) {
158 4
            return new CombineIterator($keys, $values);
159 4
        };
160
161 4
        return $this->then($combine, $keys);
162
    }
163
164
    /**
165
     * Use values as keys and visa versa.
166
     *
167
     * @return static
168
     */
169 1
    public function flip()
170
    {
171 1
        return $this->then(i\iterable_flip);
172
    }
173
}
174