- Still WIP on Api's wrapper

This commit is contained in:
Dave Mc Nicoll 2023-10-27 18:33:48 -04:00
parent 1927b43999
commit 8e0dce3e71
6 changed files with 26 additions and 6 deletions

View File

@ -68,7 +68,7 @@ abstract class Sql {
return static::identifier($sql); return static::identifier($sql);
} }
public static function escape($value) public static function escape($value) : mixed
{ {
switch(true) { switch(true) {
case is_object($value): case is_object($value):

View File

@ -346,6 +346,18 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable {
return ( new $className() )->fromArray($dataset); 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 public function jsonSerialize(): mixed
{ {
return $this->toArray(); return $this->toArray();

View File

@ -16,7 +16,7 @@ class OrderBy extends Fragment {
return $this; return $this;
} }
public function add(string $field, ? string $direction = null) : self public function add(object|string $field, ? string $direction = null) : self
{ {
$this->validateFieldType($field); $this->validateFieldType($field);

View File

@ -2,6 +2,8 @@
namespace Ulmus\Query; namespace Ulmus\Query;
use Ulmus\Query;
interface QueryBuilderInterface interface QueryBuilderInterface
{ {
public function push(Fragment $queryFragment) : self; public function push(Fragment $queryFragment) : self;
@ -9,5 +11,5 @@ interface QueryBuilderInterface
public function render(bool $skipToken = false) /* mixed */; public function render(bool $skipToken = false) /* mixed */;
public function reset() : void; public function reset() : void;
public function getFragment(string $class, int $index = 0) : ? Fragment; 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;
} }

View File

@ -278,7 +278,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
return $this; 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) ) { if ( null === $groupBy = $this->getFragment(Query\GroupBy::class) ) {
$groupBy = new Query\GroupBy(); $groupBy = new Query\GroupBy();

View File

@ -532,7 +532,7 @@ class Repository
return $this; 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); $this->queryBuilder->orderBy($field, $direction);
@ -550,7 +550,13 @@ class Repository
public function orders(array $orderList) : self public function orders(array $orderList) : self
{ {
foreach($orderList as $field => $direction) { 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; return $this;