Skip to content

Commit

Permalink
Added ICollection::fetchChecked() [closes #534]
Browse files Browse the repository at this point in the history
  • Loading branch information
radimvaculik committed Jan 11, 2022
1 parent 60ba3b9 commit 29b4dec
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ public function fetch(): ?IEntity
}


public function fetchChecked(): IEntity
{
$entity = $this->fetch();
if ($entity === null) {
throw new NoResultException();
}
return $entity;
}


public function fetchAll()
{
return iterator_to_array($this->getIterator());
Expand Down
10 changes: 10 additions & 0 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public function fetch(): ?IEntity
}


public function fetchChecked(): IEntity
{
$entity = $this->fetch();
if ($entity === null) {
throw new NoResultException();
}
return $entity;
}


public function fetchAll()
{
return iterator_to_array($this->getIterator());
Expand Down
10 changes: 10 additions & 0 deletions src/Collection/EmptyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function fetch(): ?IEntity
}


public function fetchChecked(): IEntity
{
$entity = $this->fetch();
if ($entity === null) {
throw new NoResultException();
}
return $entity;
}


public function fetchAll()
{
return [];
Expand Down
10 changes: 10 additions & 0 deletions src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ public function fetch(): ?IEntity
}


public function fetchChecked(): IEntity
{
$entity = $this->fetch();
if ($entity === null) {
throw new NoResultException();
}
return $entity;
}


public function fetchAll()
{
return iterator_to_array($this->getIterator());
Expand Down
6 changes: 6 additions & 0 deletions src/Collection/ICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public function limitBy(int $limit, int $offset = null): ICollection;
*/
public function fetch(): ?IEntity;

/**
* Fetches the first row., throw if none found.
* @throws NoResultException
* @phpstan-return E
*/
public function fetchChecked(): IEntity;

/**
* Fetches all records.
Expand Down

0 comments on commit 29b4dec

Please sign in to comment.