PHP Classes

File: src/Filters/RecursiveFilter.php

Recommend this page to a friend!
  Classes of Sascha Greuel   PHP JSON Path   src/Filters/RecursiveFilter.php   Download  
File: src/Filters/RecursiveFilter.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP JSON Path
Query values from data structures like XPATH
Author: By
Last change:
Date: 3 years ago
Size: 1,061 bytes
 

Contents

Class file image Download
<?php

/**
 * JSONPath implementation for PHP.
 *
 * @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
 */

declare(strict_types=1);

namespace
Flow\JSONPath\Filters;

use
Flow\JSONPath\{AccessHelper, JSONPathException};
use
ArrayAccess;

class
RecursiveFilter extends AbstractFilter
{
   
/**
     * @inheritDoc
     *
     * @throws JSONPathException
     */
   
public function filter($collection): array
    {
       
$result = [];

       
$this->recurse($result, $collection);

        return
$result;
    }

   
/**
     * @param array|ArrayAccess $data
     *
     * @throws JSONPathException
     */
   
private function recurse(array &$result, $data): void
   
{
       
$result[] = $data;

        if (
AccessHelper::isCollectionType($data)) {
            foreach (
AccessHelper::arrayValues($data) as $key => $value) {
               
$results[] = $value;

                if (
AccessHelper::isCollectionType($value)) {
                   
$this->recurse($result, $value);
                }
            }
        }
    }
}