mirror of
https://github.com/Intervention/image.git
synced 2025-09-02 18:32:56 +02:00
Change signature of Collection
- Removed method query() - Method get replaces query()
This commit is contained in:
@@ -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
|
||||
|
@@ -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;
|
||||
|
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user