<commit_before><?php
/**
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
namespace OPNsense\Core;

use \Phalcon\DI\FactoryDefault;

/**
 * Class Config provides access to systems config xml
 * @package Core
 */
class Config extends Singleton
{

    /**
     * config file location ( path + name )
     * @var string
     */
    private $config_file = "";

    /**
     * SimpleXML type reference to config
     * @var SimpleXML
     */
    private $simplexml = null;

    /**
     * status field: valid config loaded
     * @var bool
     */
    private $statusIsValid = false;


    /**
     * @return bool return (last known) status of this configuration
     */
    public function isValid()
    {
        return $this->statusIsValid;
    }

    /**
     * serialize xml to array structure (backwards compatibility mode)
     * @param null|array $forceList force specific tags to be contained in a list.
     * @param DOMNode $node
     * @return string|array
     * @throws ConfigException
     */
    public function toArray($forceList = null, $node = null)
    {
        $result = array();
        $this->checkvalid();
        // root node
        if ($node == null) {
            $node = $this->simplexml;
        }

        foreach ($node->children() as $xmlNode) {
            if ($xmlNode->count() > 0) {
                $tmpNode = $this->toArray($forceList, $xmlNode);
                if (array_key_exists($xmlNode->getName(), $result)) {
                    $old_content = $result[$xmlNode->getName()];
                    // check if array content is associative, move items to list
                    if (array_keys($old_content) !== range(0, count($old_content) - 1)) {
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $old_content;
                    }
                    $result[$xmlNode->getName()][] = $tmpNode;
                } elseif (is_array($forceList) && array_key_exists($xmlNode->getName(), $forceList)) {
                    // force tag in an array
                    $result[$xmlNode->getName()] = array();
                    $result[$xmlNode->getName()][] = $tmpNode;
                } else {
                    $result[$xmlNode->getName()] = $tmpNode;
                }
            } else {
                if (array_key_exists($xmlNode->getName(), $result)) {
                    // repeating item
                    if (!is_array($result[$xmlNode->getName()])) {
                        // move first item into list
                        $tmp = $result[$xmlNode->getName()];
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $tmp;
                    }
                    $result[$xmlNode->getName()][] = $xmlNode->__toString();
                } else {
                    // single content item
                    if (is_array($forceList) && array_key_exists($xmlNode->getName(), $forceList)) {
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $xmlNode->__toString();
                    } else {
                        $result[$xmlNode->getName()] = $xmlNode->__toString();
                    }
                }
            }
        }

        return $result;
    }

    /**
     * update (reset) config with array structure (backwards compatibility mode)
     * @param $source source array structure
     * @param null $node simplexml node
     * @param null|string $parentTagName
     * @throws ConfigException
     */
    public function fromArray($source, $node = null, $parentTagName = null)
    {
        $this->checkvalid();

        // root node
        if ($node == null) {
            $this->simplexml = simplexml_load_string('<'.$this->simplexml[0]->getName().'/>');
            $node = $this->simplexml ;
        }

        foreach ($source as $itemKey => $itemValue) {
            if (is_numeric($itemKey)) {
                // recurring tag (content), use parent tagname.
                $childNode = $node->addChild($parentTagName);
            } elseif (is_array($itemValue) && !(array_keys($itemValue) !== range(0, count($itemValue) - 1))) {
                // recurring tag, skip placeholder.
                $childNode = $node;
            } else {
                // add new child
                $childNode = $node->addChild($itemKey);
            }

            // set content, propagate container items.
            if (is_array($itemValue)) {
                $this->fromArray($itemValue, $childNode, $itemKey);
            } else {
                $childNode[0] = $itemValue;
            }
        }
    }

