<commit_before><?php

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  AssetHandler.php - Part of the AssetHandler project.

  File created by <NAME> at 2016-08-08 - kl 15:20
  © - 2016
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

namespace Jite\AssetHandler;

use Jite\AssetHandler\Contracts\AssetHandlerInterface;
use Jite\AssetHandler\Exceptions\AssetNameNotUniqueException;
use Jite\AssetHandler\Exceptions\InvalidAssetException;
use Jite\AssetHandler\Exceptions\InvalidContainerException;
use Jite\AssetHandler\Types\AssetTypes;

class AssetHandler implements AssetHandlerInterface {

    private $knownTypes = array(
        AssetTypes::STYLE_SHEET => "/\\.css$/i",
        AssetTypes::SCRIPT => "/\\.js$/i",
        AssetTypes::IMAGE => "/\\.(jpg|jpeg|tiff|gif|png|bmp|ico)$/i"
    );

    /** @var AssetContainer[] */
    private $containers = array();

    public function __construct() {
        foreach (AssetTypes::getTypes() as $type) {
            if ($type === AssetTypes::ANY) {
                continue;
            }

            $this->containers[$type] = new AssetContainer();
        }
    }

    private function containerExists(string $container) : bool {
        if (!array_has($this->containers, $container)) {
            return false;
        }
        return true;
    }

    private function determineContainer(string $fileName) : string {
        foreach ($this->knownTypes as $type => $pattern) {
            if (preg_match($pattern, $fileName) === 1) {
                return $type;
            }
        }
        return "";
    }


    /**
     * Add an asset to the handler.
     *
     * Observe:
     * If no container is specified the handler will add it to a predefined container depending on its file type.
     *
     * @param string $asset Asset path excluding the base path for given container.
     * @param string $assetName Asset name, Optional, if no name, the path will be used as name.
     * @param string $container Container name.<commit_msg>Added helper methods for fetching assets by name/path.<commit_after><?php

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  AssetHandler.php - Part of the AssetHandler project.

  File created by <NAME> at 2016-08-08 - kl 15:20
  © - 2016
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

namespace Jite\AssetHandler;

use Jite\AssetHandler\Contracts\AssetHandlerInterface;
use Jite\AssetHandler\Exceptions\AssetNameNotUniqueException;
use Jite\AssetHandler\Exceptions\InvalidAssetException;
use Jite\AssetHandler\Exceptions\InvalidContainerException;
use Jite\AssetHandler\Types\AssetTypes;

class AssetHandler implements AssetHandlerInterface {

    private $knownTypes = array(
        AssetTypes::STYLE_SHEET => "/\\.css$/i",
        AssetTypes::SCRIPT => "/\\.js$/i",
        AssetTypes::IMAGE => "/\\.(jpg|jpeg|tiff|gif|png|bmp|ico)$/i"
    );

    /** @var AssetContainer[] */
    private $containers = array();

    public function __construct() {
        foreach (AssetTypes::getTypes() as $type) {
            if ($type === AssetTypes::ANY) {
                continue;
            }

            $this->containers[$type] = new AssetContainer();
        }
    }

    private function containerExists(string $container) : bool {
        if (!array_has($this->containers, $container)) {
            return false;
        }
        return true;
    }

    private function determineContainer(string $fileName) : string {
        foreach ($this->knownTypes as $type => $pattern) {
            if (preg_match($pattern, $fileName) === 1) {
                return $type;
            }
        }
        return "";
    }

    /**
     * @param string $assetName
     * @param        $containers
     * @return Asset|null
     */
    private function findAssetByName(string $assetName, $containers) {
        foreach ($containers as $cType) {
            $result = $this->containers[$cType]->find(function(Asset $asset) use($assetName) {
                $result = $asset->getName() === $assetName;
                return $result;
            });

            if ($result !== null) {
                return $result;
            }
        }
        return null;
    }

    /**
     * @param string $assetPath
     * @param        $containers
     * @return Asset|null
     */
    private function findAssetByPath(string $assetPath, $containers) {
        foreach ($containers as $cType) {
            $result = $this->containers[$cType]->find(function(Asset $asset) use($assetPath) {
                $result = $asset->getPath() === $assetPath;
                return $result;
            });

            if ($result !== null) {
                return $result;
            }
        }
        return null;
    }

    /**
     * Add an asset to the handler.
     *
     * Observe:
     * If no container is specified the handler will add it to a predefined container depending on its file type.
     *
     * @param string $asset Asset path excluding the base path for given container.
     * @param string $assetName Asset name, Optional, if no name, the path will be used as name.
     * @param string $container Container name.
{'fraction_non_alphanumeric': 0.13990825688073394, 'fraction_numerical': 0.006498470948012232, 'mean_word_length': 2.146722790138304, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1737408', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1484, 'n_words': 368}