Skip to content

Commit 621c44d

Browse files
committed
AbstractCollectionMutator: refactor out SqlFixedArray which has had a BC break (update InputCollectionMutator + OutputCollectionMutator)
1 parent aa66810 commit 621c44d

File tree

3 files changed

+59
-87
lines changed

3 files changed

+59
-87
lines changed

src/Transaction/Mutator/AbstractCollectionMutator.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
abstract class AbstractCollectionMutator implements \Iterator, \ArrayAccess, \Countable
88
{
99
/**
10-
* @var \SplFixedArray
10+
* @var array
1111
*/
12-
protected $set;
12+
protected $set = [];
13+
14+
private $position = 0;
1315

1416
/**
1517
* @return array
1618
*/
1719
public function all(): array
1820
{
19-
return $this->set->toArray();
21+
return $this->set;
2022
}
2123

2224
/**
@@ -32,56 +34,44 @@ public function isNull(): bool
3234
*/
3335
public function count(): int
3436
{
35-
return $this->set->count();
37+
return count($this->set);
3638
}
3739

38-
/**
39-
*
40-
*/
41-
public function rewind()
40+
public function rewind(): void
4241
{
43-
$this->set->rewind();
42+
$this->position = 0;
4443
}
4544

4645
/**
4746
* @return mixed
4847
*/
4948
public function current()
5049
{
51-
return $this->set->current();
50+
return $this->set[$this->position];
5251
}
5352

54-
/**
55-
* @return int
56-
*/
57-
public function key()
53+
public function key(): int
5854
{
59-
return $this->set->key();
55+
return $this->position;
6056
}
6157

62-
/**
63-
*
64-
*/
65-
public function next()
58+
public function next(): void
6659
{
67-
$this->set->next();
60+
++$this->position;
6861
}
6962

70-
/**
71-
* @return bool
72-
*/
73-
public function valid()
63+
public function valid(): bool
7464
{
75-
return $this->set->valid();
65+
return array_key_exists($this->position, $this->set);
7666
}
7767

7868
/**
7969
* @param int $offset
8070
* @return bool
8171
*/
82-
public function offsetExists($offset)
72+
public function offsetExists($offset): bool
8373
{
84-
return $this->set->offsetExists($offset);
74+
return array_key_exists($offset, $this->set);
8575
}
8676

8777
/**
@@ -93,7 +83,7 @@ public function offsetUnset($offset)
9383
throw new \InvalidArgumentException('Offset does not exist');
9484
}
9585

96-
$this->set->offsetUnset($offset);
86+
$this->set = array_slice($this->set, 0, $offset - 1) + array_slice($this->set, $offset + 1);
9787
}
9888

9989
/**
@@ -102,10 +92,10 @@ public function offsetUnset($offset)
10292
*/
10393
public function offsetGet($offset)
10494
{
105-
if (!$this->set->offsetExists($offset)) {
95+
if (!array_key_exists($offset, $this->set)) {
10696
throw new \OutOfRangeException('Nothing found at this offset');
10797
}
108-
return $this->set->offsetGet($offset);
98+
return $this->set[$offset];
10999
}
110100

111101
/**
@@ -114,6 +104,9 @@ public function offsetGet($offset)
114104
*/
115105
public function offsetSet($offset, $value)
116106
{
117-
$this->set->offsetSet($offset, $value);
107+
if ($offset > count($this->set)) {
108+
throw new \InvalidArgumentException();
109+
}
110+
$this->set[$offset] = $value;
118111
}
119112
}

src/Transaction/Mutator/InputCollectionMutator.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class InputCollectionMutator extends AbstractCollectionMutator
1010
{
11-
1211
/**
1312
* @param TransactionInputInterface[] $inputs
1413
*/
@@ -20,15 +19,12 @@ public function __construct(array $inputs)
2019
$set[$i] = new InputMutator($input);
2120
}
2221

23-
$this->set = \SplFixedArray::fromArray($set, false);
22+
$this->set = $set;
2423
}
2524

26-
/**
27-
* @return InputMutator
28-
*/
2925
public function current(): InputMutator
3026
{
31-
return $this->set->current();
27+
return parent::current();
3228
}
3329

3430
/**
@@ -37,11 +33,7 @@ public function current(): InputMutator
3733
*/
3834
public function offsetGet($offset): InputMutator
3935
{
40-
if (!$this->set->offsetExists($offset)) {
41-
throw new \OutOfRangeException('Input does not exist');
42-
}
43-
44-
return $this->set->offsetGet($offset);
36+
return parent::offsetGet($offset);
4537
}
4638

