<commit_before><?php declare(strict_types=1);
/**
 * MindTouch XArray
 *
 * Copyright (C) 2006-2016 MindTouch, Inc.
 * www.mindtouch.com  <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace MindTouch\XArray;

/**
 * Class XArray - get/set accessors for arrays
 *
 * @package MindTouch\XArray
 */
class XArray {

    /**
     * Get string representation of value
     *
     * @param mixed $value
     * @return string
     */
    private static function getStringValue($value) : string {
        if(is_bool($value)) {
            return $value === true ? 'true' : 'false';
        }
        return !is_string($value) ? strval($value) : $value;
    }

    /**
     * @param array $array
     * @param string $key
     * @param mixed $value
     * @return void
     */
    private static function setValHelper(array &$array, string $key, $value) : void {
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $key) {
            $i++;
            if($i == $count) {
                if($value === null) {
                    unset($array[$key]);
                    return;
                }
                $array[$key] = $value;
            } else if(!isset($array[$key])) {
                $array[$key] = [];
            }
            if(is_string($array[$key])) {
                return;
            }
            $array = &$array[$key];
        }
    }

    /**
     * @var array
     */
    protected $array = [];

    /**
     * @param array $array - array values to create XArray from. If not supplied, XArray will start empty
     */
    public function __construct(array $array = null) { $this->array = $array !== null ? $array : []; }

    /**
     * Find $key in the XArray, which is delimited by /
     * If the found value is itself an array of multiple values, the array is returned.
     * If the found value is a single value, it is wrapped in an array then returned.
     *
     * @param string $key - the array path to return, i.e. /pages/content
     * @param array $default - if the key is not found, this array will be returned
     * @return array|null
     */
    public function getAll(string $key = '', array $default = []) : ?array {
        $array = $this->array;
        if($key === '') {
            return $array;
        }
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $val) {
            $i++;
            if($val === '') {
                continue;
            }
            if(!isset($array[$val])) {
                return $default;
            }
            if(!is_array($array[$val])) {
                return [$array[$val]];
            }
            $array = $array[$val];
            if($i == $count) {
                if(key($array) !== 0) {
                    $array = [$array];
                }
            }
        }
        return $array;
    }

    /**
     * Find $key in the XArray, which is delimited by /
     * If the found value is itself an array of multiple values, it will return the value of array key 0.
     *
     * @param string $key - the array path to return, i.e. /pages/content
     * @param mixed $default - if the key is not found, this value will be returned
     * @return mixed|null
     */
    public function getVal(string $key, $default = null) {
        $array = $this->array;
        if($key === '') {
            return null;
        }
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $k => $val) {
            $i++;<commit_msg>return default value if key not supplied, totally my idea<commit_after><?php declare(strict_types=1);
/**
 * MindTouch XArray
 *
 * Copyright (C) 2006-2016 MindTouch, Inc.
 * www.mindtouch.com  <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace MindTouch\XArray;

/**
 * Class XArray - get/set accessors for arrays
 *
 * @package MindTouch\XArray
 */
class XArray {

    /**
     * Get string representation of value
     *
     * @param mixed $value
     * @return string
     */
    private static function getStringValue($value) : string {
        if(is_bool($value)) {
            return $value === true ? 'true' : 'false';
        }
        return !is_string($value) ? strval($value) : $value;
    }

    /**
     * @param array $array
     * @param string $key
     * @param mixed $value
     * @return void
     */
    private static function setValHelper(array &$array, string $key, $value) : void {
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $key) {
            $i++;
            if($i == $count) {
                if($value === null) {
                    unset($array[$key]);
                    return;
                }
                $array[$key] = $value;
            } else if(!isset($array[$key])) {
                $array[$key] = [];
            }
            if(is_string($array[$key])) {
                return;
            }
            $array = &$array[$key];
        }
    }

    /**
     * @var array
     */
    protected $array = [];

    /**
     * @param array $array - array values to create XArray from. If not supplied, XArray will start empty
     */
    public function __construct(array $array = null) { $this->array = $array !== null ? $array : []; }

    /**
     * Find $key in the XArray, which is delimited by /
     * If the found value is itself an array of multiple values, the array is returned.
     * If the found value is a single value, it is wrapped in an array then returned.
     *
     * @param string $key - the array path to return, i.e. /pages/content
     * @param array $default - if the key is not found, this array will be returned
     * @return array|null
     */
    public function getAll(string $key = '', array $default = []) : ?array {
        $array = $this->array;
        if($key === '') {
            return $array;
        }
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $val) {
            $i++;
            if($val === '') {
                continue;
            }
            if(!isset($array[$val])) {
                return $default;
            }
            if(!is_array($array[$val])) {
                return [$array[$val]];
            }
            $array = $array[$val];
            if($i == $count) {
                if(key($array) !== 0) {
                    $array = [$array];
                }
            }
        }
        return $array;
    }

    /**
     * Find $key in the XArray, which is delimited by /
     * If the found value is itself an array of multiple values, it will return the value of array key 0.
     *
     * @param string $key - the array path to return, i.e. /pages/content
     * @param mixed $default - if the key is not found, this value will be returned
     * @return mixed|null
     */
    public function getVal(string $key, $default = null) {
        $array = $this->array;
        if($key === '') {
            return $default;
        }
        $keys = explode('/', $key);
        $count = count($keys);
        $i = 0;
        foreach($keys as $k => $val) {
            $i++;
{'fraction_non_alphanumeric': 0.1328976034858388, 'fraction_numerical': 0.004357298474945534, 'mean_word_length': 1.741539482415395, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5061742', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2277, 'n_words': 780}