Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Driver/Cassandra/Cassandra.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function query(Query $query): QueryResult

$rawQueryResult = $this->rawQuery($queryString);

$result = new QueryResult((bool)$rawQueryResult);
$result = new QueryResult();
$result->setQueryString($queryString);
foreach ($rawQueryResult as $resultRow) {
/** @var class-string<ModelInterface> $modelClass */
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/Mysqli/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected function connect(): void
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
protected function rawQuery(string $query): mysqli_result|bool
protected function rawQuery(string $query): mysqli_result|true
{
$this->connect();
$result = mysqli_query($this->connection, $query);
Expand Down Expand Up @@ -239,7 +239,7 @@ public function query(Query $query): QueryResult

$rawQueryResult = $this->rawQuery($queryString);

$result = new QueryResult((bool)$rawQueryResult);
$result = new QueryResult();
$result->setQueryString($queryString);
if ($query instanceof UpdateQuery || $query instanceof DeleteQuery) {
$result->setAffectedRows(mysqli_affected_rows($this->connection));
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/OpenSearch/OpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function search(Search $search): SearchResult
throw new SerializeException("Received invalid search response from OpenSearch");
}

$result = new SearchResult(true);
$result = new SearchResult();
if (isset($response->took) && is_int($response->took)) {
$result->setSearchTime($response->took);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Test/TestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function query(Query $query): QueryResult
$entries = $this->groupAndAggregateEntries($clonedEntries, $query->getGroup(), $query->getFields());
}

$queryResult = new QueryResult(true);
$queryResult = new QueryResult();
foreach ($entries as $entry) {
if ($query instanceof SelectQuery) {
/** @var class-string<ModelInterface> $modelClass */
Expand Down
36 changes: 20 additions & 16 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,21 +468,19 @@ public static function query(Query $query): QueryResult
$driver = static::getDriverRegistry()->getDriver($queryableDriver);
$result = $driver->query($query);

if ($result->wasSuccessful() && $query instanceof SelectQuery) {
if ($query instanceof SelectQuery) {
break;
}

if (!$query instanceof SelectQuery) {
$results[] = $result;
}
$results[] = $result;
}

if ($result === false) {
throw new BadMethodCallException("You can't query the model if no queryable driver is available.");
}

if (static::$registry) {
if ($query instanceof SelectQuery && $result->wasSuccessful() && $query->shouldSaveResultsToRegistry()) {
if ($query instanceof SelectQuery && $query->shouldSaveResultsToRegistry()) {
foreach ($result as $model) {
if ($model->getId() === null) {
continue;
Expand All @@ -495,7 +493,7 @@ public static function query(Query $query): QueryResult
if ($query instanceof SelectQuery || count($results) === 1) {
return $result;
} else {
return new QueryResultCollection(true, $results);
return new QueryResultCollection($results);
}
}

Expand All @@ -513,6 +511,7 @@ public static function query(Query $query): QueryResult
* @param array|GroupField[]|string[]|null $group
* @param bool $saveResultsToRegistry Whether results of this query should be saved in the model registry.
* @return QueryResult<static>|static[]
* @throws ModelException
* @noinspection PhpDocSignatureInspection
*/
public static function select(null|WhereCondition|array|WhereGroup $where = null,
Expand All @@ -527,14 +526,12 @@ public static function select(null|WhereCondition|array|WhereGroup $where = null

/**
* @param WhereCondition|array|WhereGroup|null $where
* @return int|null
* @return int
* @throws ModelException
*/
public static function count(null|WhereCondition|array|WhereGroup $where = null): ?int
public static function count(null|WhereCondition|array|WhereGroup $where = null): int
{
$result = static::select(where: $where, fields: [new CountField()]);
if (!$result->wasSuccessful()) {
return null;
}
if (count($result) === 0) {
return 0;
}
Expand Down Expand Up @@ -666,28 +663,35 @@ public function set(array $data): QueryResult
*
* @param Search $search
* @return SearchResult<static>
* @throws ModelException
*/
public static function search(Search $search): SearchResult
{
$search->setModelClassName(static::class);

$result = false;
$lastException = null;
foreach (static::getSearchableDrivers() as $searchableDriver) {
/** @var SearchableInterface $driver */
$driver = static::getDriverRegistry()->getDriver($searchableDriver);
$result = $driver->search($search);

if ($result->wasSuccessful()) {
break;
try {
$result = $driver->search($search);
} catch (ModelException $e) {
$lastException = $e;
}
}

if ($result === false) {
throw new BadMethodCallException("You can't search the model if no searchable driver is available.");
}

if ($lastException !== null) {
/** @var ModelException|null $lastException */
throw $lastException;
}

if (static::$registry) {
if ($result->wasSuccessful() && count($result) > 0) {
if (count($result) > 0) {
foreach ($result as $model) {
if ($model->getId() === null) {
continue;
Expand Down
14 changes: 12 additions & 2 deletions src/ModelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@
*/
class ModelCollection implements Iterator, Countable, ArrayAccess
{
protected array $models = [];
protected array $models;
protected int $iterator = 0;

/**
* ModelCollection constructor.
*
* @param TModel[] $models
*/
public function __construct(array $models = [])
{
$this->models = $models;
}

/**
* Add a model
*
Expand Down Expand Up @@ -131,4 +141,4 @@ public function offsetUnset(mixed $offset): void
unset($this->models[$offset]);
}

}
}
44 changes: 0 additions & 44 deletions src/ModelCollectionResult.php

This file was deleted.

15 changes: 7 additions & 8 deletions src/Query/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Aternos\Model\Query;

use Aternos\Model\ModelCollectionResult;
use Aternos\Model\ModelCollection;
use Aternos\Model\ModelInterface;

/**
* Class QueryResult
*
* @template TModel of ModelInterface
* @extends ModelCollectionResult<TModel>
* @extends ModelCollection<TModel>
* @package Aternos\Model\Query
*/
class QueryResult extends ModelCollectionResult
class QueryResult extends ModelCollection
{
/**
* Raw query string that was executed
Expand All @@ -33,13 +33,12 @@ class QueryResult extends ModelCollectionResult
/**
* QueryResult constructor.
*
* @param bool $success
* @param TModel[] $result
* @param TModel[] $models
* @param string|null $queryString
*/
public function __construct(bool $success, array $result = [], ?string $queryString = null)
public function __construct(array $models = [], ?string $queryString = null)
{
parent::__construct($success, $result);
parent::__construct($models);
$this->queryString = $queryString;
}

Expand Down Expand Up @@ -84,4 +83,4 @@ public function setAffectedRows(?int $affectedRows): static
}
return $this;
}
}
}
22 changes: 4 additions & 18 deletions src/Query/QueryResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,11 @@ class QueryResultCollection extends QueryResult

/**
* QueryResultCollection constructor.
* @param bool $success
* @param array|QueryResult[] $result
* @param array|QueryResult[] $models
* @param string|null $queryString
*/
public function __construct(bool $success, $result = [], ?string $queryString = null)
public function __construct($models = [], ?string $queryString = null)
{
parent::__construct($success, $result, $queryString);
parent::__construct($models, $queryString);
}

/**
* @return bool
*/
public function wasSuccessful(): bool
{
foreach ($this->models as $result) {
if (!$result->wasSuccessful()) {
return false;
}
}
return true;
}
}
}
6 changes: 3 additions & 3 deletions src/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Aternos\Model\Search;

use Aternos\Model\ModelCollectionResult;
use Aternos\Model\ModelCollection;
use Aternos\Model\ModelInterface;

/**
* Class SearchResult
* @template TModel of ModelInterface
* @extends ModelCollectionResult<TModel>
* @extends ModelCollection<TModel>
* @package Aternos\Model\Search
*/
class SearchResult extends ModelCollectionResult
class SearchResult extends ModelCollection
{
protected ?int $searchTime = null;
protected ?int $totalCount = null;
Expand Down
Loading
Loading