|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of cloak. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace cloak\reflection\collection; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
use PhpCollection\Sequence; |
|
16
|
|
|
use PhpCollection\Map; |
|
17
|
|
|
use cloak\collection\PairStackable; |
|
18
|
|
|
use cloak\reflection\Reflection; |
|
19
|
|
|
use cloak\reflection\ResultConvertible; |
|
20
|
|
|
use cloak\Collection; |
|
21
|
|
|
use cloak\result\LineResultSelectable; |
|
22
|
|
|
use cloak\result\collection\CoverageResultCollection; |
|
23
|
|
|
use \Closure; |
|
24
|
|
|
use \Iterator; |
|
25
|
|
|
use \ArrayIterator; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class ReflectionCollection |
|
30
|
|
|
* @package cloak\reflection\collection |
|
31
|
|
|
*/ |
|
32
|
|
|
class ReflectionCollection implements Collection, ResultCollectionConvertible |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
use PairStackable; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \cloak\reflection\Reflection[] $reflections |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(array $reflections = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->collection = new Map(); |
|
44
|
|
|
$this->addAll($reflections); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Reflection $reflection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function add(Reflection $reflection) |
|
51
|
|
|
{ |
|
52
|
|
|
$identityName = $reflection->getIdentityName(); |
|
53
|
|
|
$this->collection->set($identityName, $reflection); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Reflection[] $reflections |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addAll(array $reflections) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->pushAll(new ArrayIterator($reflections)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param ReflectionCollection $reflections |
|
66
|
|
|
*/ |
|
67
|
|
|
public function merge(ReflectionCollection $reflections) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->pushAll( $reflections->getIterator() ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Iterator $reflections |
|
74
|
|
|
*/ |
|
75
|
|
|
private function pushAll(Iterator $reflections) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($reflections as $reflection) { |
|
78
|
|
|
$this->add($reflection); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param callable $filter |
|
84
|
|
|
* @return ReflectionCollection |
|
85
|
|
|
*/ |
|
86
|
|
|
public function filter(Closure $filter) |
|
87
|
|
|
{ |
|
88
|
|
|
$collection = $this->collection->filter($filter); |
|
89
|
|
|
return new self( $collection->values() ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function convertToResult(LineResultSelectable $selector) |
|
97
|
|
|
{ |
|
98
|
|
|
$values = $this->collection->values(); |
|
99
|
|
|
$collection = new Sequence($values); |
|
100
|
|
|
|
|
101
|
|
|
$convertCallback = function(ResultConvertible $reflection) use($selector) { |
|
102
|
|
|
return $reflection->convertToResult($selector); |
|
103
|
|
|
}; |
|
104
|
|
|
$results = $collection->map($convertCallback); |
|
105
|
|
|
|
|
106
|
|
|
return new CoverageResultCollection( $results->all() ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|