- WIP on entity save

This commit is contained in:
Dave Mc Nicoll 2020-01-31 16:38:48 -05:00
parent 5bd60129ea
commit d12623cc5e
5 changed files with 33 additions and 220 deletions

View File

@ -1,211 +0,0 @@
<?php
namespace Ulmus\Common;
trait ArrayObjectTrait {
protected $arrayobject_pointer = null;
protected $arrayobject_container = [];
protected $arrayobject_changed = [];
protected $arrayobject_selected = false;
public function count() : int
{
return count( $this->arrayobject_container() );
}
public function contains($term, $strict = false) : bool
{
return (array_search($term, $this->arrayobject_container(), $strict) !== false) ;
}
public function &arrayobject_current()
{
if ( !is_null($this->arrayobject_pointer) ) {
$var = &$this->arrayobject_container()[$this->arrayobject_pointer] ?: [];
$var || ( $var = [] );
return $var;
}
if ( $this->arrayobject_selected !== false ){
$ret = &$this->arrayobject_selected ?: [];
return $ret;
}
# Restoring integrity of container since it could be nullified
if ( ! is_array($this->arrayobject_container()) ) {
$this->arrayobject_container([]);
}
return $this->arrayobject_container();
}
public function offsetSet($offset, $value, $changed = null)
{
if ( $changed && (!isset($this->arrayobject_current()[$offset]) || ($this->arrayobject_current()[$offset] !== $value) ) ) {
$this->arrayobject_changed($offset, true);
}
return is_null($offset) ? $this->arrayobject_current()[] = $value : $this->arrayobject_current()[$offset] = $value;
}
public function arrayobject_set_pointer($pointer)
{
# $pointer could nullify obj pointer
if ( $this->arrayobject_pointer = $pointer ) {
# Creating dataset whenever we have a new one
if ( ! isset($this->arrayobject_container()[$this->arrayobject_pointer]) ) {
$this->arrayobject_container()[$this->arrayobject_pointer] = [];
$this->arrayobject_changed[$this->arrayobject_pointer] = [];
}
}
return $this;
}
public function arrayobject_select($selection, $purge = true)
{
if ( is_bool($selection) ) {
return $this->arrayobject_selected = $selection;
}
$purge && ( $this->arrayobject_selected = [] );
foreach($selection as $pointer) {
$this->arrayobject_selected[$pointer] = &$this->arrayobject_container[$pointer];
}
return true;
}
public function arrayobject_exist($pointer) : bool
{
return isset( $this->arrayobject_container()[$pointer] );
}
public function arrayobject_flush_changed() {
! is_null($this->arrayobject_pointer) ?
$this->arrayobject_changed[$this->arrayobject_pointer] = []
:
$this->arrayobject_changed = []
;
}
public function arrayobject_changed($offset = null, $set = null) {
if ($offset) {
if ($set !== null) {
! is_null($this->arrayobject_pointer) ?
( $this->arrayobject_changed[$this->arrayobject_pointer][$offset] = $set )
:
( $this->arrayobject_changed[$offset] = $set );
}
return !is_null($this->arrayobject_pointer) ? $this->arrayobject_changed[$this->arrayobject_pointer][$offset] : $this->arrayobject_changed[$offset];
}
return array_keys( !is_null($this->arrayobject_pointer)
? $this->arrayobject_changed[$this->arrayobject_pointer] ?? []
: $this->arrayobject_changed) ?? [];
}
public function arrayobject_remove($pointer) {
if ( isset($this->arrayobject_container()[$pointer]) ) {
unset( $this->arrayobject_container()[$pointer], $this->arrayobject_changed[$pointer]);
}
}
public function arrayobject_iterate($callback) {
if ( $callback && is_callable($callback) ) {
$pointer = $this->arrayobject_pointer;
foreach($this->arrayobject_container() as $key => $value) {
$this->arrayobject_set_pointer($key);
$callback($key, $this);
}
$this->arrayobject_set_pointer($pointer);
}
return $this;
}
public function offsetGet($offset)
{
if ( !is_null($this->arrayobject_pointer) ) {
return isset($this->arrayobject_container()[$this->arrayobject_pointer][$offset]) ? $this->arrayobject_container()[$this->arrayobject_pointer][$offset] : null;
}
else {
return isset($this->arrayobject_container()[$offset]) ? $this->arrayobject_container()[$offset] : null;
}
}
public function offsetExists($offset) : bool
{
return array_key_exists($offset, $this->arrayobject_current() );
}
public function offsetUnset($offset)
{
if ( !is_null($this->arrayobject_pointer)) {
unset($this->arrayobject_container()[$this->arrayobject_pointer][$offset]);
}
else {
unset($this->arrayobject_container()[$offset]) ;
}
}
public function arrayobject_sort($field, $order = 'ASC')
{
Arrayobj::order_by($this->arrayobject_container(), $field);
$order === 'DESC' && array_reverse($this->arrayobject_current());
}
public function rewind()
{
reset( $this->arrayobject_container() );
# Rewinding will also reset the pointer
$this->arrayobject_set_pointer(key($this->arrayobject_container()));
return $this;
}
public function current()
{
return $this->arrayobject_set_pointer( $this->key() );
}
public function key()
{
$var = key( $this->arrayobject_container() );
return $var;
}
public function next()
{
$var = next( $this->arrayobject_container() );
return $var;
}
public function valid() : bool
{
$key = $this->key();
return ( $key !== NULL ) && ( $key !== FALSE );
}
protected function &arrayobject_container($set = null)
{
if ( $set !== null ) {
$this->arrayobject_container = $set;
}
if ( $this->arrayobject_selected !== false ) {
return $this->arrayobject_selected;
}
return $this->arrayobject_container;
}
}

