Failed Conditions
Push — master ( 6673f1...4ab425 )
by Arnold
10:03
created

MappingTrait::unwind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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