4739
/**
@@ -58,39 +50,30 @@ public function done(): array
5850
}
5951

6052
/**
61-
* @param int $start
62-
* @param int $length
63-
* @return $this
53+
* Return an array containing values beginning at index $start and ending
54+
* with index $start + $length. An exception is thrown if start or $length
55+
* is out of bounds
6456
*/
65-
public function slice(int $start, int $length)
57+
public function slice(int $start, int $length): InputCollectionMutator
6658
{
67-
$end = $this->set->getSize();
59+
$end = $this->count();
6860
if ($start > $end || $length > $end) {
6961
throw new \RuntimeException('Invalid start or length');
7062
}
7163

72-
$this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
64+
$this->set = array_slice($this->set, $start, $length);
7365
return $this;
7466
}
7567

76-
/**
77-
* @return $this
78-
*/
79-
public function null()
68+
public function null(): InputCollectionMutator
8069
{
8170
$this->slice(0, 0);
8271
return $this;
8372
}
8473

85-
/**
86-
* @param TransactionInputInterface $input
87-
* @return $this
88-
*/
89-
public function add(TransactionInputInterface $input)
74+
public function add(TransactionInputInterface $input): InputCollectionMutator
9075
{
91-
$size = $this->set->getSize();
92-
$this->set->setSize($size + 1);
93-
76+
$size = $this->count();
9477
$this->set[$size] = new InputMutator($input);
9578
return $this;
9679
}
@@ -100,9 +83,12 @@ public function add(TransactionInputInterface $input)
10083
* @param TransactionInputInterface $input
10184
* @return $this
10285
*/
103-
public function set(int $i, TransactionInputInterface $input)
86+
public function set(int $i, TransactionInputInterface $input): InputCollectionMutator
10487
{
105-
$this->set[$i] = new InputMutator($input);
88+
if ($i > count($this->set)) {
89+
throw new \InvalidArgumentException();
90+
}
91+
$this->set[$i] = $input;
10692
return $this;
10793
}
10894
}

src/Transaction/Mutator/OutputCollectionMutator.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ class OutputCollectionMutator extends AbstractCollectionMutator
1414
public function __construct(array $outputs)
1515
{
1616
/** @var OutputMutator[] $set */
17-
$this->set = new \SplFixedArray(count($outputs));
17+
$set = [];
1818
foreach ($outputs as $i => $output) {
1919
/** @var int $i */
20-
$this->set[$i] = new OutputMutator($output);
20+
$set[$i] = new OutputMutator($output);
2121
}
22+
23+
$this->set = $set;
2224
}
2325

24-
/**
25-
* @return OutputMutator
26-
*/
2726
public function current(): OutputMutator
2827
{
29-
return $this->set->current();
28+
return parent::current();
3029
}
3130

3231
/**
@@ -35,11 +34,7 @@ public function current(): OutputMutator
3534
*/
3635
public function offsetGet($offset): OutputMutator
3736
{
38-
if (!$this->set->offsetExists($offset)) {
39-
throw new \OutOfRangeException('Nothing found at this offset');
40-
}
41-
42-
return $this->set->offsetGet($offset);
37+
return parent::offsetGet($offset);
4338
}
4439

4540
/**
@@ -56,25 +51,22 @@ public function done(): array
5651
}
5752

5853
/**
59-
* @param int $start
60-
* @param int $length
61-
* @return $this
54+
* Return an array containing values beginning at index $start and ending
55+
* with index $start + $length. An exception is thrown if start or $length
56+
* is out of bounds
6257
*/
63-
public function slice(int $start, int $length)
58+
public function slice(int $start, int $length): OutputCollectionMutator
6459
{
6560
$end = count($this->set);
6661
if ($start > $end || $length > $end) {
6762
throw new \RuntimeException('Invalid start or length');
6863
}
6964

70-
$this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
65+
$this->set = array_slice($this->set, $start, $length);
7166
return $this;
7267
}
7368

74-
/**
75-
* @return $this
76-
*/
77-
public function null()
69+
public function null(): OutputCollectionMutator
7870
{
7971
$this->slice(0, 0);
8072
return $this;
@@ -84,11 +76,9 @@ public function null()
8476
* @param TransactionOutputInterface $output
8577
* @return $this
8678
*/
87-
public function add(TransactionOutputInterface $output)
79+
public function add(TransactionOutputInterface $output): OutputCollectionMutator
8880
{
89-
$size = $this->set->getSize();
90-
$this->set->setSize($size + 1);
91-
81+
$size = $this->count();
9282
$this->set[$size] = new OutputMutator($output);
9383
return $this;
9484
}
@@ -98,8 +88,11 @@ public function add(TransactionOutputInterface $output)
9888
* @param TransactionOutputInterface $output
9989
* @return $this
10090
*/
101-
public function set($i, TransactionOutputInterface $output)
91+
public function set(int $i, TransactionOutputInterface $output): OutputCollectionMutator
10292
{
93+
if ($i > count($this->set)) {
94+
throw new \InvalidArgumentException();
95+
}
10396
$this->set[$i] = new OutputMutator($output);
10497
return $this;
10598
}

0 commit comments

Comments
 (0)