1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Iterators\Common; |
4
|
|
|
|
5
|
|
|
use Pinq\Iterators\IIterator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Common functionality for the projection iterator |
9
|
|
|
* |
10
|
|
|
* @author Elliot Levin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
trait ProjectionIterator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var callable|null |
16
|
|
|
*/ |
17
|
|
|
private $keyProjectionFunction; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable|null |
21
|
|
|
*/ |
22
|
|
|
private $valueProjectionFunction; |
23
|
|
|
|
24
|
|
|
protected function __constructIterator( |
25
|
|
|
callable $keyProjectionFunction = null, |
26
|
|
|
callable $valueProjectionFunction = null |
27
|
|
|
) { |
28
|
|
|
$this->keyProjectionFunction = $keyProjectionFunction === null ? |
29
|
|
|
null : Functions::allowExcessiveArguments($keyProjectionFunction); |
30
|
|
|
$this->valueProjectionFunction = $valueProjectionFunction === null ? |
31
|
|
|
null : Functions::allowExcessiveArguments($valueProjectionFunction); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
final protected function projectElement(&$key, &$value) |
35
|
|
|
{ |
36
|
|
|
$keyProjectionFunction = $this->keyProjectionFunction; |
37
|
|
|
$valueProjectionFunction = $this->valueProjectionFunction; |
38
|
|
|
|
39
|
|
|
$keyCopy = $key; |
40
|
|
|
$valueCopy = $value; |
41
|
|
|
|
42
|
|
|
if ($keyProjectionFunction !== null) { |
43
|
|
|
$keyCopyForKey = $keyCopy; |
44
|
|
|
$valueCopyForKey = $valueCopy; |
45
|
|
|
|
46
|
|
|
$keyProjection = $keyProjectionFunction($valueCopyForKey, $keyCopyForKey); |
47
|
|
|
} else { |
48
|
|
|
$keyProjection =& $key; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($valueProjectionFunction !== null) { |
52
|
|
|
$keyCopyForValue = $keyCopy; |
53
|
|
|
$valueCopyForValue = $valueCopy; |
54
|
|
|
|
55
|
|
|
$valueProjection = $valueProjectionFunction($valueCopyForValue, $keyCopyForValue); |
56
|
|
|
} else { |
57
|
|
|
$valueProjection =& $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [&$keyProjection, &$valueProjection]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return IIterator |
65
|
|
|
*/ |
66
|
|
|
abstract protected function getSourceIterator(); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
final public function isArrayCompatible() |
72
|
|
|
{ |
73
|
|
|
return $this->keyProjectionFunction === null && $this->getSourceIterator()->isArrayCompatible(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|