Skip to content

Commit

Permalink
Merge pull request #2 from bartoszkubicki/replacing-array-with-iterable
Browse files Browse the repository at this point in the history
Replacing array with iterable + minor fixes
  • Loading branch information
maciejslawik authored May 13, 2021
2 parents 26975ac + dbaec43 commit e2e4289
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 31 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1.0.0
* Initial version of module

1.0.1
* Adding missing php doc (important in frameworks basing on annotations)

2.0.0
* allowing to create `ObjectArray` from iterators (iterable psuedo-type - https://www.php.net/manual/en/language.types.iterable.php)
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Use composer to install the library
composer require mslwk/php-typesafe-array
```

## Changelog

See changelog [here](CHANGELOG.md).

## Authors

* [Maciej Sławik](https://github.com/maciejslawik)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": [
"MIT"
],
"version": "1.0.1",
"version": "2.0.0",
"authors": [
{
"name": "Maciej Sławik",
Expand Down
23 changes: 12 additions & 11 deletions src/ObjectArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Countable;
use InvalidArgumentException;
use Iterator;
use function get_class;

/**
* Class ObjectArray
Expand All @@ -35,10 +36,10 @@ class ObjectArray implements Countable, ArrayAccess, Iterator
/**
* ObjectArray constructor.
* @param string $type
* @param array $objects
* @param iterable $objects
* @throws InvalidArgumentException
*/
public function __construct(string $type, array $objects = [])
public function __construct(string $type, iterable $objects = [])
{
$this->type = $type;
foreach ($objects as $object) {
Expand All @@ -52,7 +53,7 @@ public function __construct(string $type, array $objects = [])
* @return void
* @throws InvalidArgumentException
*/
public function add($value)
public function add($value): void
{
$this->validateObjectType($value);
$this->iterator->append($value);
Expand All @@ -62,7 +63,7 @@ public function add($value)
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return $this->iterator->offsetExists($offset);
}
Expand All @@ -82,7 +83,7 @@ public function offsetGet($offset)
* @return void
* @throws InvalidArgumentException
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->validateObjectType($value);
$this->iterator->offsetSet($offset, $value);
Expand All @@ -92,15 +93,15 @@ public function offsetSet($offset, $value)
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
$this->iterator->offsetUnset($offset);
}

/**
* @return int
*/
public function count()
public function count(): int
{
return $this->iterator->count();
}
Expand All @@ -116,7 +117,7 @@ public function current()
/**
* @return void
*/
public function next()
public function next(): void
{
$this->iterator->next();
}
Expand All @@ -132,15 +133,15 @@ public function key()
/**
* @return bool
*/
public function valid()
public function valid(): bool
{
return $this->iterator->valid();
}

/**
* @return void
*/
public function rewind()
public function rewind(): void
{
$this->iterator->rewind();
}
Expand All @@ -150,7 +151,7 @@ public function rewind()
* @return void
* @throws InvalidArgumentException
*/
private function validateObjectType($object)
private function validateObjectType($object): void
{
if (!$object instanceof $this->type) {
throw new InvalidArgumentException(
Expand Down
4 changes: 2 additions & 2 deletions src/ObjectArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class ObjectArrayFactory
{
/**
* @param string $type
* @param array $objects
* @param iterable $objects
* @return ObjectArray
* @throws InvalidArgumentException
*/
public function create(string $type, array $objects = []): ObjectArray
public function create(string $type, iterable $objects = []): ObjectArray
{
return new ObjectArray($type, $objects);
}
Expand Down
7 changes: 2 additions & 5 deletions tests/Unit/ObjectArrayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
class ObjectArrayFactoryTest extends TestCase
{
/**
* @test
* @return void
*/
public function testFactoryReturnsCorrectClass()
public function testFactoryReturnsCorrectClass(): void
{
$factory = new ObjectArrayFactory();

$expected = ObjectArray::class;

$arrayClass = $factory->create(DateTime::class);

$this->assertInstanceOf($expected, $arrayClass);
}
}
46 changes: 34 additions & 12 deletions tests/Unit/ObjectArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace MSlwk\TypeSafeArray\Test\Unit;

use ArrayIterator;
use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;
Expand All @@ -23,15 +24,18 @@
class ObjectArrayTest extends TestCase
{
/**
* @test
* @dataProvider provideDifferentIterables
* @param iterable $iterable
* @return void
*/
public function testOperationsOnArray()
public function testOperationsOnArray(iterable $iterable): void
{
$object1 = new DateTime();
$object2 = new DateTime();
$object3 = new DateTime();
$object4 = new DateTime();
$objectArray = new ObjectArray(DateTime::class, [$object2]);
$iterable[] = $object2;
$objectArray = new ObjectArray(DateTime::class, $iterable);
$objectArray[1] = $object3;
$objectArray->offsetSet(2, $object4);
$objectArray->add($object1);
Expand Down Expand Up @@ -63,20 +67,23 @@ public function testOperationsOnArray()
}

/**
* @test
* @dataProvider provideDifferentIterables
* @param iterable $iterable
* @return void
*/
public function testAddingWrongTypeViaConstructor()
public function testAddingWrongTypeViaConstructor(iterable $iterable): void
{
$this->expectException(InvalidArgumentException::class);

$object = new DateTimeImmutable();
$objectArray = new ObjectArray(DateTime::class, [$object]);
$iterable[] = $object;
new ObjectArray(DateTime::class, $iterable);
}

/**
* @test
* @return void
*/
public function testAddingWrongTypeViaOffsetSet()
public function testAddingWrongTypeViaOffsetSet(): void
{
$this->expectException(InvalidArgumentException::class);

Expand All @@ -86,9 +93,9 @@ public function testAddingWrongTypeViaOffsetSet()
}

/**
* @test
* @return void
*/
public function testAddingWrongTypeViaAdd()
public function testAddingWrongTypeViaAdd(): void
{
$this->expectException(InvalidArgumentException::class);

Expand All @@ -98,14 +105,29 @@ public function testAddingWrongTypeViaAdd()
}

/**
* @test
* @return void
*/
public function testAddingWrongTypeViaArrayAccess()
public function testAddingWrongTypeViaArrayAccess(): void
{
$this->expectException(InvalidArgumentException::class);

$object = new DateTimeImmutable();
$objectArray = new ObjectArray(DateTime::class);
$objectArray[0] = $object;
}

/**
* @return array
*/
public function provideDifferentIterables(): array
{
return [
'plain_array' => [
[]
],
'iterator' => [
new ArrayIterator()
]
];
}
}

0 comments on commit e2e4289

Please sign in to comment.