Skip to content

Commit 8c085cc

Browse files
committed
Setting up basic executed data preservation
1 parent 858857e commit 8c085cc

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/LazyCollection.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ abstract class LazyCollection implements Collection {
2020
*/
2121
protected $queue = [];
2222

23+
/**
24+
* A variable representing the execution chain for this collection;
25+
* @var null
26+
*/
27+
protected $executedValue = null;
28+
2329
/**
2430
* @see Collection::__construct
2531
*/
@@ -37,9 +43,17 @@ public function __construct($data, $queue = [], $limit = null) {
3743
* @return this Returns itself for chaining
3844
*/
3945
protected function enqueue($method, callable $callback) {
40-
$queue = $this->queue;
41-
$queue[] = [$method, $callback];
42-
return new static($this->data, $queue, $this->limit);
46+
$enqueued = [$method, $callback];
47+
48+
if($this->executedValue !== null) {
49+
// If this chain has been executed, then create a new object with only one chained method
50+
return new static($this->executedValue, [$enqueued], $this->limit);
51+
} else {
52+
// Otherwise extend the chain and continue
53+
$queue = $this->queue;
54+
$queue[] = $enqueued;
55+
return new static($this->data, $queue, $this->limit);
56+
}
4357
}
4458

4559
/**
@@ -80,7 +94,13 @@ public function reject(callable $callback) {
8094
* @see Collection::take
8195
*/
8296
public function take($limit = 1) {
83-
return new static($this->data, $this->queue, $limit);
97+
if($this->executedValue !== null) {
98+
// If this chain has been executed, then create a new object with no chained method
99+
return new static($this->executedValue, [], $limit);
100+
} else {
101+
// Otherwise keep the chain and modify the limit
102+
return new static($this->data, $this->queue, $limit);
103+
}
84104
}
85105

86106
/**
@@ -215,6 +235,11 @@ public function jsonSerialize() {
215235
* @see Collection::execute
216236
*/
217237
public function execute() {
238+
// If a cached value is available, run that
239+
if($this->executedValue !== null) {
240+
return $this->executedValue;
241+
}
242+
218243
// Group queued methods into execution blocks
219244
$execution_blocks = [];
220245
$current_block = [];
@@ -309,6 +334,8 @@ public function execute() {
309334
$returned = array_slice($returned, 0, $this->limit);
310335
}
311336

337+
$this->executedValue = $returned;
338+
312339
// Return the transformed data
313340
return $returned;
314341
}

test/IteratorWrapperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function testMap() {
2323
$collection = $this->collection->map([$this, 'doubleInteger'])->map([$this, 'incrementInteger']);
2424

2525
$this->assertEquals([9, 11, 7, 3, 5], $collection->execute());
26+
27+
// Validate that the chain is executed the same way consistently
28+
$this->assertEquals([9, 11, 7, 3, 5], $collection->execute());
2629
}
2730

2831
public function testEach() {

0 commit comments

Comments
 (0)