Page MenuHomeDevCentral

D2509.id6329.diff
No OneTemporary

D2509.id6329.diff

diff --git a/src/Collections/BaseCollection.php b/src/Collections/BaseCollection.php
--- a/src/Collections/BaseCollection.php
+++ b/src/Collections/BaseCollection.php
@@ -3,26 +3,26 @@
namespace Keruald\OmniTools\Collections;
-interface BaseCollection {
+abstract class BaseCollection {
///
/// Constructors
///
- public static function from (iterable $items) : static;
+ public static abstract function from (iterable $items) : static;
///
/// Getters
///
- public function toArray () : array;
+ public abstract function toArray () : array;
///
/// Properties
///
- public function count () : int;
+ public abstract function count () : int;
- public function isEmpty () : bool;
+ public abstract function isEmpty () : bool;
}
diff --git a/src/Collections/BaseMap.php b/src/Collections/BaseMap.php
--- a/src/Collections/BaseMap.php
+++ b/src/Collections/BaseMap.php
@@ -2,16 +2,45 @@
namespace Keruald\OmniTools\Collections;
-interface BaseMap {
+use ArrayAccess;
- public function get (mixed $key) : mixed;
+abstract class BaseMap extends BaseCollection implements ArrayAccess {
- public function getOr (mixed $key, mixed $defaultValue): mixed;
+ ///
+ /// Methods to implement
+ ///
- public function set (mixed $key, mixed $value) : static;
+ public abstract function get (mixed $key) : mixed;
- public function has (mixed $key) : bool;
+ public abstract function getOr (mixed $key, mixed $defaultValue): mixed;
- public function contains (mixed $value) : bool;
+ public abstract function set (mixed $key, mixed $value) : static;
+
+ public abstract function unset (mixed $key) : static;
+
+ public abstract function has (mixed $key) : bool;
+
+ public abstract function contains (mixed $value) : bool;
+
+ ///
+ /// ArrayAccess
+ /// Interface to provide accessing objects as arrays.
+ ///
+
+ public function offsetExists (mixed $offset) : bool {
+ return $this->has($offset);
+ }
+
+ public function offsetGet (mixed $offset) : mixed {
+ return $this->get($offset);
+ }
+
+ public function offsetSet (mixed $offset, mixed $value) : void {
+ $this->set($offset, $value);
+ }
+
+ public function offsetUnset (mixed $offset) : void {
+ $this->unset($offset);
+ }
}
diff --git a/src/Collections/HashMap.php b/src/Collections/HashMap.php
--- a/src/Collections/HashMap.php
+++ b/src/Collections/HashMap.php
@@ -14,7 +14,7 @@
* This class can be used as a service container,
* an application context, to store configuration.
*/
-class HashMap implements BaseCollection, BaseMap {
+class HashMap extends BaseMap {
///
/// Properties
@@ -58,11 +58,21 @@
}
public function set (mixed $key, mixed $value) : static {
+ if ($key === null) {
+ throw new InvalidArgumentException("Key can't be null");
+ }
+
$this->map[$key] = $value;
return $this;
}
+ public function unset (mixed $key) : static {
+ unset($this->map[$key]);
+
+ return $this;
+ }
+
public function has (mixed $key) : bool {
return array_key_exists($key, $this->map);
}
diff --git a/src/Collections/Vector.php b/src/Collections/Vector.php
--- a/src/Collections/Vector.php
+++ b/src/Collections/Vector.php
@@ -6,9 +6,10 @@
use Keruald\OmniTools\Reflection\CallableElement;
use Keruald\OmniTools\Strings\Multibyte\OmniString;
+use ArrayAccess;
use InvalidArgumentException;
-class Vector implements BaseCollection {
+class Vector extends BaseCollection implements ArrayAccess {
///
/// Properties
@@ -87,11 +88,16 @@
return $this;
}
+ public function unset (int $key) : static {
+ unset($this->items[$key]);
+
+ return $this;
+ }
+
public function contains (mixed $value) : bool {
return in_array($value, $this->items);
}
-
///
/// Interact with collection content at collection level
///
@@ -110,6 +116,12 @@
return $this;
}
+ public function push (mixed $item) : self {
+ $this->items[] = $item;
+
+ return $this;
+ }
+
/**
* Append all elements of the specified iterable
* to the current vector.
@@ -207,4 +219,48 @@
return new OmniString(implode($delimiter, $this->items));
}
+ ///
+ /// ArrayAccess
+ /// Interface to provide accessing objects as arrays.
+ ///
+
+ private static function ensureOffsetIsInteger (mixed $offset) {
+ if (is_int($offset)) {
+ return;
+ }
+
+ throw new InvalidArgumentException(
+ "Offset of a vector must be an integer."
+ );
+ }
+
+ public function offsetExists (mixed $offset) : bool {
+ self::ensureOffsetIsInteger($offset);
+
+ return array_key_exists($offset, $this->items);
+ }
+
+ public function offsetGet (mixed $offset) : mixed {
+ self::ensureOffsetIsInteger($offset);
+
+ return $this->get($offset);
+ }
+
+ public function offsetSet (mixed $offset, mixed $value) : void {
+ if ($offset === null) {
+ $this->push($value);
+ return;
+ }
+
+ self::ensureOffsetIsInteger($offset);
+
+ $this->set($offset, $value);
+ }
+
+ public function offsetUnset (mixed $offset) : void {
+ self::ensureOffsetIsInteger($offset);
+
+ $this->unset($offset);
+ }
+
}
diff --git a/tests/Collections/HashMapTest.php b/tests/Collections/HashMapTest.php
--- a/tests/Collections/HashMapTest.php
+++ b/tests/Collections/HashMapTest.php
@@ -104,6 +104,16 @@
$this->map->get("The Culture"));
}
+ public function testUnset() {
+ $this->map->unset("The Culture");
+ $this->assertFalse($this->map->contains("Iain Banks"));
+ }
+
+ public function testUnsetNotExistingKey() {
+ $this->map->unset("Not existing");
+ $this->assertEquals(4, $this->map->count());
+ }
+
public function testHas () {
$this->assertTrue($this->map->has("The Culture"));
$this->assertFalse($this->map->has("Not existing key"));
@@ -276,4 +286,36 @@
$this->assertEquals($expected, $actual);
}
+ ///
+ /// ArrayAccess
+ ///
+
+ public function testOffsetExists () : void {
+ $this->assertTrue(isset($this->map["The Culture"]));
+ $this->assertFalse(isset($this->map["Not existing"]));
+ }
+
+ public function testOffsetSetWithoutOffset () : void {
+ $this->expectException(InvalidArgumentException::class);
+ $this->map[] = "Another Author";
+ }
+
+ public function testOffsetSet () : void {
+ $this->map["The Culture"] = "Iain M. Banks";
+ $this->assertEquals("Iain M. Banks", $this->map["The Culture"]);
+ }
+
+ public function testOffsetUnset () : void {
+ unset($this->map["Barrayar"]);
+
+ $expected = [
+ "The Culture" => "Iain Banks",
+ "Radchaai Empire" => "Ann Leckie",
+ // "Barrayar" => "Lois McMaster Bujold", UNSET ENTRY
+ "Hainish" => "Ursula K. Le Guin",
+ ];
+
+ $this->assertEquals($expected, $this->map->toArray());
+ }
+
}
diff --git a/tests/Collections/VectorTest.php b/tests/Collections/VectorTest.php
--- a/tests/Collections/VectorTest.php
+++ b/tests/Collections/VectorTest.php
@@ -82,6 +82,12 @@
$this->assertTrue($this->vector->isEmpty());
}
+ public function testPush () : void {
+ $this->vector->push(6);
+
+ $this->assertEquals([1, 2, 3, 4, 5, 6], $this->vector->toArray());
+ }
+
public function testAppend () : void {
$this->vector->append([6, 7, 8]);
@@ -94,7 +100,6 @@
$this->assertEquals([1, 2, 3, 4, 5, 6, 7 ,8], $this->vector->toArray());
}
-
public function testMap () : void {
$actual = $this->vector
->map(function ($x) { return $x * $x; })
@@ -174,4 +179,43 @@
$this->assertEquals(["a.b.c"], $actual->toArray());
}
+ ///
+ /// ArrayAccess
+ ///
+
+ public function testArrayAccessFailsWithStringKey () : void {
+ $this->expectException(InvalidArgumentException::class);
+
+ $this->vector["foo"];
+ }
+
+ public function testOffsetExists () : void {
+ $this->assertTrue(isset($this->vector[0]));
+ $this->assertFalse(isset($this->vector[8]));
+ }
+
+ public function testOffsetSetWithoutOffset () : void {
+ $this->vector[] = 6;
+ $this->assertEquals(6, $this->vector[5]);
+ }
+
+ public function testOffsetSet () : void {
+ $this->vector[0] = 9;
+ $this->assertEquals(9, $this->vector[0]);
+ }
+
+ public function testOffsetUnset () : void {
+ unset($this->vector[2]);
+
+ $expected = [
+ 0 => 1,
+ 1 => 2,
+ // vector[2] has been unset
+ 3 => 4,
+ 4 => 5,
+ ];
+
+ $this->assertEquals($expected, $this->vector->toArray());
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 15:31 (21 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264666
Default Alt Text
D2509.id6329.diff (8 KB)

Event Timeline