- Some work done on correcting LDAP package compatibility

This commit is contained in:
Dave M. 2021-03-09 15:25:46 +00:00
parent 71de0a8ac1
commit e3d0e45a4e
13 changed files with 110 additions and 22 deletions

View File

@ -5,9 +5,4 @@ namespace Ulmus\Entity\Field;
class Date extends Datetime {
public string $format = "Y-m-d";
public function formatLocale(string $format) : string
{
return strftime($format, $this->getTimestamp());
}
}

View File

@ -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
{
foreach($this as $item) {
@ -290,6 +299,20 @@ class EntityCollection extends \ArrayObject {
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
{
$arr = $this->getArrayCopy();
@ -313,6 +336,16 @@ class EntityCollection extends \ArrayObject {
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
{
return $this->replaceWith(array_reverse($this->getArrayCopy()));;

View File

@ -0,0 +1,5 @@
<?php
namespace Ulmus\Exception;
class InvalidFieldType extends \Exception {}

View File

@ -28,4 +28,11 @@ abstract class Fragment {
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)));
}
}
}

View File

@ -18,6 +18,8 @@ class GroupBy extends Fragment {
public function add(string $field) : self
{
$this->validateFieldType($field);
$this->groupBy[] = $field;
return $this;

View File

@ -29,6 +29,9 @@ class Having extends Fragment {
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
{
$this->validateFieldType($field);
# $this->validateValueType($value);
$this->conditionList[] = [
$field,
$value,
@ -102,7 +105,8 @@ class Having extends Fragment {
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()
@ -124,6 +128,7 @@ class Having extends Fragment {
{
if ( $value === null ) {
$this->operator = in_array($this->operator, [ '!=', '<>' ]) ? Where::COMPARISON_IS . " " . Where::CONDITION_NOT : Where::COMPARISON_IS;
return Where::COMPARISON_NULL;
}
elseif ( is_object($value) && ( $value instanceof EntityField ) ) {

View File

@ -1,7 +0,0 @@
<?php
namespace Ulmus\Query;
class Like extends Fragment {
}

View File

@ -18,6 +18,8 @@ class OrderBy extends Fragment {
public function add(string $field, ? string $direction = null) : self
{
$this->validateFieldType($field);
$this->orderBy[] = [ $field, $direction ];
return $this;

View File

@ -37,6 +37,9 @@ class Where extends Fragment {
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
{
$this->validateFieldType($field);
# $this->validateValueType($value);
$this->conditionList[] = [
$field,
$value,
@ -110,7 +113,8 @@ class Where extends Fragment {
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()

View File

@ -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) ) ) {
$select->add($field);
@ -59,6 +59,8 @@ class QueryBuilder implements Query\QueryBuilderInterface
$this->push($select);
}
$select->distinct = $distinct;
return $this;
}
@ -401,7 +403,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
public function getFragment(string $class, int $index = 0) : ? Query\Fragment
{
foreach($this->queryStack as $item) {
if ( get_class($item) === $class ) {
if ( is_a($item, $class, true) ) {
if ( $index-- === 0 ) {
return $item;
}
@ -419,6 +421,11 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
}
}
public function getFragments() : array
{
return $this->queryStack;
}
public function __toString() : string
{

View File

@ -31,7 +31,7 @@ class Repository
$this->queryBuilder = new QueryBuilder();
}
public function __clone()
public function __clone()
{
#$this->queryBuilder = clone $this->queryBuilder;
}
@ -285,6 +285,8 @@ class Repository
$this->select("$alias.$key as {$prependField}{$field['name']}");
}
}
return $this;
}
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;
}
@ -616,7 +618,7 @@ class Repository
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self
{
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)
->likes($searchRequest->likes(), Query\Where::CONDITION_OR)
->groups($searchRequest->groups())
@ -632,6 +634,11 @@ class Repository
->limit($searchRequest->limit());
}
protected function serverRequestCountRepository() : Repository
{
return new Repository\ServerRequestCountRepository($this->entityClass, $this->alias, $this->adapter);
}
public function collectionFromQuery(? string $entityClass = null) : EntityCollection
{
$class = $entityClass ?: $this->entityClass;
@ -646,7 +653,7 @@ class Repository
$entityCollection->append( ( new $class() )->resetVirtualProperties()->entityFillFromDataset($entityData) );
}
$this->eventExecute(Event\RepositoryCollectionFromQueryInterface::class, $entityCollection);
$this->eventExecute(Event\Repository\CollectionFromQueryInterface::class, $entityCollection);
return $entityCollection;
}
@ -793,6 +800,11 @@ class Repository
return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_SCHEMA);
}
public function hasFilters() : bool
{
return isset($this->queryBuilder->where);
}
protected function matchEntity(object $entity) {
return get_class($entity) === $this->entityClass;
}

View File

@ -7,7 +7,7 @@ use Ulmus\{ Repository, Query };
class MssqlRepository extends Repository {
protected function finalizeQuery() : void
{
{
if ( null !== $offset = $this->queryBuilder->getFragment(Query\Offset::class) ) {
if ( null === $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
throw new \Exception("Your offset query fragment is missing a LIMIT value.");
@ -28,6 +28,7 @@ class MssqlRepository extends Repository {
$this->queryBuilder->push($mssqlOffset);
}
elseif ( null !== $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
$select->top = $limit->limit;
}

View File

@ -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;
}
}