File: src/Filters/QueryResultFilter.php

Recommend this page to a friend!
  Classes of Sascha Greuel  >  PHP JSON Path  >  src/Filters/QueryResultFilter.php  >  Download  
File: src/Filters/QueryResultFilter.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: 10 months ago
Size: 1,765 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 function
count;
use function
preg_match;

class
QueryResultFilter extends AbstractFilter
{
   
/**
     * @inheritDoc
     *
     * @throws JSONPathException
     */
   
public function filter($collection): 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);
        } elseif (
$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');
        }

       
$result = [];

        if (
AccessHelper::keyExists($collection, $resultKey, $this->magicIsAllowed)) {
           
$result[] = AccessHelper::getValue($collection, $resultKey, $this->magicIsAllowed);
        }

        return
$result;
    }
}

For more information send a message to info at phpclasses dot org.