Example #1
0
 protected function searchResult($k, $v, $path, &$results, $cpaths, $depth, $clauses)
 {
     if (qtil\ArrayUtil::isIterable($v) && $k == $path) {
         if (count($cpaths) - 1 > $depth) {
             foreach ($v as $i) {
                 $search = $this->recursiveSearch($i, $cpaths, $depth + 1);
                 if (!isset($results[$k])) {
                     $results[$k] = [];
                 }
                 $keys = array_keys($search);
                 foreach ($keys as $k2) {
                     if (!isset($results[$k][$k2])) {
                         $results[$k][$k2] = [];
                     }
                     $results[$k][$k2] = array_merge($results[$k][$k2], [$search[$k2]]);
                 }
             }
         } else {
             if (!empty($clauses)) {
                 $results[$k] = $this->match($v, $clauses);
             } else {
                 $results[$k] = $v;
             }
         }
     } elseif ($k == $path) {
         $results[$k] = $v;
     }
 }
Example #2
0
 /**
  * Updates resource using modified references
  * @param array $updates
  * @throws \InvalidArgumentException
  */
 protected function save($updates)
 {
     $out = '';
     if (qtil\ArrayUtil::isIterable($updates)) {
         foreach ($updates as $section => $values) {
             if (is_numeric($section)) {
                 throw new \InvalidArgumentException();
             }
             $this->array[$section] = $updates[$section];
         }
     }
     if (qtil\ArrayUtil::isIterable($this->array)) {
         foreach ($this->array as $section => $values) {
             $out .= '[' . $section . ']' . PHP_EOL;
             foreach ($values as $key => $value) {
                 $out .= $key . '=' . $value . PHP_EOL;
             }
             $out .= PHP_EOL;
         }
     }
     $stream = $this->getStream();
     if (!$stream->isOpen()) {
         $stream->open();
     }
     $stream->truncate(0);
     $writer = new qio\File\Writer($stream);
     $writer->write($out);
     $stream->close();
 }
Example #3
0
 /**
  * modifies xpath to limit to elements which contain input
  * @throws \InvalidArgumentException
  */
 function execute()
 {
     $query = $this->getQuery();
     $args = $this->getArguments();
     $numArgs = count($args);
     if ($numArgs == 2) {
         $name = $args[0];
         $value = $args[1];
     } else {
         throw new \InvalidArgumentException("XPath Contains must have 2 arguments");
     }
     $valueFn = function ($value) {
         if ($value !== '.') {
             return "'{$value}'";
         } else {
             return $value;
         }
     };
     if (qtil\ArrayUtil::isIterable($value)) {
         $nvalue = [];
         foreach ($value as $v) {
             $nvalue[] = $valueFn($v);
         }
         $value = implode(',', $nvalue);
     } else {
         $value = $valueFn($value);
     }
     $query['xpath'] .= '[contains(' . $name . ',' . $value . ')]';
 }
Example #4
0
 function __construct()
 {
     $this->setFactory(new GenericFactory());
     $this->setMapping(['section' => 'xral\\Tests\\Mock\\Section'], function ($input) {
         if (!qtil\ArrayUtil::isMulti($input) && qtil\ArrayUtil::isIterable($input)) {
             return 'section';
         }
     });
 }
Example #5
0
 /**
  * Default constructor for adapter
  * @param qtil\Collection|xral\Data\View|array $views
  */
 public function __construct($views = null)
 {
     if ($views instanceof View) {
         $views = new qtil\Collection([$views]);
     } elseif (qtil\ArrayUtil::isIterable($views)) {
         $views = new qtil\Collection($views);
     } else {
         $views = new qtil\Collection();
     }
     $this->views = $views;
 }
Example #6
0
 /**
  * Recursively searches array
  * @param array $array
  * @param callable $fn
  * @param array $args
  * @return array
  */
 protected function recurse(array $array, callable $fn, array $args)
 {
     foreach ($array as $key => $value) {
         if (\qtil\ArrayUtil::isIterable($value)) {
             $array[$key] = $this->recurse($value, $fn, $args);
         } elseif (!$this->search($fn, array_merge([$value, $key], $args))) {
             unset($array[$key]);
         }
     }
     return $array;
 }
Example #7
0
 /**
  * Default mapping constructor
  * @param array $mapping
  * @param \Closure $namingCallback
  */
 function __construct()
 {
     $args = func_get_args();
     $data = [];
     if (func_num_args()) {
         foreach ($args as $a) {
             if (qtil\ArrayUtil::isIterable($a)) {
                 $data = $a;
             } elseif (is_callable($a)) {
                 $this->setNamingCallback($a);
             }
         }
     }
     parent::__construct($data);
 }
Example #8
0
 /**
  * Performs recursive filtering
  * @param mixed $input
  * @param mixed $parent
  * @return mixed
  */
 function execute($input, $parent = null)
 {
     if (qtil\ArrayUtil::isMulti($input)) {
         foreach ($input as $key => $value) {
             $v = $this->execute($value, $parent);
             $input[$key] = $v;
         }
     } else {
         $input = $this->filter($input, $parent);
     }
     if (is_null($parent)) {
         $this->setState(self::COMPLETED);
     }
     return $input;
 }
Example #9
0
 /**
  * Statement execution
  * @param callable $statement
  * @return \qinq\Query\Statement|\qinq\Collection|null
  */
 protected function statement(qinq\Query\Statement $statement)
 {
     $result = null;
     $l = call_user_func_array($statement, func_get_args());
     if ($l instanceof Statement) {
         $result = $l;
     } elseif (qtil\ArrayUtil::isIterable($l)) {
         if ($statement->getMutable() === false) {
             return new qinq\Collection($l);
         } else {
             $this->collection->from($l);
         }
     } elseif (!is_null($l)) {
         $result = $l;
     }
     return $result;
 }
Example #10
0
 /**
  * starts join
  * @return \qinq\Object\Query\Join
  */
 public function execute()
 {
     $collection = $this->getCollection();
     $fn = $this->getCallback();
     $arr = $collection->toArray();
     $args = $this->getArguments();
     if (empty($fn) && count($args) === 1) {
         $this->group = [new qinq\Collection($arr), new qinq\Collection($args[0])];
     } elseif (!empty($fn)) {
         $arr1 = array_filter($arr, $fn);
         $arr2 = array_diff($arr, $arr1);
         $this->group = [new qinq\Collection($arr), new qinq\Collection($arr2)];
     }
     if (qtil\ArrayUtil::isIterable($this->group) && qtil\ArrayUtil::isMultiObject($this->group)) {
         return $this;
     }
 }
Example #11
0
 /**
  * Where statement
  * Modifies ypath to limit yml query results
  * @param string $attributeOperator
  */
 function execute($attributeOperator = '|')
 {
     $query = $this->getQuery();
     $args = $this->getArguments();
     if (!empty($args)) {
         $name = '';
         if (isset($args[0])) {
             $name = $args[0];
         }
         $value = null;
         if (isset($args[1])) {
             $value = $args[1];
         }
         $operator = 'or';
         if (isset($args[2])) {
             $operator = trim($args[2]);
         }
         if (!qtil\ArrayUtil::isIterable($value)) {
             $value = [$value];
         }
         $nvalue = '';
         $c = 0;
         foreach ($value as $v) {
             if ($c > 0) {
                 $nvalue .= ' ' . $operator . ' ';
             }
             $nvalue .= $name . '=' . '"' . $v . '"';
             $c++;
         }
         $value = $nvalue;
         if (strpos($query['ypath'], '[') !== false) {
             $newPath = ' ' . $attributeOperator . ' ' . $name;
         } else {
             $newPath = '';
         }
         if (!is_null($value)) {
             $newPath .= '[' . $value . ']';
         }
     }
     if (isset($newPath)) {
         $query['ypath'] = $query['ypath'] . $newPath;
     }
 }
Example #12
0
 /**
  * Performs xpath limiter procedure
  * @param \SimpleXMLElement|array $input
  * @return mixed
  */
 function execute($input)
 {
     $xpath = $this->xpath;
     if ($input instanceof \SimpleXMLElement && isset($input->{$xpath})) {
         return $input->{$xpath};
     }
     if (is_object($input) && method_exists($input, 'xpath')) {
         $namespaces = $this->query->getNamespaces();
         foreach ($namespaces as $prefix => $ns) {
             $input->registerXPathNamespace($prefix, $ns);
         }
         return $input->xpath($xpath);
     } elseif (qtil\ArrayUtil::isIterable($input)) {
         $results = [];
         foreach ($input as $i) {
             if (is_object($i) && method_exists($i, 'xpath')) {
                 $results[] = $this->execute($i);
             }
         }
         return $results;
     }
 }
Example #13
0
 /**
  * Merges array into local array
  * @param array $data
  * @return Collection
  * @throws \InvalidArgumentException
  */
 public function merge($data)
 {
     if (!ArrayUtil::isIterable($data)) {
         throw new \InvalidArgumentException();
     }
     foreach ($data as $key => $val) {
         $this->insert($key, $val);
     }
     return $this;
 }
Example #14
0
 function testArrayUtilInsertArray()
 {
     $data1 = ['red', 'green', 'purple'];
     $data2 = ['orange', 'blue', 'yellow'];
     $data3 = qtil\ArrayUtil::insertArray($data1, $data2, 'green');
     $count = count($data3);
     $value = $data3[1];
     $this->assertEquals(6, $count);
     $this->assertEquals('orange', $value);
 }
Example #15
0
 /**
  * Default XML query execution path
  * @return mixed
  */
 function execute()
 {
     $result = parent::execute();
     if (empty($result)) {
         return null;
     }
     $reduce = function ($val) {
         if (is_scalar($val)) {
             return $val;
         } elseif ($val instanceof \DOMDocument) {
             return $val;
         } elseif ($val instanceof \SimpleXMLElement) {
             return $val;
         }
         return $val;
     };
     if (empty($this['xpath'])) {
         return $reduce($result[0]);
     }
     if (qtil\ArrayUtil::isMultiObject($result->toArray()) && count($result) == 1) {
         $val = $result[$result->keys()[0]];
         return new qinq\Collection($val);
     }
     return $result->filter();
 }