    /**
     * @throws ConfigException
     */
    private function checkvalid()
    {
        if (!$this->statusIsValid) {
            throw new ConfigException('no valid config loaded') ;<commit_msg>skip xml tags on false booleans in Config->fromArray()<commit_after><?php
/**
 *    Copyright (C) 2015 <NAME>.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
namespace OPNsense\Core;

use \Phalcon\DI\FactoryDefault;

/**
 * Class Config provides access to systems config xml
 * @package Core
 */
class Config extends Singleton
{

    /**
     * config file location ( path + name )
     * @var string
     */
    private $config_file = "";

    /**
     * SimpleXML type reference to config
     * @var SimpleXML
     */
    private $simplexml = null;

    /**
     * status field: valid config loaded
     * @var bool
     */
    private $statusIsValid = false;


    /**
     * @return bool return (last known) status of this configuration
     */
    public function isValid()
    {
        return $this->statusIsValid;
    }

    /**
     * serialize xml to array structure (backwards compatibility mode)
     * @param null|array $forceList force specific tags to be contained in a list.
     * @param DOMNode $node
     * @return string|array
     * @throws ConfigException
     */
    public function toArray($forceList = null, $node = null)
    {
        $result = array();
        $this->checkvalid();
        // root node
        if ($node == null) {
            $node = $this->simplexml;
        }

        foreach ($node->children() as $xmlNode) {
            if ($xmlNode->count() > 0) {
                $tmpNode = $this->toArray($forceList, $xmlNode);
                if (array_key_exists($xmlNode->getName(), $result)) {
                    $old_content = $result[$xmlNode->getName()];
                    // check if array content is associative, move items to list
                    if (array_keys($old_content) !== range(0, count($old_content) - 1)) {
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $old_content;
                    }
                    $result[$xmlNode->getName()][] = $tmpNode;
                } elseif (is_array($forceList) && array_key_exists($xmlNode->getName(), $forceList)) {
                    // force tag in an array
                    $result[$xmlNode->getName()] = array();
                    $result[$xmlNode->getName()][] = $tmpNode;
                } else {
                    $result[$xmlNode->getName()] = $tmpNode;
                }
            } else {
                if (array_key_exists($xmlNode->getName(), $result)) {
                    // repeating item
                    if (!is_array($result[$xmlNode->getName()])) {
                        // move first item into list
                        $tmp = $result[$xmlNode->getName()];
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $tmp;
                    }
                    $result[$xmlNode->getName()][] = $xmlNode->__toString();
                } else {
                    // single content item
                    if (is_array($forceList) && array_key_exists($xmlNode->getName(), $forceList)) {
                        $result[$xmlNode->getName()] = array();
                        $result[$xmlNode->getName()][] = $xmlNode->__toString();
                    } else {
                        $result[$xmlNode->getName()] = $xmlNode->__toString();
                    }
                }
            }
        }

        return $result;
    }

    /**
     * update (reset) config with array structure (backwards compatibility mode)
     * @param $source source array structure
     * @param null $node simplexml node
     * @param null|string $parentTagName
     * @throws ConfigException
     */
    public function fromArray($source, $node = null, $parentTagName = null)
    {
        $this->checkvalid();

        // root node
        if ($node == null) {
            $this->simplexml = simplexml_load_string('<'.$this->simplexml[0]->getName().'/>');
            $node = $this->simplexml ;
        }

        foreach ($source as $itemKey => $itemValue) {
            if (is_bool($itemValue) && $itemValue == false) {
                // skip empty booleans
                continue;
            }
            if (is_numeric($itemKey)) {
                // recurring tag (content), use parent tagname.
                $childNode = $node->addChild($parentTagName);
            } elseif (is_array($itemValue) && !(array_keys($itemValue) !== range(0, count($itemValue) - 1))) {
                // recurring tag, skip placeholder.
                $childNode = $node;
            } else {
                // add new child
                $childNode = $node->addChild($itemKey);
            }

            // set content, propagate container items.
            if (is_array($itemValue)) {
                $this->fromArray($itemValue, $childNode, $itemKey);
            } else {
                $childNode[0] = $itemValue;
            }
        }
    }

    /**
     * @throws ConfigException
     */
    private function checkvalid()
    {
        if (!$this->statusIsValid) {
            throw new ConfigException('no valid config loaded') ;
{'fraction_non_alphanumeric': 0.11917221693625119, 'fraction_numerical': 0.0020615287028227084, 'mean_word_length': 1.8324724904558725, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 69}, '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': '251375', 'n_tokens_mistral': 3341, 'n_tokens_neox': 3168, 'n_words': 972}