<commit_before><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\Util;

/**
 * Generic path following json file handler.
 */
class JsonArray implements \JsonSerializable
{
    /**
     * The json data.
     *
     * @var array
     */
    private $data;

    /**
     * Create a new instance.
     *
     * @param string|array $data The json data.
     */
    public function __construct($data = '{}')
    {
        if (is_string($data)) {
            $this->load($data);
        }
        if (is_array($data)) {
            $this->setData($data);
        }
    }

    /**
     * Set the data.
     *
     * @param array $data The data array.
     *
     * @return JsonArray
     */
    public function setData($data)
    {
        $this->data = (array) $data;

        return $this;
    }

    /**
     * Merge the passed data into this instance.
     *
     * @param array $data The data to absorb.
     *
     * @return JsonArray
     */
    public function merge($data)
    {
        return $this->setData(array_replace_recursive($this->getData(), (array) $data));
    }

    /**
     * Retrieve the data as json string.
     *
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * Load the data.
     *
     * @param string $data The json data.
     *
     * @return JsonArray
     *
     * @throws \RuntimeException When the data is invalid.
     */
    public function load($data)
    {
        $data = json_decode($data, true);

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new \RuntimeException('Error: json decode failed. ' . $this->jsonErrorMessage(), 1);
        }

        $this->setData($data);

        return $this;
    }

    /**
     * Split the path into chunks.
     *
     * @param string $path The path to split.
     *
     * @return array
     */
    protected function splitPath($path)
    {
        // TODO: what about escaped \/?
        return array_map([$this, 'unescape'], preg_split('#(?<!\\\)\/#', ltrim($path, '/')));
    }

    /**
     * Escape a string to be used as literal path.
     *
     * @param string $path The string to escape.
     *
     * @return string
     */
    public function unescape($path)
    {
        return str_replace('\/', '/', $path);
    }

    /**
     * Escape a string to be used as literal path.
     *
     * @param string $path The string to escape.
     *
     * @return string
     */
    public function escape($path)<commit_msg>Remove obsolete TODO<commit_after><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\Util;

/**
 * Generic path following json file handler.
 */
class JsonArray implements \JsonSerializable
{
    /**
     * The json data.
     *
     * @var array
     */
    private $data;

    /**
     * Create a new instance.
     *
     * @param string|array $data The json data.
     */
    public function __construct($data = '{}')
    {
        if (is_string($data)) {
            $this->load($data);
        }
        if (is_array($data)) {
            $this->setData($data);
        }
    }

    /**
     * Set the data.
     *
     * @param array $data The data array.
     *
     * @return JsonArray
     */
    public function setData($data)
    {
        $this->data = (array) $data;

        return $this;
    }

    /**
     * Merge the passed data into this instance.
     *
     * @param array $data The data to absorb.
     *
     * @return JsonArray
     */
    public function merge($data)
    {
        return $this->setData(array_replace_recursive($this->getData(), (array) $data));
    }

    /**
     * Retrieve the data as json string.
     *
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * Load the data.
     *
     * @param string $data The json data.
     *
     * @return JsonArray
     *
     * @throws \RuntimeException When the data is invalid.
     */
    public function load($data)
    {
        $data = json_decode($data, true);

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new \RuntimeException('Error: json decode failed. ' . $this->jsonErrorMessage(), 1);
        }

        $this->setData($data);

        return $this;
    }

    /**
     * Split the path into chunks.
     *
     * @param string $path The path to split.
     *
     * @return array
     */
    protected function splitPath($path)
    {
        return array_map([$this, 'unescape'], preg_split('#(?<!\\\)\/#', ltrim($path, '/')));
    }

    /**
     * Escape a string to be used as literal path.
     *
     * @param string $path The string to escape.
     *
     * @return string
     */
    public function unescape($path)
    {
        return str_replace('\/', '/', $path);
    }

    /**
     * Escape a string to be used as literal path.
     *
     * @param string $path The string to escape.
     *
     * @return string
     */
    public function escape($path)
{'fraction_non_alphanumeric': 0.13637130801687763, 'fraction_numerical': 0.0016877637130801688, 'mean_word_length': 1.8504088504088505, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1035722', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1787, 'n_words': 517}