Skip to content

Commit

Permalink
AbstractCollectionMutator: refactor out SqlFixedArray which has had a…
Browse files Browse the repository at this point in the history
… BC break (update InputCollectionMutator + OutputCollectionMutator)
  • Loading branch information
afk11 committed Apr 24, 2021
1 parent aa66810 commit 621c44d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 87 deletions.
55 changes: 24 additions & 31 deletions src/Transaction/Mutator/AbstractCollectionMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
abstract class AbstractCollectionMutator implements \Iterator, \ArrayAccess, \Countable
{
/**
* @var \SplFixedArray
* @var array
*/
protected $set;
protected $set = [];

private $position = 0;

/**
* @return array
*/
public function all(): array
{
return $this->set->toArray();
return $this->set;
}

/**
Expand All @@ -32,56 +34,44 @@ public function isNull(): bool
*/
public function count(): int
{
return $this->set->count();
return count($this->set);
}

/**
*
*/
public function rewind()
public function rewind(): void
{
$this->set->rewind();
$this->position = 0;
}

/**
* @return mixed
*/
public function current()
{
return $this->set->current();
return $this->set[$this->position];
}

/**
* @return int
*/
public function key()
public function key(): int
{
return $this->set->key();
return $this->position;
}

/**
*
*/
public function next()
public function next(): void
{
$this->set->next();
++$this->position;
}

/**
* @return bool
*/
public function valid()
public function valid(): bool
{
return $this->set->valid();
return array_key_exists($this->position, $this->set);
}

/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return $this->set->offsetExists($offset);
return array_key_exists($offset, $this->set);
}

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

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

/**
Expand All @@ -102,10 +92,10 @@ public function offsetUnset($offset)
*/
public function offsetGet($offset)
{
if (!$this->set->offsetExists($offset)) {
if (!array_key_exists($offset, $this->set)) {
throw new \OutOfRangeException('Nothing found at this offset');
}
return $this->set->offsetGet($offset);
return $this->set[$offset];
}

/**
Expand All @@ -114,6 +104,9 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
$this->set->offsetSet($offset, $value);
if ($offset > count($this->set)) {
throw new \InvalidArgumentException();
}
$this->set[$offset] = $value;
}
}
48 changes: 17 additions & 31 deletions src/Transaction/Mutator/InputCollectionMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class InputCollectionMutator extends AbstractCollectionMutator
{

/**
* @param TransactionInputInterface[] $inputs
*/
Expand All @@ -20,15 +19,12 @@ public function __construct(array $inputs)
$set[$i] = new InputMutator($input);
}

$this->set = \SplFixedArray::fromArray($set, false);
$this->set = $set;
}

/**
* @return InputMutator
*/
public function current(): InputMutator
{
return $this->set->current();
return parent::current();
}

/**
Expand All @@ -37,11 +33,7 @@ public function current(): InputMutator
*/
public function offsetGet($offset): InputMutator
{
if (!$this->set->offsetExists($offset)) {
throw new \OutOfRangeException('Input does not exist');
}

return $this->set->offsetGet($offset);
return parent::offsetGet($offset);
}

/**
Expand All @@ -58,39 +50,30 @@ public function done(): array
}

/**
* @param int $start
* @param int $length
* @return $this
* Return an array containing values beginning at index $start and ending
* with index $start + $length. An exception is thrown if start or $length
* is out of bounds
*/
public function slice(int $start, int $length)
public function slice(int $start, int $length): InputCollectionMutator
{
$end = $this->set->getSize();
$end = $this->count();
if ($start > $end || $length > $end) {
throw new \RuntimeException('Invalid start or length');
}

$this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
$this->set = array_slice($this->set, $start, $length);
return $this;
}

/**
* @return $this
*/
public function null()
public function null(): InputCollectionMutator
{
$this->slice(0, 0);
return $this;
}

/**
* @param TransactionInputInterface $input
* @return $this
*/
public function add(TransactionInputInterface $input)
public function add(TransactionInputInterface $input): InputCollectionMutator
{
$size = $this->set->getSize();
$this->set->setSize($size + 1);

$size = $this->count();
$this->set[$size] = new InputMutator($input);
return $this;
}
Expand All @@ -100,9 +83,12 @@ public function add(TransactionInputInterface $input)
* @param TransactionInputInterface $input
* @return $this
*/
public function set(int $i, TransactionInputInterface $input)
public function set(int $i, TransactionInputInterface $input): InputCollectionMutator
{
$this->set[$i] = new InputMutator($input);
if ($i > count($this->set)) {
throw new \InvalidArgumentException();
}
$this->set[$i] = $input;
return $this;
}
}
43 changes: 18 additions & 25 deletions src/Transaction/Mutator/OutputCollectionMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ class OutputCollectionMutator extends AbstractCollectionMutator
public function __construct(array $outputs)
{
/** @var OutputMutator[] $set */
$this->set = new \SplFixedArray(count($outputs));
$set = [];
foreach ($outputs as $i => $output) {
/** @var int $i */
$this->set[$i] = new OutputMutator($output);
$set[$i] = new OutputMutator($output);
}

$this->set = $set;
}

/**
* @return OutputMutator
*/
public function current(): OutputMutator
{
return $this->set->current();
return parent::current();
}

/**
Expand All @@ -35,11 +34,7 @@ public function current(): OutputMutator
*/
public function offsetGet($offset): OutputMutator
{
if (!$this->set->offsetExists($offset)) {
throw new \OutOfRangeException('Nothing found at this offset');
}

return $this->set->offsetGet($offset);
return parent::offsetGet($offset);
}

/**
Expand All @@ -56,25 +51,22 @@ public function done(): array
}

/**
* @param int $start
* @param int $length
* @return $this
* Return an array containing values beginning at index $start and ending
* with index $start + $length. An exception is thrown if start or $length
* is out of bounds
*/
public function slice(int $start, int $length)
public function slice(int $start, int $length): OutputCollectionMutator
{
$end = count($this->set);
if ($start > $end || $length > $end) {
throw new \RuntimeException('Invalid start or length');
}

$this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length), false);
$this->set = array_slice($this->set, $start, $length);
return $this;
}

/**
* @return $this
*/
public function null()
public function null(): OutputCollectionMutator
{
$this->slice(0, 0);
return $this;
Expand All @@ -84,11 +76,9 @@ public function null()
* @param TransactionOutputInterface $output
* @return $this
*/
public function add(TransactionOutputInterface $output)
public function add(TransactionOutputInterface $output): OutputCollectionMutator
{
$size = $this->set->getSize();
$this->set->setSize($size + 1);

$size = $this->count();
$this->set[$size] = new OutputMutator($output);
return $this;
}
Expand All @@ -98,8 +88,11 @@ public function add(TransactionOutputInterface $output)
* @param TransactionOutputInterface $output
* @return $this
*/
public function set($i, TransactionOutputInterface $output)
public function set(int $i, TransactionOutputInterface $output): OutputCollectionMutator
{
if ($i > count($this->set)) {
throw new \InvalidArgumentException();
}
$this->set[$i] = new OutputMutator($output);
return $this;
}
Expand Down

0 comments on commit 621c44d

Please sign in to comment.