Ejemplo n.º 1
0
 public function testSliceException()
 {
     $exception = null;
     $array = array(0, 1, 2, 3);
     try {
         ArraySlice::slice($array, 1, 4, 0);
     } catch (\Exception $e) {
         $exception = $e;
     }
     $this->assertEquals("Step cannot be 0", $exception->getMessage());
 }
Ejemplo n.º 2
0
 protected function opChildSelector(&$jsonObject, $contents, &$result, $createInexistent = false)
 {
     if (is_array($jsonObject)) {
         $match = array();
         $contentsLen = strlen($contents);
         if ($contents === self::TOK_ALL) {
             $this->hasDiverged = true;
             foreach ($jsonObject as $key => $item) {
                 $result[] =& $jsonObject[$key];
             }
         } else {
             if (preg_match(self::RE_CHILD_NAME_LIST, $contents, $match)) {
                 $names = array_map(function ($x) {
                     return trim($x, " \t\n\r\v'\"");
                 }, explode(self::TOK_COMA, $contents));
                 if (count($names) > 1) {
                     $this->hasDiverged = true;
                 }
                 $names = array_filter($names, function ($x) use($createInexistent, $jsonObject) {
                     return $createInexistent || array_key_exists($x, $jsonObject);
                 });
                 foreach ($names as $name) {
                     if (!array_key_exists($name, $jsonObject)) {
                         $jsonObject[$name] = array();
                     }
                     $result[] =& $jsonObject[$name];
                 }
             } else {
                 if (preg_match(self::RE_INDEX_LIST, $contents)) {
                     $index = array_map(function ($x) use($jsonObject) {
                         $i = intval(trim($x));
                         if ($i < 0) {
                             $n = count($jsonObject);
                             $i = $i % $n;
                             if ($i < 0) {
                                 $i += $n;
                             }
                         }
                         return $i;
                     }, explode(self::TOK_COMA, $contents));
                     if (count($index) > 1) {
                         $this->hasDiverged = true;
                     }
                     $index = array_filter($index, function ($x) use($createInexistent, $jsonObject) {
                         return $createInexistent || array_key_exists($x, $jsonObject);
                     });
                     foreach ($index as $i) {
                         if (!array_key_exists($i, $jsonObject)) {
                             $jsonObject[$i] = array();
                         }
                         $result[] =& $jsonObject[$i];
                     }
                 } else {
                     if (preg_match(self::RE_ARRAY_INTERVAL, $contents, $match)) {
                         $this->hasDiverged = true;
                         $begin = null;
                         $step = null;
                         $end = null;
                         // end($match) has the matched group with the interval
                         $numbers = explode(self::TOK_COLON, end($match));
                         // $numbers has the different numbers of the interval
                         // depending on if there are 2 (begin:end) or 3 (begin:end:step)
                         // numbers $begin, $step, $end are reassigned
                         if (count($numbers) === 3) {
                             $step = $numbers[2] !== '' ? intval($numbers[2]) : $step;
                         }
                         $end = $numbers[1] !== '' ? intval($numbers[1]) : $end;
                         $begin = $numbers[0] !== '' ? intval($numbers[0]) : $begin;
                         $slice = ArraySlice::slice($jsonObject, $begin, $end, $step, true);
                         foreach ($slice as $i => $x) {
                             if ($x !== null) {
                                 $result[] =& $slice[$i];
                             }
                         }
                     } else {
                         if ($contents[0] === self::TOK_BOOL_EXPR && $contents[1] === self::TOK_EXPRESSION_BEGIN && $contents[$contentsLen - 1] === self::TOK_EXPRESSION_END) {
                             $this->hasDiverged = true;
                             $subexpr = substr($contents, 2, $contentsLen - 3);
                             foreach ($jsonObject as &$child) {
                                 if ($this->booleanExpression($child, $subexpr)) {
                                     $result[] =& $child;
                                 }
                             }
                         } else {
                             throw new InvalidJsonPathException($contents);
                         }
                     }
                 }
             }
         }
         return true;
     }
     return false;
 }