<commit_before><?php

namespace Sokil\IsoCodes;

/**
 * Abstract collection of ISO entries
 */
abstract class AbstractDatabase implements \Iterator, \Countable
{
    /**
     * Path to ISO databases
     */
    const DATABASE_PATH = '/../databases';

    /**
     * Path to gettext localised messages
     */
    const MESSAGES_PATH = '/../messages';

    /**
     * Cluster index used for iteration by entries
     *
     * @var array[]
     */
    private $clusterIndex;

    /**
     * Index to search by entry field's values
     *
     * @var array
     */
    private $index;

    public function __construct()
    {
        $this->loadDatabase($this->getDatabaseFilePath());
    }

    /**
     * ISO Standard Number
     *
     * @return string
     *
     * @throws \Exception
     */
    public static function getISONumber()
    {
        // abstract static methods not allowed on PHP < 7.0
        throw new \Exception(
            sprintf(
                'Method "%s" must be inmpemented in class %s',
                __METHOD__,
                get_class()
            )
        );
    }

    /**
     * @param array $entry
     *
     * @return object
     */
    abstract protected function arrayToEntry(array $entry);

    /**
     * List of entry fields to be indexed and searched.
     * May be override in child classes to search by indexed fields.
     *
     * First index in array used as cluster index.
     *
     * @return array
     */
    protected function getIndexDefinition()
    {
        return [];
    }

    /**
     * @return string
     */
    private function getDatabaseFilePath()
    {
        return __DIR__ . self::DATABASE_PATH . '/iso_' . $this->getISONumber() . '.json';
    }

    /**
     * @return string
     */
    private function getLocalMessagesPath()
    {
        return __DIR__ . self::MESSAGES_PATH;
    }

    /**
     * Build cluster index for iteration
     *
     * @param string $databaseFile
     */
    private function loadDatabase($databaseFile)
    {
        $isoNumber = $this->getISONumber();

        // add gettext domain
        \bindtextdomain(
            $isoNumber,
            $this->getLocalMessagesPath()
        );

        \bind_textdomain_codeset(
            $isoNumber,
            'UTF-8'
        );

        // load database from json file
        $json = json_decode(
            file_get_contents($databaseFile),
            true
        );<commit_msg>remove the \ from internal function<commit_after><?php

namespace Sokil\IsoCodes;

/**
 * Abstract collection of ISO entries
 */
abstract class AbstractDatabase implements \Iterator, \Countable
{
    /**
     * Path to ISO databases
     */
    const DATABASE_PATH = '/../databases';

    /**
     * Path to gettext localised messages
     */
    const MESSAGES_PATH = '/../messages';

    /**
     * Cluster index used for iteration by entries
     *
     * @var array[]
     */
    private $clusterIndex;

    /**
     * Index to search by entry field's values
     *
     * @var array
     */
    private $index;

    public function __construct()
    {
        $this->loadDatabase($this->getDatabaseFilePath());
    }

    /**
     * ISO Standard Number
     *
     * @return string
     *
     * @throws \Exception
     */
    public static function getISONumber()
    {
        // abstract static methods not allowed on PHP < 7.0
        throw new \Exception(
            sprintf(
                'Method "%s" must be inmpemented in class %s',
                __METHOD__,
                get_class()
            )
        );
    }

    /**
     * @param array $entry
     *
     * @return object
     */
    abstract protected function arrayToEntry(array $entry);

    /**
     * List of entry fields to be indexed and searched.
     * May be override in child classes to search by indexed fields.
     *
     * First index in array used as cluster index.
     *
     * @return array
     */
    protected function getIndexDefinition()
    {
        return [];
    }

    /**
     * @return string
     */
    private function getDatabaseFilePath()
    {
        return __DIR__ . self::DATABASE_PATH . '/iso_' . $this->getISONumber() . '.json';
    }

    /**
     * @return string
     */
    private function getLocalMessagesPath()
    {
        return __DIR__ . self::MESSAGES_PATH;
    }

    /**
     * Build cluster index for iteration
     *
     * @param string $databaseFile
     */
    private function loadDatabase($databaseFile)
    {
        $isoNumber = $this->getISONumber();

        // add gettext domain
        bindtextdomain(
            $isoNumber,
            $this->getLocalMessagesPath()
        );

        bind_textdomain_codeset(
            $isoNumber,
            'UTF-8'
        );

        // load database from json file
        $json = json_decode(
            file_get_contents($databaseFile),
            true
        );
{'fraction_non_alphanumeric': 0.10505915952672379, 'fraction_numerical': 0.0012239902080783353, 'mean_word_length': 1.645979492714517, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1648395', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1321, 'n_words': 368}