- Some work done on correcting LDAP package compatibility
This commit is contained in:
parent
71de0a8ac1
commit
e3d0e45a4e
|
@ -5,9 +5,4 @@ namespace Ulmus\Entity\Field;
|
||||||
class Date extends Datetime {
|
class Date extends Datetime {
|
||||||
|
|
||||||
public string $format = "Y-m-d";
|
public string $format = "Y-m-d";
|
||||||
|
|
||||||
public function formatLocale(string $format) : string
|
|
||||||
{
|
|
||||||
return strftime($format, $this->getTimestamp());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,15 @@ class EntityCollection extends \ArrayObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function filtersOne(Callable $callback) : ? object
|
||||||
|
{
|
||||||
|
foreach($this->filters($callback, true) as $item) {
|
||||||
|
return $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function iterate(Callable $callback) : self
|
public function iterate(Callable $callback) : self
|
||||||
{
|
{
|
||||||
foreach($this as $item) {
|
foreach($this as $item) {
|
||||||
|
@ -290,6 +299,20 @@ class EntityCollection extends \ArrayObject {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function batch(int $size, $fill = null, $preserveKeys = true) : array
|
||||||
|
{
|
||||||
|
$batch = [];
|
||||||
|
$split = ceil( $this->count() / $size );
|
||||||
|
|
||||||
|
for($i = 0; $i < $split; $i++) {
|
||||||
|
$slice = array_slice($this->getArrayCopy(), $i * $size, $size, $preserveKeys);
|
||||||
|
$pad = ( count($slice) !== $size ) && ( $fill !== null );
|
||||||
|
$batch[] = $pad ? array_pad($slice, $size, $fill) : $slice;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $batch;
|
||||||
|
}
|
||||||
|
|
||||||
public function randomize() : self
|
public function randomize() : self
|
||||||
{
|
{
|
||||||
$arr = $this->getArrayCopy();
|
$arr = $this->getArrayCopy();
|
||||||
|
@ -313,6 +336,16 @@ class EntityCollection extends \ArrayObject {
|
||||||
return $this->reverse();
|
return $this->reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function sortField(? string $field = null) : self
|
||||||
|
{
|
||||||
|
return $this->sort(fn($e1, $e2) => $field ? $e1->$field <=> $e2->$field : $e1 <=> $e2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rsortField(string $field) : self
|
||||||
|
{
|
||||||
|
return $this->sort(fn($e1, $e2) => $field ? $e2->$field <=> $e1->$field : $e2 <=> $e1);
|
||||||
|
}
|
||||||
|
|
||||||
public function reverse() : self
|
public function reverse() : self
|
||||||
{
|
{
|
||||||
return $this->replaceWith(array_reverse($this->getArrayCopy()));;
|
return $this->replaceWith(array_reverse($this->getArrayCopy()));;
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Exception;
|
||||||
|
|
||||||
|
class InvalidFieldType extends \Exception {}
|
|
@ -28,4 +28,11 @@ abstract class Fragment {
|
||||||
|
|
||||||
return implode(", ", $list);
|
return implode(", ", $list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function validateFieldType($field) : void
|
||||||
|
{
|
||||||
|
if ( is_numeric($field) ) {
|
||||||
|
throw new \Ulmus\Exception\InvalidFieldType(sprintf("Validation of field `%s` failed from query `%s`", $field, get_class($this)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ class GroupBy extends Fragment {
|
||||||
|
|
||||||
public function add(string $field) : self
|
public function add(string $field) : self
|
||||||
{
|
{
|
||||||
|
$this->validateFieldType($field);
|
||||||
|
|
||||||
$this->groupBy[] = $field;
|
$this->groupBy[] = $field;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
@ -29,6 +29,9 @@ class Having extends Fragment {
|
||||||
|
|
||||||
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
|
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
|
||||||
{
|
{
|
||||||
|
$this->validateFieldType($field);
|
||||||
|
# $this->validateValueType($value);
|
||||||
|
|
||||||
$this->conditionList[] = [
|
$this->conditionList[] = [
|
||||||
$field,
|
$field,
|
||||||
$value,
|
$value,
|
||||||
|
@ -102,7 +105,8 @@ class Having extends Fragment {
|
||||||
return (in_array($this->operator, [ '!=', '<>' ]) ? Where::CONDITION_NOT . " " : "") . Where::COMPARISON_IN;
|
return (in_array($this->operator, [ '!=', '<>' ]) ? Where::CONDITION_NOT . " " : "") . Where::COMPARISON_IN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->operator;
|
# whitelisting operators
|
||||||
|
return in_array(strtoupper($this->operator), [ '=', '!=', '<>', 'LIKE' ]) ? $this->operator : "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function value()
|
protected function value()
|
||||||
|
@ -124,6 +128,7 @@ class Having extends Fragment {
|
||||||
{
|
{
|
||||||
if ( $value === null ) {
|
if ( $value === null ) {
|
||||||
$this->operator = in_array($this->operator, [ '!=', '<>' ]) ? Where::COMPARISON_IS . " " . Where::CONDITION_NOT : Where::COMPARISON_IS;
|
$this->operator = in_array($this->operator, [ '!=', '<>' ]) ? Where::COMPARISON_IS . " " . Where::CONDITION_NOT : Where::COMPARISON_IS;
|
||||||
|
|
||||||
return Where::COMPARISON_NULL;
|
return Where::COMPARISON_NULL;
|
||||||
}
|
}
|
||||||
elseif ( is_object($value) && ( $value instanceof EntityField ) ) {
|
elseif ( is_object($value) && ( $value instanceof EntityField ) ) {
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Query;
|
|
||||||
|
|
||||||
class Like extends Fragment {
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,6 +18,8 @@ class OrderBy extends Fragment {
|
||||||
|
|
||||||
public function add(string $field, ? string $direction = null) : self
|
public function add(string $field, ? string $direction = null) : self
|
||||||
{
|
{
|
||||||
|
$this->validateFieldType($field);
|
||||||
|
|
||||||
$this->orderBy[] = [ $field, $direction ];
|
$this->orderBy[] = [ $field, $direction ];
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
@ -37,6 +37,9 @@ class Where extends Fragment {
|
||||||
|
|
||||||
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
|
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
|
||||||
{
|
{
|
||||||
|
$this->validateFieldType($field);
|
||||||
|
# $this->validateValueType($value);
|
||||||
|
|
||||||
$this->conditionList[] = [
|
$this->conditionList[] = [
|
||||||
$field,
|
$field,
|
||||||
$value,
|
$value,
|
||||||
|
@ -110,7 +113,8 @@ class Where extends Fragment {
|
||||||
return (in_array($this->operator, [ '!=', '<>' ]) ? Where::CONDITION_NOT . " " : "") . Where::COMPARISON_IN;
|
return (in_array($this->operator, [ '!=', '<>' ]) ? Where::CONDITION_NOT . " " : "") . Where::COMPARISON_IN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->operator;
|
# whitelisting operators
|
||||||
|
return in_array(strtoupper($this->operator), [ '=', '!=', '<>', 'LIKE' ]) ? $this->operator : "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function value()
|
protected function value()
|
||||||
|
|
|
@ -48,7 +48,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function select($field) : self
|
public function select($field, bool $distinct = false) : self
|
||||||
{
|
{
|
||||||
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {
|
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {
|
||||||
$select->add($field);
|
$select->add($field);
|
||||||
|
@ -59,6 +59,8 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
$this->push($select);
|
$this->push($select);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$select->distinct = $distinct;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -401,7 +403,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
public function getFragment(string $class, int $index = 0) : ? Query\Fragment
|
public function getFragment(string $class, int $index = 0) : ? Query\Fragment
|
||||||
{
|
{
|
||||||
foreach($this->queryStack as $item) {
|
foreach($this->queryStack as $item) {
|
||||||
if ( get_class($item) === $class ) {
|
if ( is_a($item, $class, true) ) {
|
||||||
if ( $index-- === 0 ) {
|
if ( $index-- === 0 ) {
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
|
@ -420,6 +422,11 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getFragments() : array
|
||||||
|
{
|
||||||
|
return $this->queryStack;
|
||||||
|
}
|
||||||
|
|
||||||
public function __toString() : string
|
public function __toString() : string
|
||||||
{
|
{
|
||||||
return $this->render();
|
return $this->render();
|
||||||
|
|
|
@ -285,6 +285,8 @@ class Repository
|
||||||
$this->select("$alias.$key as {$prependField}{$field['name']}");
|
$this->select("$alias.$key as {$prependField}{$field['name']}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function selectJsonEntity(string $entity, string $alias, bool $skipFrom = false) : self
|
public function selectJsonEntity(string $entity, string $alias, bool $skipFrom = false) : self
|
||||||
|
@ -307,9 +309,9 @@ class Repository
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function select(/*array|Stringable*/ $fields) : self
|
public function select(/*array|Stringable*/ $fields, bool $distinct = false) : self
|
||||||
{
|
{
|
||||||
$this->queryBuilder->select($fields);
|
$this->queryBuilder->select($fields, $distinct);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -616,7 +618,7 @@ class Repository
|
||||||
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self
|
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self
|
||||||
{
|
{
|
||||||
if ($count) {
|
if ($count) {
|
||||||
$searchRequest->count = $searchRequest->filter(new Repository\ServerRequestCountRepository($this->entityClass, $this->alias, $this->adapter))
|
$searchRequest->count = $searchRequest->filter($this->serverRequestCountRepository())
|
||||||
->wheres($searchRequest->wheres(), Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
|
->wheres($searchRequest->wheres(), Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
|
||||||
->likes($searchRequest->likes(), Query\Where::CONDITION_OR)
|
->likes($searchRequest->likes(), Query\Where::CONDITION_OR)
|
||||||
->groups($searchRequest->groups())
|
->groups($searchRequest->groups())
|
||||||
|
@ -632,6 +634,11 @@ class Repository
|
||||||
->limit($searchRequest->limit());
|
->limit($searchRequest->limit());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function serverRequestCountRepository() : Repository
|
||||||
|
{
|
||||||
|
return new Repository\ServerRequestCountRepository($this->entityClass, $this->alias, $this->adapter);
|
||||||
|
}
|
||||||
|
|
||||||
public function collectionFromQuery(? string $entityClass = null) : EntityCollection
|
public function collectionFromQuery(? string $entityClass = null) : EntityCollection
|
||||||
{
|
{
|
||||||
$class = $entityClass ?: $this->entityClass;
|
$class = $entityClass ?: $this->entityClass;
|
||||||
|
@ -646,7 +653,7 @@ class Repository
|
||||||
$entityCollection->append( ( new $class() )->resetVirtualProperties()->entityFillFromDataset($entityData) );
|
$entityCollection->append( ( new $class() )->resetVirtualProperties()->entityFillFromDataset($entityData) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->eventExecute(Event\RepositoryCollectionFromQueryInterface::class, $entityCollection);
|
$this->eventExecute(Event\Repository\CollectionFromQueryInterface::class, $entityCollection);
|
||||||
|
|
||||||
return $entityCollection;
|
return $entityCollection;
|
||||||
}
|
}
|
||||||
|
@ -793,6 +800,11 @@ class Repository
|
||||||
return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_SCHEMA);
|
return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_SCHEMA);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function hasFilters() : bool
|
||||||
|
{
|
||||||
|
return isset($this->queryBuilder->where);
|
||||||
|
}
|
||||||
|
|
||||||
protected function matchEntity(object $entity) {
|
protected function matchEntity(object $entity) {
|
||||||
return get_class($entity) === $this->entityClass;
|
return get_class($entity) === $this->entityClass;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ class MssqlRepository extends Repository {
|
||||||
$this->queryBuilder->push($mssqlOffset);
|
$this->queryBuilder->push($mssqlOffset);
|
||||||
}
|
}
|
||||||
elseif ( null !== $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
|
elseif ( null !== $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
|
||||||
|
|
||||||
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
|
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
|
||||||
$select->top = $limit->limit;
|
$select->top = $limit->limit;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\SearchRequest;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
trait SearchRequestConstructorTrait
|
||||||
|
{
|
||||||
|
public function __construct(array $parameters = [])
|
||||||
|
{
|
||||||
|
$this->fromArray($parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromArray(array $data): self
|
||||||
|
{
|
||||||
|
foreach(array_filter($data, fn($e) => $e !== "") as $key => $value) {
|
||||||
|
$this->$key = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue