/**
  * @param $collection
  * @return array
  */
 public function filter($collection)
 {
     $return = [];
     foreach ($this->token->value as $index) {
         if (AccessHelper::keyExists($collection, $index, $this->magicIsAllowed)) {
             $return[] = AccessHelper::getValue($collection, $index, $this->magicIsAllowed);
         }
     }
     return $return;
 }
Exemple #2
0
 /**
  * @param array $collection
  * @return array
  */
 public function filter($collection)
 {
     if (AccessHelper::keyExists($collection, $this->token->value, $this->magicIsAllowed)) {
         return array(AccessHelper::getValue($collection, $this->token->value, $this->magicIsAllowed));
     } else {
         if ($this->token->value === "*") {
             return AccessHelper::arrayValues($collection);
         }
     }
     return array();
 }
Exemple #3
0
 private function recurse(&$result, $data)
 {
     $result[] = $data;
     if (AccessHelper::isCollectionType($data)) {
         foreach (AccessHelper::arrayValues($data) as $key => $value) {
             $results[] = $value;
             if (AccessHelper::isCollectionType($value)) {
                 $this->recurse($result, $value);
             }
         }
     }
 }
 /**
  * @param $collection
  * @return array
  */
 public function filter($collection)
 {
     /*
     
             a[start:end] # items start through end-1
             a[start:]    # items start through the rest of the array
             a[:end]      # items from the beginning through end-1
             a[:]         # a copy of the whole array
     
             a[-1]    # last item in the array
             a[-2:]   # last two items in the array
             a[:-2]   # everything except the last two items
     */
     $result = [];
     $length = count($collection);
     $start = $this->token->value['start'];
     $end = $this->token->value['end'];
     $step = $this->token->value['step'] ?: 1;
     if ($start !== null && $end !== null) {
         // standard
     }
     if ($start !== null && $end === null) {
         // negative index start means the end is -1, else the end is last index
         $end = $start < 0 ? -1 : $length - 1;
     }
     if ($start === null && $end !== null) {
         $start = 0;
     }
     if ($start === null && $end === null) {
         $start = 0;
         $end = $length - 1;
     }
     if ($end < 0 && $start >= 0) {
         $end = $length + $end - 1;
     }
     if ($start < 0 && $end === null) {
         $end = -1;
     }
     for ($i = $start; $i <= $end; $i += $step) {
         $index = $i;
         if ($i < 0) {
             $index = $length + $i;
         }
         if (AccessHelper::keyExists($collection, $index, $this->magicIsAllowed)) {
             $result[] = $collection[$index];
         }
     }
     return $result;
 }
 /**
  * @param array $collection
  * @throws \Exception
  * @return array
  */
 public function filter($collection)
 {
     $return = [];
     preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
     if (!isset($matches[1])) {
         throw new \Exception("Malformed filter query");
     }
     $key = $matches['key'] ?: $matches['keySquare'];
     if ($key === "") {
         throw new \Exception("Malformed filter query: key was not set");
     }
     $operator = isset($matches['operator']) ? $matches['operator'] : null;
     $comparisonValue = isset($matches['comparisonValue']) ? $matches['comparisonValue'] : null;
     if (strtolower($comparisonValue) === "false") {
         $comparisonValue = false;
     }
     if (strtolower($comparisonValue) === "true") {
         $comparisonValue = true;
     }
     if (strtolower($comparisonValue) === "null") {
         $comparisonValue = null;
     }
     $comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
     $comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
     foreach ($collection as $value) {
         if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
             $value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed);
             if ($operator === null && AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
                 $return[] = $value;
             }
             if (($operator === "=" || $operator === "==") && $value1 == $comparisonValue) {
                 $return[] = $value;
             }
             if (($operator === "!=" || $operator === "!==" || $operator === "<>") && $value1 != $comparisonValue) {
                 $return[] = $value;
             }
             if ($operator == ">" && $value1 > $comparisonValue) {
                 $return[] = $value;
             }
             if ($operator == "<" && $value1 < $comparisonValue) {
                 $return[] = $value;
             }
         }
     }
     return $return;
 }
 /**
  * @param array $collection
  * @return array
  */
 public function filter($collection)
 {
     $result = array();
     preg_match('/@\\.(?<key>\\w+)\\s*(?<operator>-|\\+|\\*|\\/)\\s*(?<numeric>\\d+)/', $this->token->value, $matches);
     $matchKey = $matches['key'];
     if (AccessHelper::keyExists($collection, $matchKey, $this->magicIsAllowed)) {
         $value = AccessHelper::getValue($collection, $matchKey, $this->magicIsAllowed);
     } else {
         if ($matches['key'] === 'length') {
             $value = count($collection);
         } else {
             return;
         }
     }
     switch ($matches['operator']) {
         case '+':
             $resultKey = $value + $matches['numeric'];
             break;
         case '*':
             $resultKey = $value * $matches['numeric'];
             break;
         case '-':
             $resultKey = $value - $matches['numeric'];
             break;
         case '/':
             $resultKey = $value / $matches['numeric'];
             break;
         default:
             throw new JSONPathException("Unsupported operator in expression");
             break;
     }
     if (AccessHelper::keyExists($collection, $resultKey, $this->magicIsAllowed)) {
         $result[] = AccessHelper::getValue($collection, $resultKey, $this->magicIsAllowed);
     }
     return $result;
 }
Exemple #7
0
 /**
  * Return the current element
  */
 public function current()
 {
     $value = current($this->data);
     return AccessHelper::isCollectionType($value) ? new static($value, $this->options) : $value;
 }