commit 9be32ca3c2282b16ef6cfbb0e4d892fcdb95b374
Author: finwo <finwo@pm.me>
Date: Wed, 13 Apr 2016 16:36:46 +0200
Project init
Diffstat:
4 files changed, 322 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+/vendor/
+/.idea/
diff --git a/composer.json b/composer.json
@@ -0,0 +1,17 @@
+{
+ "name": "finwo/data-tools",
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "autoload": {
+ "psr-4": {
+ "Finwo\\Datatools\\": "src/"
+ }
+ },
+ "require": {
+ "finwo/property-accessor": "dev-master"
+ }
+}
diff --git a/composer.lock b/composer.lock
@@ -0,0 +1,50 @@
+{
+ "_readme": [
+ "This file locks the dependencies of your project to a known state",
+ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+ "This file is @generated automatically"
+ ],
+ "hash": "f2d93d684f129ad6f224e3b555c70cd2",
+ "content-hash": "a9673b6decde4278c21615caa82ce0ec",
+ "packages": [
+ {
+ "name": "finwo/property-accessor",
+ "version": "dev-master",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/finwo/property-accessor.git",
+ "reference": "43a9b49f82ba566800068427c12d28ac48efa8e3"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/finwo/property-accessor/zipball/43a9b49f82ba566800068427c12d28ac48efa8e3",
+ "reference": "43a9b49f82ba566800068427c12d28ac48efa8e3",
+ "shasum": ""
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Finwo\\PropertyAccessor\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "time": "2016-04-13 09:05:33"
+ }
+ ],
+ "packages-dev": [],
+ "aliases": [],
+ "minimum-stability": "stable",
+ "stability-flags": {
+ "finwo/property-accessor": 20
+ },
+ "prefer-stable": false,
+ "prefer-lowest": false,
+ "platform": [],
+ "platform-dev": []
+}
diff --git a/src/ArrayQuery.php b/src/ArrayQuery.php
@@ -0,0 +1,253 @@
+<?php
+
+namespace Finwo\Datatools;
+
+use Finwo\PropertyAccessor\PropertyAccessor;
+
+class ArrayQuery
+{
+ /**
+ * @var array
+ */
+ protected $table = array();
+
+ /**
+ * @var array
+ */
+ protected $selects = array();
+
+ /**
+ * @var array
+ */
+ protected $filters = array();
+
+ /**
+ * @var string
+ */
+ protected $selectedField = '';
+
+ /**
+ * @var PropertyAccessor
+ */
+ protected $accessor;
+
+ /**
+ * @var integer
+ */
+ protected $skip = 0;
+
+ /**
+ * @var integer
+ */
+ protected $limit = -1;
+
+ /**
+ * @var array
+ */
+ protected $columnNames = array();
+
+ /**
+ * ArrayQuery constructor.
+ *
+ * @param array $data
+ */
+ public function __construct($data = array())
+ {
+ $this->table = $data;
+ }
+
+ /**
+ * @param array $input
+ *
+ * @return ArrayQuery
+ */
+ public static function table($input = array())
+ {
+ return new ArrayQuery($input);
+ }
+
+ /**
+ * @param $input
+ *
+ * @return ArrayQuery $this
+ */
+ public function select( $input )
+ {
+ if (
+ is_callable($input) ||
+ is_string($input)
+ ) {
+ $this->selects[] = $input;
+ } elseif (is_array($input)) {
+ foreach($input as $item) {
+ $this->select($item);
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param $input
+ *
+ * @return $this
+ */
+ public function field( $input )
+ {
+ if(is_string($input)) {
+ $this->selectedField = $input;
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param $input
+ *
+ * @return $this
+ */
+ public function equals( $input )
+ {
+ if(is_string($input)) {
+ $this->filters[]= array(
+ 'field' => $this->selectedField,
+ 'type' => 'equals',
+ 'value' => $input,
+ );
+
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param $input
+ *
+ * @return $this
+ */
+ public function validates( $input )
+ {
+ if(is_callable($input)) {
+ $this->filters[]= array(
+ 'field' => $this->selectedField,
+ 'type' => 'validate',
+ 'value' => $input,
+ );
+
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param $input
+ *
+ * @return $this
+ */
+ public function columnName( $input )
+ {
+ if (is_array($input)) {
+ $this->columnNames = $input;
+ }
+
+ if (is_string($input)) {
+ $this->columnNames[] = $input;
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param integer $input
+ *
+ * @return $this
+ */
+ public function skip( $input )
+ {
+ $this->skip = intval($input);
+ return $this;
+ }
+
+ /**
+ * @param integer $input
+ *
+ * @return $this
+ */
+ public function limit( $input )
+ {
+ $this->limit = intval($input);
+ return $this;
+ }
+
+ /**
+ * @return PropertyAccessor
+ */
+ protected function getAccessor()
+ {
+ if (is_null($this->accessor)) {
+ $this->accessor = new PropertyAccessor();
+ }
+ return $this->accessor;
+ }
+
+ /**
+ * @return array
+ */
+ public function execute()
+ {
+ $outputIndex = 0;
+ $output = array();
+
+ //loop through table
+ foreach ($this->table as $row) {
+
+ // handle offset
+ if ( $this->skip-- > 0) {
+ continue;
+ }
+
+ // handle limit
+ if ( $this->limit-- == 0 ) {
+ break;
+ }
+
+ // reset the current row
+ $columnIndex = 0;
+ $outputRow = array();
+
+ // fetch columns
+ foreach ($this->selects as $select) {
+
+ // allow associative column names
+ $colName = $columnIndex++;
+ if (isset($this->columnNames[$colName])) {
+ $colName = $this->columnNames[$colName];
+ }
+
+ // fetch & store value
+ $outputRow[$colName] = $this->executeSelect($row, $select);
+ }
+
+ // add row to output
+ $output[$outputIndex++] = $outputRow;
+ }
+
+ return $output;
+ }
+
+ protected function executeSelect($row, $select)
+ {
+ // Callable select statements
+ if (is_callable($select)) {
+ return call_user_func($select, $row);
+ }
+
+ // String selects
+ if (is_string($select)) {
+ return $this->getAccessor()->get($row, $select);
+ }
+
+ // Return empty column
+ return null;
+ }
+}