|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use TreeHouse\Feeder\Resource\Transformer\ResourceTransformerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class ResourceCollection extends \SplStack |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var ResourceTransformerInterface[] |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $transformers = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $transformed = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param ResourceInterface[] $resources |
|
21
|
|
|
*/ |
|
22
|
132 |
|
public function __construct(array $resources = []) |
|
23
|
|
|
{ |
|
24
|
132 |
|
$this->pushAll($resources); |
|
25
|
132 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return ResourceInterface |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function current() |
|
31
|
|
|
{ |
|
32
|
4 |
|
$resource = parent::current(); |
|
33
|
|
|
|
|
34
|
4 |
|
return $resource ? $this->transform($resource) : $resource; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return ResourceInterface |
|
39
|
|
|
*/ |
|
40
|
64 |
|
public function shift() |
|
41
|
|
|
{ |
|
42
|
64 |
|
return $this->transform(parent::shift()); |
|
43
|
8 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return ResourceInterface |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function pop() |
|
49
|
|
|
{ |
|
50
|
6 |
|
return $this->transform(parent::pop()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return ResourceInterface |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function bottom() |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->transform(parent::bottom()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return ResourceInterface |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function top() |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->transform(parent::top()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param int $index |
|
71
|
|
|
* |
|
72
|
|
|
* @return ResourceInterface |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function offsetGet($index) |
|
75
|
|
|
{ |
|
76
|
2 |
|
return $this->transform(parent::offsetGet($index)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param ResourceInterface[] $resources |
|
81
|
|
|
*/ |
|
82
|
132 |
|
public function pushAll(array $resources) |
|
83
|
|
|
{ |
|
84
|
132 |
|
foreach ($resources as $resource) { |
|
85
|
126 |
|
$this->push($resource); |
|
86
|
132 |
|
} |
|
87
|
|
|
|
|
88
|
132 |
|
$this->rewind(); |
|
89
|
132 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ResourceInterface[] $resources |
|
93
|
|
|
*/ |
|
94
|
10 |
|
public function unshiftAll(array $resources) |
|
95
|
|
|
{ |
|
96
|
10 |
|
foreach (array_reverse($resources) as $resource) { |
|
97
|
10 |
|
$this->unshift($resource); |
|
98
|
10 |
|
} |
|
99
|
|
|
|
|
100
|
10 |
|
$this->rewind(); |
|
101
|
10 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param ResourceTransformerInterface $transformer |
|
105
|
|
|
*/ |
|
106
|
22 |
|
public function addTransformer(ResourceTransformerInterface $transformer) |
|
107
|
|
|
{ |
|
108
|
22 |
|
$this->transformers[] = $transformer; |
|
109
|
22 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param ResourceInterface $resource |
|
113
|
|
|
* |
|
114
|
|
|
* @return ResourceInterface |
|
115
|
|
|
*/ |
|
116
|
76 |
|
protected function transform(ResourceInterface $resource) |
|
117
|
|
|
{ |
|
118
|
76 |
|
$hash = spl_object_hash($resource); |
|
119
|
|
|
|
|
120
|
|
|
// see if it needs transforming |
|
121
|
76 |
|
if (!in_array($hash, $this->transformed)) { |
|
122
|
76 |
|
foreach ($this->transformers as $transformer) { |
|
123
|
22 |
|
if ($transformer->needsTransforming($resource)) { |
|
124
|
22 |
|
$resource = $transformer->transform($resource, $this); |
|
125
|
22 |
|
} |
|
126
|
76 |
|
} |
|
127
|
|
|
|
|
128
|
76 |
|
$this->transformed[] = $hash; |
|
129
|
76 |
|
} |
|
130
|
|
|
|
|
131
|
76 |
|
return $resource; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|