Add basic Collection class with add method
This commit is contained in:
parent
9d6eb52ed5
commit
a9665c3611
7 changed files with 117 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
/.idea
|
/.idea
|
||||||
/vendor
|
/vendor
|
||||||
composer.lock
|
composer.lock
|
||||||
|
.phpunit.result.cache
|
|
@ -2,12 +2,12 @@
|
||||||
"name": "loomlabs/utility.collection",
|
"name": "loomlabs/utility.collection",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"LoomLabs\\Utility\\Collection\\": "src/"
|
"Loom\\Utility\\Collection\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"LoomLabs\\Utility\\Collection\\Tests\\": "tests/"
|
"Loom\\Utility\\Collection\\Tests\\": "tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
|
23
phpunit.xml
Normal file
23
phpunit.xml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit colors="true"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
displayDetailsOnIncompleteTests="true"
|
||||||
|
displayDetailsOnSkippedTests="true"
|
||||||
|
displayDetailsOnTestsThatTriggerDeprecations="true"
|
||||||
|
displayDetailsOnTestsThatTriggerWarnings="true"
|
||||||
|
displayDetailsOnTestsThatTriggerNotices="true"
|
||||||
|
failOnWarning="true"
|
||||||
|
failOnRisky="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Unit Tests">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<coverage/>
|
||||||
|
<source>
|
||||||
|
<include>
|
||||||
|
<directory suffix=".php">src/</directory>
|
||||||
|
</include>
|
||||||
|
</source>
|
||||||
|
</phpunit>
|
24
src/AbstractCollection.php
Normal file
24
src/AbstractCollection.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Loom\Utility\Collection;
|
||||||
|
|
||||||
|
use Loom\Utility\Collection\Interface\CollectionInterface;
|
||||||
|
|
||||||
|
class AbstractCollection implements CollectionInterface
|
||||||
|
{
|
||||||
|
public function __construct(protected array $items = [])
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add(mixed $item): void
|
||||||
|
{
|
||||||
|
$this->items[] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
}
|
18
src/Collection.php
Normal file
18
src/Collection.php
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Loom\Utility\Collection;
|
||||||
|
|
||||||
|
class Collection extends AbstractCollection implements \IteratorAggregate, \Countable
|
||||||
|
{
|
||||||
|
public function getIterator(): \Traversable
|
||||||
|
{
|
||||||
|
return new \ArrayIterator($this->items);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return count($this->items);
|
||||||
|
}
|
||||||
|
}
|
10
src/Interface/CollectionInterface.php
Normal file
10
src/Interface/CollectionInterface.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Loom\Utility\Collection\Interface;
|
||||||
|
|
||||||
|
interface CollectionInterface
|
||||||
|
{
|
||||||
|
public function add(mixed $item): void;
|
||||||
|
public function toArray(): array;
|
||||||
|
}
|
38
tests/CollectionTest.php
Normal file
38
tests/CollectionTest.php
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Loom\Utility\Collection\Tests;
|
||||||
|
|
||||||
|
use Loom\Utility\Collection\Collection;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class CollectionTest extends TestCase
|
||||||
|
{
|
||||||
|
#[DataProvider('addDataProvider')]
|
||||||
|
public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult): void
|
||||||
|
{
|
||||||
|
$collection = new Collection($startingItems);
|
||||||
|
$collection->add($itemToAdd);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedResult, $collection->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function addDataProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'startingItems' => ['apple', 'banana', 'cherry'],
|
||||||
|
'itemToAdd' => 'grape',
|
||||||
|
'expectedResult' => ['apple', 'banana', 'cherry', 'grape'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'startingItems' => [],
|
||||||
|
'itemToAdd' => 1,
|
||||||
|
'expectedResult' => [1],
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue