<commit_before><?php

namespace Becklyn\AssetsBundle\Asset;

use Becklyn\AssetsBundle\Exception\AssetsException;
use Becklyn\AssetsBundle\File\FilePath;


class Asset
{
    const NAMESPACE_REGEX = '[a-z][a-z0-9_]*?';

    //region Fields
    /**
     * @var string
     */
    private $namespace;


    /**
     * @var string
     */
    private $filePath;


    /**
     * @var string|null
     */
    private $hash;


    /**
     * @var string
     */
    private $fileType;
    //endregion


    /**
     * @param string $filePath
     * @param string $hash
     */
    public function __construct (string $namespace, string $filePath)
    {
        $this->namespace = $namespace;
        $this->filePath = $filePath;
        $this->fileType = \pathinfo($filePath, \PATHINFO_EXTENSION);
    }


    //region Accessors
    /**
     * @return string
     */
    public function getNamespace () : string
    {
        return $this->namespace;
    }


    /**
     * @return string
     */
    public function getFilePath () : string
    {
        return $this->filePath;
    }


    /**
     * @return null|string
     */
    public function getHash () : ?string
    {
        return $this->hash;
    }


    /**
     * @param null|string $hash
     */
    public function setHash (?string $hash) : void
    {
        $this->hash = $hash;
    }


    /**
     * @return mixed
     */
    public function getFileType ()
    {
        return $this->fileType;
    }
    //endregion


    /**
     * Returns the full asset path
     *
     * @return string
     */
    public function getAssetPath () : string
    {
        return "@{$this->getNamespace()}/{$this->getFilePath()}";
    }


    /**
     * Returns the final storage path, where the production file is dumped to
     *
     * @return string
     */
    public function getDumpFilePath () : string
    {
        $dir = dirname($this->filePath);
        $fileName = \basename($this->filePath, ".{$this->fileType}");

        $dir = "." === $dir
            ? ""
            : "{$dir}/";

        return "{$dir}{$fileName}.{$this->hash}.{$this->fileType}";
    }


    /**
     * @param string $assetPath
     * @return Asset
     * @throws AssetsException
     */
    public static function createFromAssetPath (string $assetPath) : self
    {
        if (1 === \preg_match('~^@(?<namespace>' . self::NAMESPACE_REGEX . ')/(?<path>.+)$~', $assetPath, $matches))
        {
            $path = trim($matches["path"], "/");

            if ("" === $path)<commit_msg>Separately handle the hash in the filename of the asset<commit_after><?php

namespace Becklyn\AssetsBundle\Asset;

use Becklyn\AssetsBundle\Exception\AssetsException;
use Becklyn\AssetsBundle\File\FilePath;


class Asset
{
    const NAMESPACE_REGEX = '[a-z][a-z0-9_]*?';

    //region Fields
    /**
     * @var string
     */
    private $namespace;


    /**
     * @var string
     */
    private $filePath;


    /**
     * @var string|null
     */
    private $hash;


    /**
     * @var string|null
     */
    private $fileNameHash;


    /**
     * @var string
     */
    private $fileType;
    //endregion


    /**
     * @param string $filePath
     * @param string $hash
     */
    public function __construct (string $namespace, string $filePath)
    {
        $this->namespace = $namespace;
        $this->filePath = $filePath;
        $this->fileType = \pathinfo($filePath, \PATHINFO_EXTENSION);
    }


    //region Accessors
    /**
     * @return string
     */
    public function getNamespace () : string
    {
        return $this->namespace;
    }


    /**
     * @return string
     */
    public function getFilePath () : string
    {
        return $this->filePath;
    }


    /**
     * @return null|string
     */
    public function getHash () : ?string
    {
        return $this->hash;
    }


    /**
     * @param null|string $hash
     */
    public function setHash (?string $hash) : void
    {
        if (null !== $hash)
        {
            $fileNameHash = rtrim($hash, "=");
            $fileNameHash = \strtr($fileNameHash, [
                "/" => "_",
            ]);
            $fileNameHash = \substr($fileNameHash, 0, 20);
        }
        else
        {
            $fileNameHash = null;
        }

        $this->hash = $hash;
        $this->fileNameHash = $fileNameHash;
    }


    /**
     * @return mixed
     */
    public function getFileType ()
    {
        return $this->fileType;
    }
    //endregion


    /**
     * Returns the full asset path
     *
     * @return string
     */
    public function getAssetPath () : string
    {
        return "@{$this->getNamespace()}/{$this->getFilePath()}";
    }


    /**
     * Returns the final storage path, where the production file is dumped to
     *
     * @return string
     */
    public function getDumpFilePath () : string
    {
        $dir = dirname($this->filePath);
        $fileName = \basename($this->filePath, ".{$this->fileType}");

        $dir = "." === $dir
            ? ""
            : "{$dir}/";

        return "{$this->getNamespace()}/{$dir}{$fileName}.{$this->fileNameHash}.{$this->fileType}";
    }


    /**
     * @param string $assetPath
     * @return Asset
     * @throws AssetsException
     */
    public static function createFromAssetPath (string $assetPath) : self
    {
        if (1 === \preg_match('~^@(?<namespace>' . self::NAMESPACE_REGEX . ')/(?<path>.+)$~', $assetPath, $matches))
        {
            $path = trim($matches["path"], "/");

            if ("" === $path)
{'fraction_non_alphanumeric': 0.15609228550829127, 'fraction_numerical': 0.0016222062004325882, 'mean_word_length': 1.7856425702811245, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 40, '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': '2680253', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1674, 'n_words': 352}