1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection\Stream; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\Util\Optional; |
6
|
|
|
use Bdf\Collection\Util\OptionalInterface; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Iterator; |
9
|
|
|
use function array_combine; |
10
|
|
|
use function array_values; |
11
|
|
|
use function current; |
12
|
|
|
use function key; |
13
|
|
|
use function next; |
14
|
|
|
use function reset; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Make a stream with combine keys and values |
18
|
|
|
* Works like PHP array_combine() function, but create a stream instead of an array |
19
|
|
|
* This streams works with any type of key, like objects or arrays |
20
|
|
|
* |
21
|
|
|
* /!\ Some methods can have an unexpected behavior with complex keys |
22
|
|
|
* The preserve keys parameter must be set to false for : |
23
|
|
|
* - sort() : An array is created before sorting it |
24
|
|
|
* - toArray() : Cannot create an array with complex key |
25
|
|
|
* |
26
|
|
|
* <code> |
27
|
|
|
* $stream = new ArrayCombineStream( |
28
|
|
|
* [new Geoloc(14.23, -5.02), new Geoloc(12.6, 1.25)], |
29
|
|
|
* [new Location(1), new Location(2)] |
30
|
|
|
* ); |
31
|
|
|
* |
32
|
|
|
* foreach ($stream as $geoloc => $location) { |
33
|
|
|
* // First iteration : $geoloc = Geoloc(14.23, -5.02), $location = Location(1) |
34
|
|
|
* // Second iteration : $geoloc = Geoloc(12.6, 1.25), $location = Location(2) |
35
|
|
|
* } |
36
|
|
|
* </code> |
37
|
|
|
* |
38
|
|
|
* @see array_combine() |
39
|
|
|
*/ |
40
|
|
|
final class ArrayCombineStream implements Iterator, StreamInterface |
41
|
|
|
{ |
42
|
|
|
use StreamTrait; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $keys; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $values; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* ArrayCombineStream constructor. |
57
|
|
|
* |
58
|
|
|
* The two parameters must be two arrays of same size |
59
|
|
|
* |
60
|
|
|
* @param array $keys The stream keys |
61
|
|
|
* @param array $values The stream values |
62
|
|
|
*/ |
63
|
22 |
|
public function __construct(array $keys, array $values) |
64
|
|
|
{ |
65
|
22 |
|
if (count($keys) !== count($values)) { |
66
|
1 |
|
throw new InvalidArgumentException('The two arrays have different size'); |
67
|
|
|
} |
68
|
|
|
|
69
|
21 |
|
$this->keys = $keys; |
70
|
21 |
|
$this->values = $values; |
71
|
21 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
3 |
|
public function toArray(bool $preserveKeys = true): array |
77
|
|
|
{ |
78
|
3 |
|
return $preserveKeys |
79
|
1 |
|
? array_combine($this->keys, $this->values) |
80
|
3 |
|
: array_values($this->values) |
81
|
|
|
; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function first(): OptionalInterface |
88
|
|
|
{ |
89
|
1 |
|
if (empty($this->values)) { |
90
|
1 |
|
return Optional::empty(); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
reset($this->values); |
94
|
|
|
|
95
|
1 |
|
return Optional::of(current($this->values)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
#[\ReturnTypeWillChange] |
102
|
17 |
|
public function current() |
103
|
|
|
{ |
104
|
17 |
|
return $this->values[key($this->keys)]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
17 |
|
public function next(): void |
111
|
|
|
{ |
112
|
17 |
|
next($this->keys); |
113
|
17 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
#[\ReturnTypeWillChange] |
119
|
16 |
|
public function key() |
120
|
|
|
{ |
121
|
16 |
|
return current($this->keys); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
17 |
|
public function valid(): bool |
128
|
|
|
{ |
129
|
17 |
|
return key($this->keys) !== null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
17 |
|
public function rewind(): void |
136
|
|
|
{ |
137
|
17 |
|
reset($this->keys); |
138
|
17 |
|
} |
139
|
|
|
} |
140
|
|
|
|