From 3a6c6814dffcd747850768a567699884d5dec986 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 5 Oct 2023 16:28:19 +0200 Subject: [PATCH] Change signature of Collection - Removed method query() - Method get replaces query() --- src/Collection.php | 53 ++++++++++++++++++-------- src/Interfaces/CollectionInterface.php | 3 +- tests/CollectionTest.php | 47 +++++++++-------------- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 76e35b13..eca7ce26 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -96,33 +96,54 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable } /** - * Return item with given key + * Return item at given position starting at 0 * * @param integer $key * @return mixed */ - public function get(int $key = 0, $default = null) + public function getAtPosition(int $key = 0, $default = null) { - if (! array_key_exists($key, $this->items)) { + if ($this->count() == 0) { return $default; } - return $this->items[$key]; - } - - public function has(int $key): bool - { - return array_key_exists($key, $this->items); - } - - public function query(string $query, $default = null) - { - $items = $this->getItemsFlat(); - if (!array_key_exists($query, $items)) { + $positions = array_values($this->items); + if (! array_key_exists($key, $positions)) { return $default; } - return $items[$query]; + return $positions[$key]; + } + + public function get(int|string $query, $default = null) + { + if ($this->count() == 0) { + return $default; + } + + if (is_int($query) && array_key_exists($query, $this->items)) { + return $this->items[$query]; + } + + if (is_string($query) && strpos($query, '.') === false) { + return array_key_exists($query, $this->items) ? $this->items[$query] : $default; + } + + $query = explode('.', $query); + + $result = $default; + $items = $this->items; + foreach ($query as $key) { + if (!is_array($items) || !array_key_exists($key, $items)) { + $result = $default; + break; + } + + $result = $items[$key]; + $items = $result; + } + + return $result; } public function map(callable $callback): self diff --git a/src/Interfaces/CollectionInterface.php b/src/Interfaces/CollectionInterface.php index 9cfcbae3..f42769a3 100644 --- a/src/Interfaces/CollectionInterface.php +++ b/src/Interfaces/CollectionInterface.php @@ -7,8 +7,7 @@ use Traversable; interface CollectionInterface extends Traversable { public function push($item): CollectionInterface; - public function get(int $key, $default = null); - public function has(int $key); + public function get(int|string $key, $default = null); public function first(); public function last(); public function count(): int; diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 67488a62..22857526 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -86,24 +86,6 @@ class CollectionTest extends TestCase $this->assertEquals('BAZ', $collection->get(2)); } - public function testGet() - { - $collection = new Collection(['foo', 'bar', 'baz']); - $this->assertEquals('foo', $collection->get(0)); - $this->assertEquals('bar', $collection->get(1)); - $this->assertEquals('baz', $collection->get(2)); - $this->assertNull($collection->get(3)); - $this->assertEquals('test', $collection->get(3, 'test')); - } - - public function testHas(): void - { - $collection = new Collection(['foo', 'bar']); - $this->assertTrue($collection->has(0)); - $this->assertTrue($collection->has(1)); - $this->assertFalse($collection->has(2)); - } - public function testToArray() { $collection = new Collection(['foo', 'bar', 'baz']); @@ -122,11 +104,14 @@ class CollectionTest extends TestCase $this->assertEquals(['foo', 'bar', 'baz'], $mapped->toArray()); } - public function testQuery(): void + public function testGet(): void { $collection = new Collection([ - 'foo' => 'FOO', - 'bar' => 'BAR', + 'first', + 'second', + ['testx' => 'x'], + 'foo' => 'foo_value', + 'bar' => 'bar_value', 'baz' => [ 'test1' => '1', 'test2' => '2', @@ -136,12 +121,18 @@ class CollectionTest extends TestCase ] ]); - $this->assertEquals('FOO', $collection->query('foo')); - $this->assertEquals('BAR', $collection->query('bar')); - $this->assertEquals('1', $collection->query('baz.test1')); - $this->assertEquals('2', $collection->query('baz.test2')); - $this->assertEquals('value', $collection->query('baz.test3.example')); - $this->assertEquals('value', $collection->query('baz.test3.example', 'default')); - $this->assertEquals('default', $collection->query('baz.test3.no', 'default')); + $this->assertEquals('first', $collection->get(0)); + $this->assertEquals('second', $collection->get(1)); + $this->assertEquals('first', $collection->get('0')); + $this->assertEquals('second', $collection->get('1')); + $this->assertEquals('x', $collection->get('2.testx')); + $this->assertEquals('foo_value', $collection->get('foo')); + $this->assertEquals('bar_value', $collection->get('bar')); + $this->assertEquals('1', $collection->get('baz.test1')); + $this->assertEquals('2', $collection->get('baz.test2')); + $this->assertEquals('value', $collection->get('baz.test3.example')); + $this->assertEquals('value', $collection->get('baz.test3.example', 'default')); + $this->assertEquals('default', $collection->get('baz.test3.no', 'default')); + $this->assertEquals(['example' => 'value'], $collection->get('baz.test3')); } }