Skip to content

Commit

Permalink
Version 4: RC2
Browse files Browse the repository at this point in the history
- isNotEmpty() for Map and List added
- isMap() added
- isList() added
- PHP 8.1 compatibility
- unit tests / cleanup
  • Loading branch information
seboettg committed Aug 7, 2022
1 parent 1a82682 commit 9cfe94c
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/Lists/ArrayListTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ public function isEmpty(): bool
return $this->count() === 0;
}

public function isNotEmpty(): bool
{
return !$this->isEmpty();
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Lists/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ final public static function listFromArray($elements): ListInterface
$list->setArray(array_values($elements));
return $list;
}

public static function isList($value): bool
{
return $value instanceof ListInterface;
}
}

function emptyList(): ListInterface
Expand All @@ -41,3 +46,8 @@ function listFromArray(array $elements): ListInterface
{
return Functions::listFromArray($elements);
}

function isList($value): bool
{
return Functions::isList($value);
}
5 changes: 5 additions & 0 deletions src/Lists/ListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,9 @@ public function searchBy(callable $match);
* @return bool
*/
public function isEmpty(): bool;

/**
* @return bool
*/
public function isNotEmpty(): bool;
}
3 changes: 1 addition & 2 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ class Map implements MapInterface
/**
* @var array $array
*/
protected $array;
protected $array = [];
}

10 changes: 10 additions & 0 deletions src/Map/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public final static function pair($key, $value): Pair
{
return Pair::factory($key, $value);
}

public static function isMap($value): bool
{
return $value instanceof MapInterface;
}
}

/**
Expand Down Expand Up @@ -77,3 +82,8 @@ function pair($key, $value): Pair
{
return Functions::pair($key, $value);
}

function isMap($value): bool
{
return Functions::isMap($value);
}
5 changes: 5 additions & 0 deletions src/Map/MapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function containsValue($value): bool;
*/
public function isEmpty(): bool;

/**
* @return bool
*/
public function isNotEmpty(): bool;

/**
* Returns the value corresponding to the given key, or null if such a key is not present in the map.
*
Expand Down
30 changes: 27 additions & 3 deletions src/Map/MapTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public function isEmpty(): bool
return $this->count() === 0;
}

/**
* @inheritDoc
* @return bool
*/
public function isNotEmpty(): bool
{
return !$this->isEmpty();
}

