1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-03 10:53:01 +02:00

Change signature of Collection

- Removed method query()
- Method get replaces query()
This commit is contained in:
Oliver Vogel
2023-10-05 16:28:19 +02:00
parent 4fbc7f5840
commit 3a6c6814df
3 changed files with 57 additions and 46 deletions

View File

@@ -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 * @param integer $key
* @return mixed * @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 $default;
} }
return $this->items[$key]; $positions = array_values($this->items);
} if (! array_key_exists($key, $positions)) {
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)) {
return $default; 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 public function map(callable $callback): self

View File

@@ -7,8 +7,7 @@ use Traversable;
interface CollectionInterface extends Traversable interface CollectionInterface extends Traversable
{ {
public function push($item): CollectionInterface; public function push($item): CollectionInterface;
public function get(int $key, $default = null); public function get(int|string $key, $default = null);
public function has(int $key);
public function first(); public function first();
public function last(); public function last();
public function count(): int; public function count(): int;

View File

@@ -86,24 +86,6 @@ class CollectionTest extends TestCase
$this->assertEquals('BAZ', $collection->get(2)); $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() public function testToArray()
{ {
$collection = new Collection(['foo', 'bar', 'baz']); $collection = new Collection(['foo', 'bar', 'baz']);
@@ -122,11 +104,14 @@ class CollectionTest extends TestCase
$this->assertEquals(['foo', 'bar', 'baz'], $mapped->toArray()); $this->assertEquals(['foo', 'bar', 'baz'], $mapped->toArray());
} }
public function testQuery(): void public function testGet(): void
{ {
$collection = new Collection([ $collection = new Collection([
'foo' => 'FOO', 'first',
'bar' => 'BAR', 'second',
['testx' => 'x'],
'foo' => 'foo_value',
'bar' => 'bar_value',
'baz' => [ 'baz' => [
'test1' => '1', 'test1' => '1',
'test2' => '2', 'test2' => '2',
@@ -136,12 +121,18 @@ class CollectionTest extends TestCase
] ]
]); ]);
$this->assertEquals('FOO', $collection->query('foo')); $this->assertEquals('first', $collection->get(0));
$this->assertEquals('BAR', $collection->query('bar')); $this->assertEquals('second', $collection->get(1));
$this->assertEquals('1', $collection->query('baz.test1')); $this->assertEquals('first', $collection->get('0'));
$this->assertEquals('2', $collection->query('baz.test2')); $this->assertEquals('second', $collection->get('1'));
$this->assertEquals('value', $collection->query('baz.test3.example')); $this->assertEquals('x', $collection->get('2.testx'));
$this->assertEquals('value', $collection->query('baz.test3.example', 'default')); $this->assertEquals('foo_value', $collection->get('foo'));
$this->assertEquals('default', $collection->query('baz.test3.no', 'default')); $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'));
} }
} }