- Fixed some mssql caused bugs within Insert and Create into querybuilding

- Added some methods to EntityCollection
This commit is contained in:
Dave M. 2020-12-07 17:26:51 +00:00
parent f74d1907d9
commit 65de1dd849
2 changed files with 95 additions and 79 deletions

@ -5,17 +5,17 @@ namespace Ulmus;
use Generator; use Generator;
class EntityCollection extends \ArrayObject { class EntityCollection extends \ArrayObject {
public ? string $entityClass = null; public ? string $entityClass = null;
public function filters(Callable $callback, bool $yieldValueOnly = false) : Generator public function filters(Callable $callback, bool $yieldValueOnly = false) : Generator
{ {
$idx = 0; $idx = 0;
foreach($this as $key => $item) { foreach($this as $key => $item) {
if ( $callback($item, $key, $idx) ) { if ( $callback($item, $key, $idx) ) {
$idx++; $idx++;
if ( $yieldValueOnly ) { if ( $yieldValueOnly ) {
yield $item; yield $item;
} }
@ -25,91 +25,91 @@ class EntityCollection extends \ArrayObject {
} }
} }
} }
public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self
{ {
$collection = new static(); $collection = new static();
foreach($this->filters($callback, $yieldValueOnly) as $item) { foreach($this->filters($callback, $yieldValueOnly) as $item) {
$collection->append($item); $collection->append($item);
} }
if ($replaceCollection) { if ($replaceCollection) {
$this->exchangeArray(array_values($collection->getArrayCopy())); $this->exchangeArray(array_values($collection->getArrayCopy()));
return $this; return $this;
} }
else { else {
return $collection; return $collection;
} }
} }
public function iterate(Callable $callback) : self public function iterate(Callable $callback) : self
{ {
foreach($this as $item) { foreach($this as $item) {
$callback($item); $callback($item);
} }
return $this; return $this;
} }
public function removeOne($value, string $field, bool $strict = true) : ? object public function removeOne($value, string $field, bool $strict = true) : ? object
{ {
foreach($this->search($value, $field, $strict) as $key => $item) { foreach($this->search($value, $field, $strict) as $key => $item) {
$this->offsetUnset($key); $this->offsetUnset($key);
return $item; return $item;
} }
return null; return null;
} }
public function remove($value, string $field, bool $strict = true) : array public function remove($value, string $field, bool $strict = true) : array
{ {
$removed = []; $removed = [];
foreach($this->search($value, $field, $strict) as $key => $item) { foreach($this->search($value, $field, $strict) as $key => $item) {
$this->offsetUnset($key); $this->offsetUnset($key);
$removed[] = $item; $removed[] = $item;
} }
return $removed; return $removed;
} }
public function search($value, string $field, bool $strict = true) : Generator public function search($value, string $field, bool $strict = true) : Generator
{ {
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) { foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
yield $key => $item; yield $key => $item;
} }
} }
public function searchOne($value, string $field, bool $strict = true) : ? object public function searchOne($value, string $field, bool $strict = true) : ? object
{ {
# Returning first value only # Returning first value only
foreach($this->search($value, $field, $strict) as $item) { foreach($this->search($value, $field, $strict) as $item) {
return $item; return $item;
} }
return null; return null;
} }
public function searchAll($value, string $field, bool $strict = true) : self public function searchAll($value, string $field, bool $strict = true) : self
{ {
$obj = new static(); $obj = new static();
foreach($this->search($value, $field, $strict) as $item) { foreach($this->search($value, $field, $strict) as $item) {
$obj->append($item); $obj->append($item);
} }
return $obj; return $obj;
} }
public function column($field, bool $unique = false) : array public function column($field, bool $unique = false) : array
{ {
$list = []; $list = [];
foreach($this as $item) { foreach($this as $item) {
if ( is_callable($field) ) { if ( is_callable($field) ) {
$value = call_user_func_array($field, [ $item ]); $value = call_user_func_array($field, [ $item ]);
@ -117,22 +117,22 @@ class EntityCollection extends \ArrayObject {
else { else {
$value = $item->$field; $value = $item->$field;
} }
if ($unique && in_array($value, $list)) { if ($unique && in_array($value, $list)) {
break; break;
} }
$list[] = $value; $list[] = $value;
} }
return $list; return $list;
} }
public function unique(/*stringable|callable */ $field, bool $strict = false) : self public function unique(/*stringable|callable */ $field, bool $strict = false) : self
{ {
$list = []; $list = [];
$obj = new static(); $obj = new static();
foreach($this as $item) { foreach($this as $item) {
if ( $field === null) { if ( $field === null) {
$value = $this; $value = $this;
@ -143,23 +143,23 @@ class EntityCollection extends \ArrayObject {
else { else {
$value = $item->$field; $value = $item->$field;
} }
if ( ! in_array($value, $list, $strict) ) { if ( ! in_array($value, $list, $strict) ) {
$list[] = $value; $list[] = $value;
$obj->append($item); $obj->append($item);
} }
} }
return $obj; return $obj;
} }
public function first() : ? object public function first() : ? object
{ {
foreach($this as $item) { foreach($this as $item) {
return $item; return $item;
} }
return null; return null;
} }
@ -168,103 +168,119 @@ class EntityCollection extends \ArrayObject {
foreach($this as $item) { foreach($this as $item) {
$return = $item; $return = $item;
} }
return $return ?? null; return $return ?? null;
} }
public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array
{ {
$list = []; $list = [];
foreach($this as $item) { foreach($this as $item) {
switch (true) { switch (true) {
case is_null($value): case is_null($value):
$list[] = $item->$keyColumn; $list[] = $item->$keyColumn;
break; break;
case is_callable($value): case is_callable($value):
$list[$item->$keyColumn] = $value($item); $list[$item->$keyColumn] = $value($item);
break; break;
case is_object($value): case is_object($value):
case is_string($value): case is_string($value):
$value = (string) $value; $value = (string) $value;
$list[$item->$keyColumn] = $item->$value; $list[$item->$keyColumn] = $item->$value;
break; break;
} }
} }
return $list; return $list;
} }
public function implode(string $glue, ? Callable $callback = null) : string public function implode(string $glue, ? Callable $callback = null) : string
{ {
$values = []; $values = [];
foreach($this as $item) { foreach($this as $item) {
$values[] = $callback ? $callback($item) : (string) $item; $values[] = $callback ? $callback($item) : (string) $item;
} }
return implode($glue, $values); return implode($glue, $values);
} }
public function toArray(bool $includeRelations = false) : array { public function toArray(bool $includeRelations = false) : array {
$list = []; $list = [];
foreach($this as $entity) { foreach($this as $entity) {
$list[] = $entity->toArray($includeRelations); $list[] = $entity->toArray($includeRelations);
} }
return $list; return $list;
} }
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
{
foreach($datasets as $dataset) {
$this->append( $this->arrayToEntity($dataset, $entityClass) );
}
return $this;
}
public function arrayToEntity(array $dataset, ? string /*stringable*/ $entityClass = null) : object
{ {
if ( ! ($this->entityClass || $entityClass) ) { if ( ! ($this->entityClass || $entityClass) ) {
throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection."); throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection.");
} }
$className = $entityClass ?: $this->entityClass; $className = $this->entityClass;
foreach($datasets as $dataset) { return ( new $className() )->fromArray($dataset);
$this->append( (new $className() )->fromArray($dataset) );
}
return $this;
} }
public function append($value) : void
{
if ( is_array($value) ) {
$this->append( $this->arrayToEntity($value) );
}
else {
parent::append($value);
}
}
public function mergeWith( /*array|EntityCollection*/ $datasets ) : self public function mergeWith( /*array|EntityCollection*/ $datasets ) : self
{ {
if ( is_object($datasets) ) { if ( is_object($datasets) ) {
$datasets = $datasets->getArrayCopy(); $datasets = $datasets->getArrayCopy();
} }
$this->exchangeArray( array_merge( $this->getArrayCopy(), $datasets ) ); $this->exchangeArray( array_merge( $this->getArrayCopy(), $datasets ) );
return $this; return $this;
} }
public function replaceWith( /*array|EntityCollection*/ $datasets ) : self public function replaceWith( /*array|EntityCollection*/ $datasets ) : self
{ {
if ( is_object($datasets) ) { if ( is_object($datasets) ) {
$datasets = $datasets->getArrayCopy(); $datasets = $datasets->getArrayCopy();
} }
$this->exchangeArray( $datasets ); $this->exchangeArray( $datasets );
return $this; return $this;
} }
public function randomize() : self public function randomize() : self
{ {
$arr = $this->getArrayCopy(); $arr = $this->getArrayCopy();
shuffle($arr); shuffle($arr);
$this->exchangeArray($arr); $this->exchangeArray($arr);
return $this; return $this;
} }
public function sort(callable $callback, $function = "uasort") : self public function sort(callable $callback, $function = "uasort") : self
{ {
call_user_func_array([ $this, $function ], [ $callback ]); call_user_func_array([ $this, $function ], [ $callback ]);

@ -64,11 +64,11 @@ class QueryBuilder
{ {
if ( null === $this->getFragment(Query\Insert::class) ) { if ( null === $this->getFragment(Query\Insert::class) ) {
if ( $schema ) { if ( $schema ) {
$table = "\"$schema\".$table"; $table = "$schema.$table";
} }
if ( $database ) { if ( $database ) {
$table = "\"$database\".$table"; $table = "$database.$table";
} }
$insert = new Query\Insert(); $insert = new Query\Insert();
@ -327,11 +327,11 @@ class QueryBuilder
{ {
if ( null === $this->getFragment(Query\Create::class) ) { if ( null === $this->getFragment(Query\Create::class) ) {
if ( $schema ) { if ( $schema ) {
$table = "\"$schema\".$table"; $table = "$schema.$table";
} }
if ( $database ) { if ( $database ) {
$table = "\"$database\".$table"; $table = "$database.$table";
} }
$create = new Query\Create(); $create = new Query\Create();