<?php
/**
 * OpenMage
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available at https://opensource.org/license/osl-3-0-php
 *
 * @category   Varien
 * @package    Varien_File
 * @copyright  Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright  Copyright (c) 2022-2023 The OpenMage Contributors (https://www.openmage.org)
 * @license    https://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * File Object
 * *
 * @category   Varien
 * @package    Varien_File
 */

class Varien_File_Object extends SplFileObject implements Varien_Directory_IFactory
{
    protected string $_filename;
    protected string $_path;
    protected ?array $_filter = null;
    protected bool $_isCorrect = true; # - pass or not filter checking
    protected $filtered;

    /**
     * Constructor
     *
     * @param string $path - path to directory
     */
    public function __construct($path)
    {
        parent::__construct($path);
        $this->_path = $path;
        $this->_filename = basename($path);
    }
    /**
     * add file name to array
     *
     * @param   array &$files - array of files
     * @return  void
     */
    public function getFilesName(&$files)
    {
        $this->getFileName($files);
    }
    /**
     * add file name to array
     *
     * @param   array &$files - array of files
     */
    public function getFileName(&$files=null): string
    {
        if($this->_isCorrect){
            if($files === null) {
                return $this->_filename;
            }

            $files[] = $this->_filename;
        }

        return $this->_filename;
    }
    /**
     * add file path to array
     *
     * @param   array &$paths - array of paths
     * @return  void
     */
    public function getFilesPaths(&$paths)
    {
        if ($this->_isCorrect) {
            $paths[] = (string)$this->_path;
        }
    }
    /**
     * add file path to array
     *
     * @param   array &$paths - array of paths
     * @return  ?string
     */
    public function getFilePath(&$path = null)
    {
        if ($this->_isCorrect) {
            if ($path === null) {
                return $this->_path;
            }
            $paths[] = $this->_path;
        }
    }
    /**
     * use filter
     *
     * @param   bool $useFilter - use or not filter
     * @return  void
     */
    public function useFilter($useFilter)
    {
        if ($useFilter) {
            $this->renderFilter();
        } else {
            $this->_isCorrect = true;
            $this->filtered = false;
        }
    }
    /**
     * add file object to array
     *
     * @param   array &$objs - array of gile objects
     * @return  void
     */
    public function getFilesObj(&$objs)
    {
        if ($this->_isCorrect) {
            $objs[] = $this;
        }
    }
    /**
     * @param array &$dirs - array of dirs
     * @return string
     */
    public function getDirsName(&$dirs)
    {
        return Varien_Directory_Collection::getLastDir($this->_path);
    }
    /**
     * @param   array &$dirs - array of dirs
     * @return  string
     */
    public function getDirName()
    {
        return Varien_Directory_Collection::getLastDir($this->_path);
    }
    /**
     * set file filter
     *
     * @param   array $filter - array of filter
     * @return  void
     */
    public function setFilesFilter($filter)
    {
        $this->addFilter($filter);
    }
    /**
     * set file filter
     *
     * @param   array $filter - array of filter
     * @return  void
     */
    public function addFilter($filter)
    {
        $this->_filter = $filter;
    }
    /**
     * get extension of file
     */
    public function getExtension(): string
    {
        return self::getExt($this->_filename);
    }
    /**
     * get extension of file
     *
     * @param   string $fileName - name of file
     * @return  string - extension of file
     */
    public static function getExt($fileName)
    {
        $path_parts = pathinfo($fileName);
        if (isset($path_parts["extension"])) {
            return $path_parts["extension"];
        } else {
            return '';
        }
    }
    /**
     * get name of file
     *
     * @return  string - name of file
     */
    public function getName()
    {
        return basename($this->_filename, '.' . $this->getExtension());
    }
    /**
     * render filters
     *
     * @return void
     */
    public function renderFilter()
    {
        if (isset($this->_filter) && count($this->_filter) > 0 && $this->filtered == false) {
            $this->filtered = true;
            if (isset($this->_filter['extension'])) {
                $filter = $this->_filter['extension'];
                if ($filter != null) {
                    if (is_array($filter)) {
                        if (!in_array($this->getExtension(), $filter)) {
                            $this->_isCorrect = false;
                        }
                    } else {
                        if ($this->getExtension() != $filter) {
                            $this->_isCorrect = false;
                        }
                    }
                }
            }
            if (isset($this->_filter['name'])) {
                $filter = $this->_filter['name'];
                if ($filter != null) {
                    if (is_array($filter)) {
                        if (!in_array($this->getName(), $filter)) {
                            $this->_isCorrect = false;
                        }
                    } else {
                        if ($this->getName() != $filter) {
                            $this->_isCorrect = false;
                        }
                    }
                }
            }

            if (isset($this->_filter['regName'])) {
                $filter = $this->_filter['regName'];

                if ($filter != null) {
                    foreach ($filter as $value) {
                        if (!preg_match($value, $this->getName())) {
                            $this->_isCorrect = false;
                        }
                    }
                }
            }
        }
    }
    /**
     * add to array file name
     *
     * @param  array &$arr -export array
     * @return void
     */
    public function toArray(&$arr)
    {
        if ($this->_isCorrect) {
            $arr['files_in_dirs'][] = $this->_filename;
        }
    }
    /**
     * add to xml file name
     *
     * @param   array &$xml -export xml
     * @param   int $recursionLevel - level of recursion
     * @param   bool $addOpenTag - nothing
     * @param   string $rootName - nothing
     * @return void
     */
    public function toXml(&$xml, $recursionLevel = 0, $addOpenTag = true, $rootName = 'Struct')
    {
        if ($this->_isCorrect) {
            $xml .= str_repeat("\t", $recursionLevel + 2) . '<fileName>' . $this->_filename . '</fileName>' . "\n";
        }
    }
}
