<commit_before><?php

/**
 * Copyright (c) 2011-present Mediasift Ltd
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * 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.
 *
 *   * Neither the names of the copyright holders nor the names of his
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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
 * COPYRIGHT OWNER OR CONTRIBUTORS 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.
 *
 * @category  Libraries
 * @package   Storyplayer/ConfigLib
 * @author    <NAME> <<EMAIL>>
 * @copyright 2011-present Mediasift Ltd www.datasift.com
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @link      http://datasift.github.io/storyplayer
 */

namespace DataSift\Storyplayer\ConfigLib;

use DataSift\Stone\ObjectLib\BaseObject;
use DataSift\Stone\ConfigLib\ConfigHelper;

/**
 * tracks a list of available configs of a given type
 *
 * @category  Libraries
 * @package   Storyplayer/ConfigLib
 * @author    <NAME> <<EMAIL>>
 * @copyright 2011-present Mediasift Ltd www.datasift.com
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @link      http://datasift.github.io/storyplayer
 */
class ConfigList
{
    /**
     * what kind of config are we wrapping?
     *
     * this is the name of a class from ConfigLib
     *
     * @var string
     */
    private $configType = null;

    /**
     * where are we looking for these configs?
     *
     * @var string
     */
    private $searchFolder = null;

    /**
     * what configs (including hard-coded defaults) do we know about?
     *
     * @var array<SystemUnderTestConfig>
     */
    private $configs = [];

    /**
     * constructor
     */
    public function __construct($configType, $searchFolder)
    {
        $this->setConfigType($configType);
        $this->setSearchFolder($searchFolder);
    }

    /**
     * returns the folder where we look for config files
     *
     * @return string|null
     */
    public function getSearchFolder()
    {
        return $this->searchFolder;
    }

    /**
     * tells us where to look for config files
     *
     * @param string $searchFolder
     *        the folder to search inside
     */
    public function setSearchFolder($searchFolder)
    {
        $this->searchFolder = $searchFolder;
    }

    /**
     * find a list of config files in a folder, and load them
     *
     * @return array
     */
    public function findConfigs()
    {
        // look on disk
        $filenames = $this->findConfigFilenames();

        // let's get them processed
        foreach ($filenames as $filename) {
            $config = $this->newWrappedConfigObject();
            $config->loadConfigFromFile($filename);
            $config->validateConfig();
            $this->configs[$config->getName()] = $config;
        }

        // we want our list of configs to be sorted, to make presentation
        // to end-users easier
        ksort($this->configs);

        // all done
    }

    /**
     * build a list of the config files in the $searchFolder
     *
     * @return array<string>
     */
    protected function findConfigFilenames()
    {
        // where are we looking?
        $searchFolder = $this->getSearchFolder();

        // do we have somewhere to look?
        if (null === $searchFolder || !is_dir($searchFolder)) {
            return [];
        }

        // build our list
        $helper = new ConfigHelper();
        $filenames = $helper->getListOfConfigFilesIn($searchFolder, 'json');

        // all done
        return $filenames;
    }

    /**
     * what type of content are we a list of?
     *
     * @return string
     */
    public function getConfigType()
    {
        return $this->configType;
    }

    /**
     * tells us what type of content we are going to be a list of
     *
     * @param string $configType
     *        the name of a class from ConfigLib
     */
    public function setConfigType($configType)
    {
        $this->configType = $configType;
        $classname = $this->getWrappedConfigClassname();

        if (!class_exists($classname)) {
            throw new E4xx_NoSuchConfigClass($classname);
        }
    }

    /**
     * returns a classname you can use for creating objects
     *
     * @return string
     */
    public function getWrappedConfigClassname()
    {
        $classname = 'DataSift\Storyplayer\ConfigLib\\' . $this->configType;

        return $classname;
    }

    /**
     * returns a object of the correct type for the content we are listing
     *
     * @return object
     */
    public function newWrappedConfigObject()
    {
        $classname = $this->getWrappedConfigClassname();
        $obj = new $classname;

        return $obj;
    }

    /**
     * inject a config into our list
     *
     * useful for adding in hard-coded config options
     *
     * @param string $name
     *        the name of the config to inject
     * @param object $config
     *        the config to inject into our list
     */
    public function addConfig($name, $config)
    {
        // make sure $config is the right type
        $classname = $this->getWrappedConfigClassname();
        if (!$config instanceof $classname) {
            throw new E4xx_IncompatibleConfigClass(get_class($config), $classname);
        }

        // if we get here, all is good
        $this->configs[$name] = $config;

        // keep the configs sorted
        ksort($this->configs);
    }

