|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Zicht\Itertools\lib; |
|
7
|
|
|
|
|
8
|
|
|
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface; |
|
9
|
|
|
use Zicht\Itertools\lib\Traits\FiniteIterableTrait; |
|
10
|
|
|
|
|
11
|
|
|
class MapIterator extends \MultipleIterator implements FiniteIterableInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use FiniteIterableTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Closure */ |
|
16
|
|
|
private $valueFunc; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Closure */ |
|
19
|
|
|
private $keyFunc; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param \Closure $valueFunc |
|
23
|
|
|
*/ |
|
24
|
90 |
|
public function __construct(\Closure $valueFunc /* [\Closure $keyFunc], \Iterator $iterable1, [\Iterator $iterable2, [...]] */) |
|
25
|
|
|
{ |
|
26
|
90 |
|
parent::__construct(\MultipleIterator::MIT_NEED_ALL | \MultipleIterator::MIT_KEYS_NUMERIC); |
|
27
|
90 |
|
$args = func_get_args(); |
|
28
|
90 |
|
$argsContainsKeyFunc = $args[1] instanceof \Closure; |
|
29
|
90 |
|
$this->valueFunc = $args[0]; |
|
30
|
|
|
|
|
31
|
90 |
|
if ($argsContainsKeyFunc) { |
|
32
|
1 |
|
$this->keyFunc = $args[1]; |
|
33
|
|
|
} else { |
|
34
|
|
|
$this->keyFunc = function () { |
|
35
|
81 |
|
return $this->genericKeysToKey(func_get_args()); |
|
36
|
|
|
}; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
90 |
|
foreach (array_slice($args, $argsContainsKeyFunc ? 2 : 1) as $iterable) { |
|
40
|
90 |
|
if (!$iterable instanceof \Iterator) { |
|
41
|
1 |
|
throw new \InvalidArgumentException(sprintf('Not all arguments are iterators')); |
|
42
|
|
|
} |
|
43
|
89 |
|
$this->attachIterator($iterable); |
|
44
|
|
|
} |
|
45
|
89 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Compute the key for an element |
|
49
|
|
|
* |
|
50
|
|
|
* When multiple iterables are given, the key for an element is computed by taking |
|
51
|
|
|
* the keys of the element of all iterables, when they are the same, this key is used, |
|
52
|
|
|
* otherwise a compound key is created by concatenating the keys together. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $keysAndValues |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
81 |
|
protected function genericKeysToKey($keysAndValues) |
|
58
|
|
|
{ |
|
59
|
81 |
|
$keys = array_splice($keysAndValues, 0, count($keysAndValues) / 2); |
|
60
|
|
|
|
|
61
|
81 |
|
if (count($keys) == 1) { |
|
62
|
68 |
|
return $keys[0]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
13 |
|
$value = $keys[0]; |
|
66
|
13 |
|
foreach ($keys as $key) { |
|
67
|
13 |
|
if ($key !== $value) { |
|
68
|
|
|
// the keys are different, we will make a new string identifying this entry |
|
69
|
5 |
|
return join( |
|
70
|
5 |
|
':', |
|
71
|
5 |
|
array_map( |
|
72
|
5 |
|
function ($key) { |
|
73
|
5 |
|
return (string)$key; |
|
74
|
5 |
|
}, |
|
75
|
13 |
|
$keys |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// all values are the same, use it |
|
82
|
9 |
|
return $value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritDoc} |
|
87
|
|
|
*/ |
|
88
|
82 |
|
public function current() |
|
89
|
|
|
{ |
|
90
|
82 |
|
return call_user_func_array($this->valueFunc, array_merge(parent::current(), parent::key())); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritDoc} |
|
95
|
|
|
*/ |
|
96
|
82 |
|
public function key() |
|
97
|
|
|
{ |
|
98
|
82 |
|
return call_user_func_array($this->keyFunc, array_merge(parent::key(), parent::current())); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
82 |
|
public function next() |
|
105
|
|
|
{ |
|
106
|
82 |
|
parent::next(); |
|
107
|
82 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.