PHP Classes

File: tests/Traits/TestDataTrait.php

Recommend this page to a friend!
  Classes of Sascha Greuel   PHP JSON Path   tests/Traits/TestDataTrait.php   Download  
File: tests/Traits/TestDataTrait.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: 966 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\Test\Traits;

use
ArrayAccess;
use
RuntimeException;

trait
TestDataTrait
{
   
/**
     * Returns decoded JSON from a given file either as array or object.
     *
     * @param bool|int $asArray
     *
     * @return array|ArrayAccess|null
     */
   
protected function getData(string $type, $asArray = true)
    {
       
$filePath = sprintf('%s/data/%s.json', dirname(__DIR__, 1), $type);

        if (!
file_exists($filePath)) {
            throw new
RuntimeException("File {$filePath} does not exist.");
        }

       
$json = json_decode(file_get_contents($filePath), (bool)$asArray);

        if (
json_last_error() !== JSON_ERROR_NONE) {
            throw new
RuntimeException("File {$filePath} does not contain valid JSON.");
        }

        return
$json;
    }
}