Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
2114c6e312 | |||
![]() |
d439a25084 | ||
b638d684a2 | |||
![]() |
a986b85d2f | ||
![]() |
787b91e880 | ||
![]() |
b9c90339b4 | ||
![]() |
69eed6681a | ||
![]() |
76db32a24e |
8 changed files with 61 additions and 6 deletions
4
.gitattributes
vendored
4
.gitattributes
vendored
|
@ -1,3 +1,5 @@
|
||||||
phpunit.xml export-ignore
|
phpunit.xml export-ignore
|
||||||
tests/ export-ignore
|
tests/ export-ignore
|
||||||
CHANGELOG.md export-ignore
|
CHANGELOG.md export-ignore
|
||||||
|
package.json export-ignore
|
||||||
|
.gitignore export-ignore
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,7 @@
|
||||||
/.idea
|
/.idea
|
||||||
/vendor
|
/vendor
|
||||||
|
/node_modules
|
||||||
|
/coverage
|
||||||
composer.lock
|
composer.lock
|
||||||
|
package-lock.json
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
|
@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [1.1.0] - 2025-04-12
|
||||||
|
### Added
|
||||||
|
- Add optional `key` argument to `AbstractCollection::add()` method
|
||||||
|
|
||||||
|
## [1.0.2] - 2025-04-10
|
||||||
|
### Changed
|
||||||
|
- Increased unit test coverage to 100%
|
||||||
|
|
||||||
## [1.0.1] - 2025-04-10
|
## [1.0.1] - 2025-04-10
|
||||||
### Added
|
### Added
|
||||||
- Added description to `composer.json` to provide a description on Packagist.
|
- Added description to `composer.json` to provide a description on Packagist.
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
# Loom Utilities | Collection
|
# Loom Utilities | Collection
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<!-- Version Badge -->
|
||||||
|
<img src="https://img.shields.io/badge/Version-1.1.0-blue" alt="Version 1.1.0">
|
||||||
|
<!-- PHP Coverage Badge -->
|
||||||
|
<img src="https://img.shields.io/badge/PHP%20Coverage-100.00%25-green" alt="PHP Coverage 100.00%">
|
||||||
|
<!-- License Badge -->
|
||||||
|
<img src="https://img.shields.io/badge/License-MIT-34ad9b" alt="License MIT">
|
||||||
|
</div>
|
||||||
|
|
||||||
A Collection library for an object-oriented way to work with arrays.
|
A Collection library for an object-oriented way to work with arrays.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "loomlabs/utility.collection",
|
"name": "loomlabs/utility.collection",
|
||||||
"description": "An object-oriented way to work with arrays",
|
"description": "An object-oriented way to work with arrays.",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Loom\\Utility\\Collection\\": "src/"
|
"Loom\\Utility\\Collection\\": "src/"
|
||||||
|
@ -11,7 +11,10 @@
|
||||||
"Loom\\Utility\\Collection\\Tests\\": "tests/"
|
"Loom\\Utility\\Collection\\Tests\\": "tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "1.0.1",
|
"scripts": {
|
||||||
|
"test": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --testdox --colors=always --coverage-html coverage --coverage-clover coverage/coverage.xml --testdox-html coverage/testdox.html && npx badger --phpunit ./coverage/coverage.xml && npx badger --version ./composer.json && npx badger --license ./composer.json"
|
||||||
|
},
|
||||||
|
"version": "1.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^12.1"
|
"phpunit/phpunit": "^12.1"
|
||||||
|
|
5
package.json
Normal file
5
package.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"devDependencies": {
|
||||||
|
"@dannyxcii/badger": "^0.5.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,12 +12,18 @@ class AbstractCollection implements CollectionInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add(mixed $item): void
|
public function add(mixed $item, string $key = null): void
|
||||||
{
|
{
|
||||||
if (null === $item) {
|
if (null === $item) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($key) {
|
||||||
|
$this->items[$key] = $item;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->items[] = $item;
|
$this->items[] = $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,10 @@ use PHPUnit\Framework\TestCase;
|
||||||
class CollectionTest extends TestCase
|
class CollectionTest extends TestCase
|
||||||
{
|
{
|
||||||
#[DataProvider('addDataProvider')]
|
#[DataProvider('addDataProvider')]
|
||||||
public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult): void
|
public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult, string $key = null): void
|
||||||
{
|
{
|
||||||
$collection = new Collection($startingItems);
|
$collection = new Collection($startingItems);
|
||||||
$collection->add($itemToAdd);
|
$collection->add($itemToAdd, $key);
|
||||||
|
|
||||||
$this->assertEquals($expectedResult, $collection->toArray());
|
$this->assertEquals($expectedResult, $collection->toArray());
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,16 @@ class CollectionTest extends TestCase
|
||||||
$this->assertEquals($expectedResult, $collection->count());
|
$this->assertEquals($expectedResult, $collection->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIteration(): void
|
||||||
|
{
|
||||||
|
$array = ['A', 'B', 'C'];
|
||||||
|
$collection = new Collection($array);
|
||||||
|
|
||||||
|
foreach ($collection as $key => $value) {
|
||||||
|
$this->assertEquals($array[$key], $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function addDataProvider(): array
|
public static function addDataProvider(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -42,17 +52,26 @@ class CollectionTest extends TestCase
|
||||||
'startingItems' => ['A', 'B', 'C'],
|
'startingItems' => ['A', 'B', 'C'],
|
||||||
'itemToAdd' => 'D',
|
'itemToAdd' => 'D',
|
||||||
'expectedResult' => ['A', 'B', 'C', 'D'],
|
'expectedResult' => ['A', 'B', 'C', 'D'],
|
||||||
|
'key' => null,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'startingItems' => [],
|
'startingItems' => [],
|
||||||
'itemToAdd' => 1,
|
'itemToAdd' => 1,
|
||||||
'expectedResult' => [1],
|
'expectedResult' => [1],
|
||||||
|
'key' => null,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'startingItems' => [],
|
'startingItems' => [],
|
||||||
'itemToAdd' => null,
|
'itemToAdd' => null,
|
||||||
'expectedResult' => [],
|
'expectedResult' => [],
|
||||||
|
'key' => null,
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'startingItems' => ['A' => 'A', 'B' => 'B', 'C' => 'C'],
|
||||||
|
'itemToAdd' => 'D',
|
||||||
|
'expectedResult' => ['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D'],
|
||||||
|
'key' => 'D',
|
||||||
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue