Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ICollection::fetchChecked() #554

Merged
merged 1 commit into from
Jan 11, 2022
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
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;


/**
radimvaculik marked this conversation as resolved.
Show resolved Hide resolved
* Fetches the first row., throw if none found.
radimvaculik marked this conversation as resolved.
Show resolved Hide resolved
* @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