/**
* @inheritDoc
* @param scalar
Expand Down Expand Up @@ -351,13 +360,28 @@ public function plus(iterable $pairs): MapInterface

/**
* @inheritDoc
* @param callable $action f(entry: Pair<scalar, mixed>) -> mixed|void
* @param callable $action f(entry: Pair<scalar, mixed>) -> mixed|void OR f(key: scalar, value: mixed>) -> mixed|void
* @return void
*/
public function forEach(callable $action): void
{
foreach ($this->array as $key => $value) {
$action(pair($key, $value));
try {
$reflected = new ReflectionFunction($action);
if (count($reflected->getParameters()) === 1) {
assertValidCallable($action, [Pair::class]);
foreach ($this->array as $key => $value) {
$action(pair($key, $value));
}
} else {
if (count($reflected->getParameters()) === 2) {
assertValidCallable($action, ["scalar", "mixed"]);
}
foreach ($this->array as $key => $value) {
$action($key, $value);
}
}
} catch (ReflectionException $ex) {
throw new NotApplicableCallableException("Invalid callable passed.");
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/NativePhp/IteratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Seboettg\Collection\NativePhp;

use ReturnTypeWillChange;

trait IteratorTrait
{
private int $offset = 0;
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function current()
{
return $this->valid() ? $this->array[$this->offset] : false;
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/QueueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function dequeue()
/**
* {@inheritdoc}
*/
public function count()
public function count(): int
{
return count($this->array);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Stack/StackTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait StackTrait
/**
* {@inheritdoc}
*/
public function push($item)
public function push($item): StackInterface
{
$this->array[] = $item;
return $this;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function search($element)
/**
* {@inheritdoc}
*/
public function count()
public function count(): int
{
return count($this->array);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ArrayListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ public function testAssociateWith()

public function testJoinToString()
{
$arrayList = new ArrayList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$arrayList = listOf('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$result = $arrayList->joinToString(", ");
$this->assertEquals("a, b, c, d, e, f, g, h", $result);
}

public function testJoinToStringWithDoubleValues()
{
$arrayList = new ArrayList(1.0, 1.1, 1.2, 1.3);
$arrayList = listOf(1.0, 1.1, 1.2, 1.3);
$result = $arrayList->joinToString("; ");
$this->assertEquals("1.0; 1.1; 1.2; 1.3", $result);
}
Expand Down
30 changes: 25 additions & 5 deletions tests/Lists/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
use PHPUnit\Framework\TestCase;
use Seboettg\Collection\Lists\ListInterface;
use function Seboettg\Collection\Lists\emptyList;
use function Seboettg\Collection\Lists\isList;
use function Seboettg\Collection\Lists\listFromArray;
use function Seboettg\Collection\Lists\listOf;
use function Seboettg\Collection\Map\mapOf;
use function Seboettg\Collection\Map\pair;

class FunctionsTest extends TestCase
{

public function testEmptyListShouldReturnAInstanceOfListInterface()
public function test_emptyList_shouldReturnAInstanceOfListInterface()
{
$this->assertInstanceOf(ListInterface::class, emptyList());
}

public function testListOfShouldReturnAListContainingPassedStrings()
public function test_emptyList_shouldReturnAnEmptyList()
{
$this->assertCount(0, emptyList());
}

public function test_listOf_shouldReturnAListContainingPassedStrings()
{
$list = listOf("A", "B", "C");
$this->assertCount(3, $list);
Expand All @@ -25,7 +33,7 @@ public function testListOfShouldReturnAListContainingPassedStrings()
});
}

public function testListFromArrayShouldReturnAListContainingEachElementOfPassedArray()
public function test_listFromArray_shouldReturnAListContainingEachElementOfPassedArray()
{
$array = ["A", "B", "C"];
$list = listFromArray($array);
Expand All @@ -36,15 +44,15 @@ public function testListFromArrayShouldReturnAListContainingEachElementOfPassedA
}
}

public function testListOfShouldReturnAInstanceOfListInterface()
public function test_listOf_shouldReturnAInstanceOfListInterface()
{
$this->assertInstanceOf(
ListInterface::class,
listOf(1, 2, 3)
);
}

public function testListOfShouldReturnAListContainingPassedItems()
public function test_listOf_shouldReturnAListContainingPassedItems()
{
$integers = [1, 2, 3];
$list = listOf(...$integers);
Expand All @@ -54,4 +62,16 @@ public function testListOfShouldReturnAListContainingPassedItems()
$this->assertContains($item, $integers);
});
}

public function test_isList_shouldReturnTrueIfList()
{
$this->assertTrue(isList(mapOf(pair("a", 0), pair("b", 1))->map(
fn (string $key, int $value) => $value
)));
}

public function test_isList_shouldReturnFalseIfNotList()
{
$this->assertFalse(isList(mapOf(pair("a", 0), pair("b", 1))));
}
}
30 changes: 28 additions & 2 deletions tests/Map/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@

namespace Seboettg\Collection\Test\Map;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Seboettg\Collection\Map\MapInterface;
use function Seboettg\Collection\Lists\listOf;
use function Seboettg\Collection\Map\emptyMap;
use function Seboettg\Collection\Map\isMap;
use function Seboettg\Collection\Map\mapOf;
use function Seboettg\Collection\Map\pair;

class FunctionsTest extends TestCase
{
public function testMapOfShouldCreateMap()
public function test_emptyMap_shouldReturnAnInstanceOfMapInterface()
{
$this->assertInstanceOf(MapInterface::class, emptyMap());
}

public function test_emptyMap_shouldReturnAnEmptyMap()
{
$this->assertCount(0, emptyMap());
}

public function test_mapOf_shouldCreateMap()
{
$map = mapOf(pair("a", "b"), pair("c", "d"));
$this->assertInstanceOf(MapInterface::class, $map);
$this->assertCount(2, $map);
}

public function test_isMap_shouldReturnTrueIfMap()
{
$this->assertTrue(
isMap(listOf(pair("a", 1), pair("b", 2))->toMap())
);
}

public function test_isMap_shouldReturnFalseIfNotAMap()
{
$this->assertFalse(
isMap(listOf(pair("a", 1), pair("b", 2)))
);
}
}
Loading

0 comments on commit 9cfe94c

Please sign in to comment.