diff --git a/src/Common/Sql.php b/src/Common/Sql.php index 7ee3a98..58f4410 100644 --- a/src/Common/Sql.php +++ b/src/Common/Sql.php @@ -68,7 +68,7 @@ abstract class Sql { return static::identifier($sql); } - public static function escape($value) + public static function escape($value) : mixed { switch(true) { case is_object($value): diff --git a/src/EntityCollection.php b/src/EntityCollection.php index b664fee..fb412cc 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -346,6 +346,18 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable { return ( new $className() )->fromArray($dataset); } + public function arrayToEntities(array $list, ? string /*stringable*/ $entityClass = null) : self + { + $collection = new static(); + $collection->entityClass = $entityClass ?? $this->entityClass; + + foreach($list as $dataset) { + $collection->append($this->arrayToEntity($dataset, $entityClass)); + } + + return $collection; + } + public function jsonSerialize(): mixed { return $this->toArray(); diff --git a/src/Query/OrderBy.php b/src/Query/OrderBy.php index bb971ed..780bcd3 100644 --- a/src/Query/OrderBy.php +++ b/src/Query/OrderBy.php @@ -16,7 +16,7 @@ class OrderBy extends Fragment { return $this; } - public function add(string $field, ? string $direction = null) : self + public function add(object|string $field, ? string $direction = null) : self { $this->validateFieldType($field); diff --git a/src/Query/QueryBuilderInterface.php b/src/Query/QueryBuilderInterface.php index 9f40562..6ac6e7e 100644 --- a/src/Query/QueryBuilderInterface.php +++ b/src/Query/QueryBuilderInterface.php @@ -2,6 +2,8 @@ namespace Ulmus\Query; +use Ulmus\Query; + interface QueryBuilderInterface { public function push(Fragment $queryFragment) : self; @@ -9,5 +11,5 @@ interface QueryBuilderInterface public function render(bool $skipToken = false) /* mixed */; public function reset() : void; public function getFragment(string $class, int $index = 0) : ? Fragment; - public function removeFragment(Fragment $fragment) : void; + public function removeFragment(Query\Fragment|array|\Stringable|string $fragment) : void; } \ No newline at end of file diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 261bfcf..1f43f81 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -278,7 +278,7 @@ class QueryBuilder implements Query\QueryBuilderInterface return $this; } - public function groupBy(string|\Stringable $field, ? string $direction = null) : self + public function groupBy(string|object $field, ? string $direction = null) : self { if ( null === $groupBy = $this->getFragment(Query\GroupBy::class) ) { $groupBy = new Query\GroupBy(); diff --git a/src/Repository.php b/src/Repository.php index e4ce0e4..c784efd 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -532,7 +532,7 @@ class Repository return $this; } - public function orderBy(string|\Stringable $field, ? string $direction = null) : self + public function orderBy(string|object $field, ? string $direction = null) : self { $this->queryBuilder->orderBy($field, $direction); @@ -550,7 +550,13 @@ class Repository public function orders(array $orderList) : self { foreach($orderList as $field => $direction) { - $this->orderBy($field, $direction); + if (is_numeric($field)) { + $this->orderBy($direction); + } + else { + # Associative array with direction + $this->orderBy($field, $direction); + } } return $this;