Skip to content

Commit

Permalink
Merge 3f4a4e0 into e2e4289
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszkubicki committed May 14, 2021
2 parents e2e4289 + 3f4a4e0 commit bdccf59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ObjectArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class ObjectArray implements Countable, ArrayAccess, Iterator
public function __construct(string $type, iterable $objects = [])
{
$this->type = $type;
$this->iterator = new ArrayIterator();
foreach ($objects as $object) {
$this->validateObjectType($object);
$this->add($object);
}
$this->iterator = new ArrayIterator($objects);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/ObjectArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace MSlwk\TypeSafeArray\Test\Unit;

use ArrayAccess;
use ArrayIterator;
use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;
use IteratorAggregate;
use MSlwk\TypeSafeArray\ObjectArray;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -121,12 +123,48 @@ public function testAddingWrongTypeViaArrayAccess(): void
*/
public function provideDifferentIterables(): array
{
$iteratorAggregate = new class() implements IteratorAggregate, ArrayAccess {
private $innerIterator;
public function __construct()
{
$this->innerIterator = new ArrayIterator();
}

public function getIterator(): ArrayIterator
{
return $this->innerIterator;
}

public function offsetExists($offset): bool
{
return $this->innerIterator->offsetExists($offset);
}

public function offsetGet($offset)
{
return $this->innerIterator->offsetGet($offset);
}

public function offsetSet($offset, $value): void
{
$this->innerIterator->offsetSet($offset, $value);
}

public function offsetUnset($offset): void
{
$this->innerIterator->offsetUnset($offset);
}
};

return [
'plain_array' => [
[]
],
'iterator' => [
new ArrayIterator()
],
'iterator-aggregate' => [
$iteratorAggregate
]
];
}
Expand Down

0 comments on commit bdccf59

Please sign in to comment.