View File

@ -19,6 +19,21 @@ class EntityCollection extends \ArrayObject {
}
}
public function searchOne($value, string $field) : object
{
# Returning first value only
foreach($this->search($value, $field) as $item) {
return $item;
}
}
public function search($value, string $field) : Generator
{
foreach($this->filters(fn($v) => $v->$field === $value) as $item) {
yield $item;
}
}
public function buildArray(string $keyColumn, string $valueColumn) : array
{
$list = [];

View File

@ -53,7 +53,7 @@ trait EntityTrait {
switch($relation->type) {
case 'oneToMany':
$repository->where( $baseEntity->field($relation->foreignKey), 866); # <<<<<<<<< CHANGE $THIS->ID WITH PROPER NOMENCLATURE
$repository->where( $baseEntity->field($relation->foreignKey), 2166); # <<<<<<<<< CHANGE $THIS->ID WITH PROPER NOMENCLATURE
return $this->$name = $repository->loadAll();

View File

@ -31,7 +31,6 @@ class QueryBuilder
return $this;
}
public function delete(string $alias = "") : self
{
if ( ! $this->has(Query\Delete::class) ) {

View File

@ -26,21 +26,21 @@ class Repository
$this->entityResolver = Ulmus::resolveEntity($entity);
}
public function loadOne() : EntityCollection
public function loadOne() : ? object
{
return $this->limit(1)->collectionFromQuery();
return $this->limit(1)->collectionFromQuery()[0] ?? null;
}
public function loadFromPk($value, $primaryKey = "id") : ? object
{
return $this->where($primaryKey, $value)->loadOne();
}
public function loadAll() : EntityCollection
{
return $this->collectionFromQuery();
}
public function loadFromPk($value, $primaryKey = "id") : EntityCollection
{
return $this->where($primaryKey, $value)->loadOne();
}
public function loadFromField($field, $value) : EntityCollection
{
return $this->where($field, $value)->collectionFromQuery();
@ -61,6 +61,16 @@ class Repository
return $this->where($primaryKey, $value)->deleteOne();
}
public function save(object $entity) : bool
{
}
public function saveAll(EntityCollection $collection) : bool
{
}
public function yieldAll() : \Generator
{