Skip to content

Commit

Permalink
Changed Collection to allow for reset
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Mar 17, 2024
1 parent 1d3b224 commit 42a3d84
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 2 additions & 0 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
* # HTTP caching
* @method void etag(string $id, ('strong'|'weak') $type = 'strong') Handles ETag HTTP caching.
* @method void lastModified(int $time) Handles last modified HTTP caching.
*
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
*/
class Engine
{
Expand Down
5 changes: 2 additions & 3 deletions flight/database/PdoWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public function runQuery(string $sql, array $params = []): PDOStatement
*/
public function fetchField(string $sql, array $params = [])
{
$collection_data = $this->fetchRow($sql, $params);
$array_data = $collection_data->getData();
return reset($array_data);
$result = $this->fetchRow($sql, $params);
return reset($result);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions flight/util/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
*/
class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
{
/**
* This is to allow for reset() to work properly.
*
* WARNING! This MUST be the first variable in this class!!!
*
* PHPStan is ignoring this because we don't need actually to read the property
*
* @var mixed
* @phpstan-ignore-next-line
*/
private $first_property = null;

/**
* Collection data.
*
Expand All @@ -35,6 +47,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
public function __construct(array $data = [])
{
$this->data = $data;
$this->handleReset();
}

/**
Expand All @@ -55,6 +68,7 @@ public function __get(string $key)
public function __set(string $key, $value): void
{
$this->data[$key] = $value;
$this->handleReset();
}

/**
Expand All @@ -71,6 +85,7 @@ public function __isset(string $key): bool
public function __unset(string $key): void
{
unset($this->data[$key]);
$this->handleReset();
}

/**
Expand Down Expand Up @@ -100,6 +115,7 @@ public function offsetSet($offset, $value): void
} else {
$this->data[$offset] = $value;
}
$this->handleReset();
}

/**
Expand All @@ -120,6 +136,17 @@ public function offsetExists($offset): bool
public function offsetUnset($offset): void
{
unset($this->data[$offset]);
$this->handleReset();
}

/**
* This is to allow for reset() of a Collection to work properly.
*
* @return void
*/
public function handleReset()
{
$this->first_property = reset($this->data);
}

/**
Expand Down Expand Up @@ -207,6 +234,7 @@ public function getData(): array
public function setData(array $data): void
{
$this->data = $data;
$this->handleReset();
}

#[\ReturnTypeWillChange]
Expand Down
23 changes: 23 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,27 @@ public function testClear()
$this->collection->clear();
$this->assertEquals(0, $this->collection->count());
}

public function testResetByProperty()
{
$this->collection->a = 11;
$this->collection->b = 22;
$result = reset($this->collection);
$this->assertEquals(11, $result);
}

public function testResetBySetData()
{
$this->collection->setData(['a' => 11, 'b' => 22]);
$result = reset($this->collection);
$this->assertEquals(11, $result);
}

public function testResetByArraySet()
{
$this->collection['a'] = 11;
$this->collection['b'] = 22;
$result = reset($this->collection);
$this->assertEquals(11, $result);
}
}

0 comments on commit 42a3d84

Please sign in to comment.