    /**
     * returns our list of all known configs
     *<commit_msg>Fix: parameters in wrong order<commit_after><?php

/**
 * Copyright (c) 2011-present Mediasift Ltd
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * 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.
 *
 *   * Neither the names of the copyright holders nor the names of his
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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
 * COPYRIGHT OWNER OR CONTRIBUTORS 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.
 *
 * @category  Libraries
 * @package   Storyplayer/ConfigLib
 * @author    <NAME> <<EMAIL>>
 * @copyright 2011-present Mediasift Ltd www.datasift.com
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @link      http://datasift.github.io/storyplayer
 */

namespace DataSift\Storyplayer\ConfigLib;

use DataSift\Stone\ObjectLib\BaseObject;
use DataSift\Stone\ConfigLib\ConfigHelper;

/**
 * tracks a list of available configs of a given type
 *
 * @category  Libraries
 * @package   Storyplayer/ConfigLib
 * @author    <NAME> <<EMAIL>>
 * @copyright 2011-present Mediasift Ltd www.datasift.com
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @link      http://datasift.github.io/storyplayer
 */
class ConfigList
{
    /**
     * what kind of config are we wrapping?
     *
     * this is the name of a class from ConfigLib
     *
     * @var string
     */
    private $configType = null;

    /**
     * where are we looking for these configs?
     *
     * @var string
     */
    private $searchFolder = null;

    /**
     * what configs (including hard-coded defaults) do we know about?
     *
     * @var array<SystemUnderTestConfig>
     */
    private $configs = [];

    /**
     * constructor
     */
    public function __construct($configType, $searchFolder)
    {
        $this->setConfigType($configType);
        $this->setSearchFolder($searchFolder);
    }

    /**
     * returns the folder where we look for config files
     *
     * @return string|null
     */
    public function getSearchFolder()
    {
        return $this->searchFolder;
    }

    /**
     * tells us where to look for config files
     *
     * @param string $searchFolder
     *        the folder to search inside
     */
    public function setSearchFolder($searchFolder)
    {
        $this->searchFolder = $searchFolder;
    }

    /**
     * find a list of config files in a folder, and load them
     *
     * @return array
     */
    public function findConfigs()
    {
        // look on disk
        $filenames = $this->findConfigFilenames();

        // let's get them processed
        foreach ($filenames as $filename) {
            $config = $this->newWrappedConfigObject();
            $config->loadConfigFromFile($filename);
            $config->validateConfig();
            $this->configs[$config->getName()] = $config;
        }

        // we want our list of configs to be sorted, to make presentation
        // to end-users easier
        ksort($this->configs);

        // all done
    }

    /**
     * build a list of the config files in the $searchFolder
     *
     * @return array<string>
     */
    protected function findConfigFilenames()
    {
        // where are we looking?
        $searchFolder = $this->getSearchFolder();

        // do we have somewhere to look?
        if (null === $searchFolder || !is_dir($searchFolder)) {
            return [];
        }

        // build our list
        $helper = new ConfigHelper();
        $filenames = $helper->getListOfConfigFilesIn($searchFolder, 'json');

        // all done
        return $filenames;
    }

    /**
     * what type of content are we a list of?
     *
     * @return string
     */
    public function getConfigType()
    {
        return $this->configType;
    }

    /**
     * tells us what type of content we are going to be a list of
     *
     * @param string $configType
     *        the name of a class from ConfigLib
     */
    public function setConfigType($configType)
    {
        $this->configType = $configType;
        $classname = $this->getWrappedConfigClassname();

        if (!class_exists($classname)) {
            throw new E4xx_NoSuchConfigClass($classname);
        }
    }

    /**
     * returns a classname you can use for creating objects
     *
     * @return string
     */
    public function getWrappedConfigClassname()
    {
        $classname = 'DataSift\Storyplayer\ConfigLib\\' . $this->configType;

        return $classname;
    }

    /**
     * returns a object of the correct type for the content we are listing
     *
     * @return object
     */
    public function newWrappedConfigObject()
    {
        $classname = $this->getWrappedConfigClassname();
        $obj = new $classname;

        return $obj;
    }

    /**
     * inject a config into our list
     *
     * useful for adding in hard-coded config options
     *
     * @param string $name
     *        the name of the config to inject
     * @param object $config
     *        the config to inject into our list
     */
    public function addConfig($name, $config)
    {
        // make sure $config is the right type
        $classname = $this->getWrappedConfigClassname();
        if (!$config instanceof $classname) {
            throw new E4xx_IncompatibleConfigClass($classname, get_class($config));
        }

        // if we get here, all is good
        $this->configs[$name] = $config;

        // keep the configs sorted
        ksort($this->configs);
    }

    /**
     * returns our list of all known configs
     *
{'fraction_non_alphanumeric': 0.09385400488129576, 'fraction_numerical': 0.002070852747577842, 'mean_word_length': 2.3313624045331363, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '4735764', 'n_tokens_mistral': 3795, 'n_tokens_neox': 3602, 'n_words': 1276}