1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 04:01:30 +02:00

Add Collection::get() method

This commit is contained in:
Oliver Vogel
2022-06-18 16:02:11 +02:00
parent ed12d374ff
commit a7eb80f4f1
3 changed files with 14 additions and 0 deletions

View File

@@ -109,6 +109,11 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable
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();

View File

@@ -6,6 +6,7 @@ interface CollectionInterface
{
public function push($item): CollectionInterface;
public function get(int $key);
public function has(int $key);
public function first();
public function last();
public function count(): int;

View File

@@ -86,6 +86,14 @@ class CollectionTest extends TestCase
$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']);