diff --git a/system/arrayClass/caseInsensitiveArray.php b/system/arrayClass/caseInsensitiveArray.php new file mode 100644 index 0000000..8e110cc --- /dev/null +++ b/system/arrayClass/caseInsensitiveArray.php @@ -0,0 +1,88 @@ +_container = array_map("strtolower", $initial_array); + $this->_container = array_change_key_case($initial_array); + } + + /** + * Assigns a value to the specified offset + * + * @param string $offset The offset to assign the value to + * @param mixed $value The value to set + * @access public + * @abstracting ArrayAccess + * + * @return void + */ + public function offsetSet($offset, $value) + { + if (is_string($offset)) $offset = strtolower($offset); + if (is_null($offset)) { + $this->_container[] = $value; + } else { + $this->_container[$offset] = $value; + } + } + + /** + * Whether or not an offset exists + * + * @param string $offset An offset to check for + * @access public + * @return bool + * @abstracting ArrayAccess + */ + public function offsetExists($offset) + { + if (is_string($offset)) $offset = strtolower($offset); + return isset($this->_container[$offset]); + } + + /** + * Unsets an offset + * + * @param string $offset The offset to unset + * @access public + * @abstracting ArrayAccess + * + * @return void + */ + public function offsetUnset($offset) + { + if (is_string($offset)) $offset = strtolower($offset); + unset($this->_container[$offset]); + } + + /** + * Returns the value at specified offset + * + * @param string $offset The offset to retrieve + * @access public + * @return mixed + * @abstracting ArrayAccess + */ + public function offsetGet($offset) + { + if (is_string($offset)) $offset = strtolower($offset); + return isset($this->_container[$offset]) + ? $this->_container[$offset] + : null; + } +} \ No newline at end of file diff --git a/system/database/databases/object/item.php b/system/database/databases/object/item.php new file mode 100644 index 0000000..7fe25d2 --- /dev/null +++ b/system/database/databases/object/item.php @@ -0,0 +1,135 @@ +__data = $data; + return $this; + } + + /** + * + * @return int<0, \max> + */ + public function count(): int { + return count($this->__data); + } + + /** + * + * @param array $data + * @return $this + */ + public function setData(array $data) { + $this->__data = $data; + //$this->__data = new SplObjectStorage(); + + /* foreach($data as $key => $val) { + $keyForA = new self([$key => $val]); + $this->__data[$keyForA] = [$key => $val]; + } */ + + //$this->__data->attach(new self($data)); + //$this->__data->attach(json_decode(json_encode($data))); + return $this; + } + + /** + * + * @return \Traversable|mixed[] + */ + public function getIterator(): Traversable { + return new \ArrayIterator($this->__data); + } + + /** + * @param string $key + * @return mixed + */ + private function _getValue($key){ + return $this->__data[$key] ?? null; + } + + public function __get($key){ + return $this->__data[$key] ?? null; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setValue($key, $value){ + $this->__data[$key] = $value; + + return $this; + } + + /** + * + * @param string $method + * @param string[] $args + * @return mixed + */ + public function __call($method, $args) { + if(($p = strpos($method, 'get')) !== false && $p === 0){ + $fieldname = (str_replace('get', '', $method)); + + if(!empty($args)){ + switch ($fieldname) { + case 'Values': + case 'Value': + if(count($args) === 1) + return $this->_getValue($args[0]); + + $values = []; + foreach($args as $arg){ + $values[$arg] = $this->_getValue($arg); + } + return $values; + break; + + default: + # code... + break; + } + + } + + //Convert data to insensitive case + $lower_array_object = new \Phacil\Framework\ArrayClass\CaseInsensitiveArray( + $this->__data + ); + + return $lower_array_object[$fieldname]; + } + } +} \ No newline at end of file diff --git a/system/database/databases/object/itemInterface.php b/system/database/databases/object/itemInterface.php new file mode 100644 index 0000000..3c77790 --- /dev/null +++ b/system/database/databases/object/itemInterface.php @@ -0,0 +1,17 @@ + + */ + public function count(): int { + return (int) $this->getNumRows(); + } + /** * * {@inheritdoc} @@ -38,6 +53,13 @@ class Result implements ResultInterface{ return $numRow ? $this->getRow($numRow) : $this->getRows(); } + /** + * {@inheritdoc} + */ + public function getItems() { + return $this->__toObject(); + } + /** * {@inheritdoc} */ @@ -83,5 +105,41 @@ class Result implements ResultInterface{ public function getNumRows(){ return $this->num_rows; } + + /** + * {@inheritdoc} + */ + public function __toObject() { + return $this->data ? $this->data : $this->loop($this->rows); + } + + /** + * + * @param mixed $array + * @return \Phacil\Framework\Databases\Object\ItemInterface[] + */ + protected function loop($array) + { + if($this->data) return $this->data; + + $this->data = new SplObjectStorage(); + foreach ($array as $key => $value) { + //$this->data[] = new \Phacil\Framework\Databases\Object\Item($value); + $obj = new \Phacil\Framework\Databases\Object\Item(); + $this->data->attach($obj->setData($value)); + } + + return $this->data; + } + + /** + * {@inheritdoc} + */ + public function getIterator(): \Traversable + { + $this->loop($this->rows); + return $this->data; + //return new \ArrayIterator($this->loop($this->rows)); + } } \ No newline at end of file diff --git a/system/database/databases/object/resultInterface.php b/system/database/databases/object/resultInterface.php index e225267..c0bf19a 100644 --- a/system/database/databases/object/resultInterface.php +++ b/system/database/databases/object/resultInterface.php @@ -15,7 +15,7 @@ namespace Phacil\Framework\Databases\Object; * @property array $rows * @package Phacil\Framework\Databases\Object */ -interface ResultInterface { +interface ResultInterface extends \Countable, \IteratorAggregate { /** * * @param array $rows @@ -62,4 +62,16 @@ interface ResultInterface { * @return array */ public function getData($numRow = false); + + /** + * + * @return \Phacil\Framework\Databases\Object\Item[] + */ + public function __toObject(); + + /** + * + * @return \Phacil\Framework\Databases\Object\Item[] + */ + public function getItems(); } \ No newline at end of file