Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Phaseolies/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __call($method, $parameters)
*/
public function __isset($name)
{
return isset($this->data[$name]);
return array_key_exists($name, $this->data);
}

/**
Expand All @@ -110,7 +110,7 @@ public function __isset($name)
*/
public function offsetExists($offset): bool
{
return isset($this->data[$offset]);
return array_key_exists($offset, $this->data);
}

/**
Expand Down Expand Up @@ -194,7 +194,11 @@ public function all(): array
*/
public function first(): mixed
{
return $this->data[0] ?? null;
foreach ($this->data as $item) {
return $item;
}

return null;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public function testMagicGetAndIsset()
$this->assertFalse(isset($collection->baz));
}

public function testNullValuesAreStillConsideredPresent()
{
$collection = new Collection(Model::class, ["foo" => null]);

$this->assertArrayHasKey("foo", $collection->all());
$this->assertTrue(isset($collection["foo"]));
$this->assertTrue(isset($collection->foo));
$this->assertNull($collection["foo"]);
$this->assertNull($collection->foo);
}

public function testGetIterator()
{
$items = [1, 2, 3];
Expand Down Expand Up @@ -97,6 +108,9 @@ public function testFirst()
$collection = new Collection(Model::class, [1, 2, 3]);
$this->assertEquals(1, $collection->first());

$associativeCollection = new Collection(Model::class, ['a' => 10, 'b' => 20]);
$this->assertEquals(10, $associativeCollection->first());

$emptyCollection = new Collection(Model::class, []);
$this->assertNull($emptyCollection->first());
}
Expand Down
Loading