Skip to content

Commit

Permalink
Merge pull request #554 from radimvaculik/feature/fetchChecked
Browse files Browse the repository at this point in the history
Added `ICollection::fetchChecked()`
  • Loading branch information
hrach committed Jan 11, 2022
2 parents 60ba3b9 + bb36ed0 commit b46c756
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 29 deletions.
29 changes: 15 additions & 14 deletions docs/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ In Orm, we use coding standard which assumes that

Collection itself is **immutable**, all methods that modify the collection return a new `ICollection` instance. Collection provides following methods:

| Function | Description |
| --- | --- |
| `getBy(array $conds): ?IEntity` | applies an additional filtering and returns the first result's entity or a `null`
| `getByChecked(array $conds): IEntity` | applies an additional filtering and returns the first result's entity or a throws `NoResultException`
| `getById($primaryValue): ?IEntity` | applies filtering by `id` property and returns the first result's entity or a `null`
| `getByIdChecked($primaryValue): IEntity` | applies filtering by `id` property and returns the first result's entity or a throws `NoResultException`
| `findBy(array $conds): ICollection` | applies an additional filtering
| `orderBy($property, $direction): ICollection` | applies an additional ordering
| Function | Description |
|--------------------------------------------------------| --- |
| `getBy(array $conds): ?IEntity` | applies an additional filtering and returns the first result's entity or a `null`
| `getByChecked(array $conds): IEntity` | applies an additional filtering and returns the first result's entity or a throws `NoResultException`
| `getById($primaryValue): ?IEntity` | applies filtering by `id` property and returns the first result's entity or a `null`
| `getByIdChecked($primaryValue): IEntity` | applies filtering by `id` property and returns the first result's entity or a throws `NoResultException`
| `findBy(array $conds): ICollection` | applies an additional filtering
| `orderBy($property, $direction): ICollection` | applies an additional ordering
| `orderBy($propertyExpression, $direction): ICollection` | applies an additional ordering using collection function
| `orderBy(array $properties): ICollection` | applies an additional multiple ordering
| `resetOrderBy(): ICollection` | removes all defined orderings
| `limitBy($limit, $offset): ICollection` | limits the collection and sets the starting offset
| `fetch(): ?IEntity` | returns the next unprocessed result's entity, repeated calls iterate over the whole result-set
| `fetchAll(): IEntity[]` | returns the all result's entities as an array
| `fetchPairs($key, $value): array` | process the whole result and returns it as an associative array
| `orderBy(array $properties): ICollection` | applies an additional multiple ordering
| `resetOrderBy(): ICollection` | removes all defined orderings
| `limitBy($limit, $offset): ICollection` | limits the collection and sets the starting offset
| `fetch(): ?IEntity` | returns the next unprocessed result's entity, repeated calls iterate over the whole result-set
| `fetchChecked(): IEntity` | returns the next unprocessed result's entity, repeated calls iterate over the whole result-set or a throws `NoResultException`
| `fetchAll(): IEntity[]` | returns the all result's entities as an array
| `fetchPairs($key, $value): array` | process the whole result and returns it as an associative array

#### Single result fetching

Expand Down
16 changes: 11 additions & 5 deletions src/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ public function getBy(array $conds): ?IEntity
/** {@inheritDoc} */
public function getByChecked(array $conds): IEntity
{
$entity = $this->getBy($conds);
if ($entity === null) {
throw new NoResultException();
}
return $entity;
return $this->findBy($conds)->fetchChecked();
}


Expand Down Expand Up @@ -181,6 +177,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
16 changes: 11 additions & 5 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ public function getBy(array $conds): ?IEntity
/** {@inheritDoc} */
public function getByChecked(array $conds): IEntity
{
$entity = $this->getBy($conds);
if ($entity === null) {
throw new NoResultException();
}
return $entity;
return $this->findBy($conds)->fetchChecked();
}


Expand Down Expand Up @@ -192,6 +188,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/EmptyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public function fetch(): ?IEntity
}


public function fetchChecked(): IEntity
{
throw new NoResultException();
}


public function fetchAll()
{
return [];
Expand Down
16 changes: 11 additions & 5 deletions src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ public function getBy(array $conds): ?IEntity

public function getByChecked(array $conds): IEntity
{
$entity = $this->getBy($conds);
if ($entity === null) {
throw new NoResultException();
}
return $entity;
return $this->findBy($conds)->fetchChecked();
}


Expand Down Expand Up @@ -157,6 +153,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
8 changes: 8 additions & 0 deletions src/Collection/ICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ 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.
* @return IEntity[]
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/integration/Collection/collection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ class CollectionTest extends DataTestCase
Assert::type(EmptyCollection::class, $c8);
Assert::same($c8->count(), $c7->countStored());
}


public function testFetchChecked(): void
{
Assert::throws(function (): void {
$this->orm->books->findBy(['id' => 923])->fetchChecked();
}, NoResultException::class);

$book = $this->orm->books->findAll()->fetchChecked();
Assert::type(Book::class, $book);
}
}


Expand Down

0 comments on commit b46c756

Please sign in to comment.