<commit_before><?php namespace ZN;
/**
 * ZN PHP Web Framework
 * 
 * "Simplicity is the ultimate sophistication." ~ Da Vinci
 * 
 * @package ZN
 * @license MIT [http://opensource.org/licenses/MIT]
 * @author  <NAME> [<EMAIL>]
 */

class Wizard
{
    /**
     * CRLF
     * 
     * @const string
     */
    const CRLF = '\s*(\n|' . PHP_EOL . '|\:|$)';

    /**
     * Get config
     * 
     * @var array
     */
    protected static $config;

    /**
     * Keeps true codes
     * 
     * @var array
     */
    protected static $keepTrueCodes;

    /**
     * PHP tag isolation
     * 
     * @param string $data
     * 
     * @return void
     */
	public static function isolation(String $data = '')
	{
		Filesystem::replaceData($data, ['<?php', '<?', '?>'], ['{[', '{[', ']}']);
	}

    /**
     * Get data.
     * 
     * @param string $string
     * @param array  $data   = []
     * @param array  $config = []
     * 
     * @return string
     */
    public static function data(String $string, Array $data = [], Array $config = []) : String
    {
        self::$config = $config ?: Config::get('ViewObjects', 'wizard');

        self::callableJS($string);

        $code = self::convertWizardContent($string);

        self::convertTrueCode($code);

        return Buffering::code($code, $data);
    }

    /**
     * Protected convert true code
     */
    protected static function convertTrueCode(&$code)
    {
        if( ! empty(self::$keepTrueCodes) )
        {
            $code = str_replace(array_keys(self::$keepTrueCodes), array_values(self::$keepTrueCodes), $code);
        }
    }

    /**
     * Protected convert wizard content
     */
    protected static function convertWizardContent($string)
    {
        self::textControl($string); # 5.4.6[added]
        
        $pattern = array_merge
        (
            self::symbolsHeader(),
            self::tags(),
            self::keywords(),
            self::printable(),
            self::functions(),
            self::symbolsFooter(),
            self::comments(),
            self::required(),
            self::jsdata(),
            self::html()
        );

        return self::replace($pattern, $string);
    }

    /**
     * protected text control.
     * 
     * [changed]5.7.5.1
     * [fixed  ]5.7.5.6|5.7.5.7
     * 
     * @param string &$string
     * 
     * @return void
     */
    protected static function textControl(&$string)
    {   
        self::internalTextControl('style' , ['#' => '/#', '@' => '/@', ':' => '/:'], $string);
        self::internalTextControl('script', ['#' => '/#'], $string);
    }
    
    /**
     * Protected internal text control
     * 
     * [added]5.7.5.7
     */
    protected static function internalTextControl($type, $controls, &$string)
    {
        # If the use of custom html is obvious or if the page uses an internal style or code
        preg_match_all('/(\<|\#)'.$type.'(?<data>.*?)(\<\/|\#\#)'.$type.'/si', $string, $standart);

        $patterns = $standart['data'];

        if( ! empty($patterns) ) 
        {
            $changes = [];

            $keys   = array_keys($controls);
            $values = array_values($controls);

            foreach( $patterns as $pattern )
            {
                $changes[] = str_replace($keys, $values, $pattern);
            }

            $string = str_replace($patterns, $changes, $string);
        } 
    }

    /**
     * protected replace.
     * 
     * @param array  $pattern
     * @param string $string
     * 
     * @return string
     */
    protected static function replace($pattern, $string)
    {
        return preg_replace(array_keys($pattern), array_values($pattern), $string);
    }

    /**
     * protected required
     * 
     * @param void
     * 
     * @return array
     */
    protected static function required()
    {
        return
        [
            '/\{\{\{\s*(.*?)\s*\}\}\}/s' => '<?php echo htmlentities($1) ?>',
            '/\{\{(\s*.*?)\s*\}\}/s'     => '<?php echo $1 ?>',
        ];
    }

    /**
     * protected keywords<commit_msg>The use of the "$this" object has been added in the views.<commit_after><?php namespace ZN;
/**
 * ZN PHP Web Framework
 * 
 * "Simplicity is the ultimate sophistication." ~ Da Vinci
 * 
 * @package ZN
 * @license MIT [http://opensource.org/licenses/MIT]
 * @author  <NAME> [<EMAIL>]
 */

class Wizard
{
    /**
     * CRLF
     * 
     * @const string
     */
    const CRLF = '\s*(\n|' . PHP_EOL . '|\:|$)';

    /**
     * Get config
     * 
     * @var array
     */
    protected static $config;

    /**
     * Keeps true codes
     * 
     * @var array
     */
    protected static $keepTrueCodes;

    /**
     * PHP tag isolation
     * 
     * @param string $data
     * 
     * @return void
     */
	public static function isolation(String $data = '')
	{
		Filesystem::replaceData($data, ['<?php', '<?', '?>'], ['{[', '{[', ']}']);
	}

    /**
     * Get data.
     * 
     * @param string $string
     * @param array  $data   = []
     * @param array  $config = []
     * 
     * @return string
     */
    public static function data(String $string, Array $data = [], Array $config = []) : String
    {
        self::$config = $config ?: Config::get('ViewObjects', 'wizard');

        self::callableJS($string);

        $code = self::convertWizardContent($string);

        self::convertTrueCode($code);

        return Buffering::code($code, $data);
    }

    /**
     * Protected convert true code
     */
    protected static function convertTrueCode(&$code)
    {
        if( ! empty(self::$keepTrueCodes) )
        {
            $code = str_replace(array_keys(self::$keepTrueCodes), array_values(self::$keepTrueCodes), $code);
        }
    }

    /**
     * Protected convert wizard content
     */
    protected static function convertWizardContent($string)
    {
        self::textControl($string); # 5.4.6[added]
        
        $pattern = array_merge
        (
            self::changeVariables(),
            self::symbolsHeader(),
            self::tags(),
            self::keywords(),
            self::printable(),
            self::functions(),
            self::symbolsFooter(),
            self::comments(),
            self::required(),
            self::jsdata(),
            self::html()
        );

        return self::replace($pattern, $string);
    }

    /**
     * protected text control.
     * 
     * [changed]5.7.5.1
     * [fixed  ]5.7.5.6|5.7.5.7
     * 
     * @param string &$string
     * 
     * @return void
     */
    protected static function textControl(&$string)
    {   
        self::internalTextControl('style' , ['#' => '/#', '@' => '/@', ':' => '/:'], $string);
        self::internalTextControl('script', ['#' => '/#'], $string);
    }
    
    /**
     * Protected internal text control
     * 
     * [added]5.7.5.7
     */
    protected static function internalTextControl($type, $controls, &$string)
    {
        # If the use of custom html is obvious or if the page uses an internal style or code
        preg_match_all('/(\<|\#)'.$type.'(?<data>.*?)(\<\/|\#\#)'.$type.'/si', $string, $standart);

        $patterns = $standart['data'];

        if( ! empty($patterns) ) 
        {
            $changes = [];

            $keys   = array_keys($controls);
            $values = array_values($controls);

            foreach( $patterns as $pattern )
            {
                $changes[] = str_replace($keys, $values, $pattern);
            }

            $string = str_replace($patterns, $changes, $string);
        } 
    }

    /**
     * protected replace.
     * 
     * @param array  $pattern
     * @param string $string
     * 
     * @return string
     */
    protected static function replace($pattern, $string)
    {
        return preg_replace(array_keys($pattern), array_values($pattern), $string);
    }

    /**
     * protected required
     * 
     * @param void
     * 
     * @return array
     */
    protected static function required()
    {
        return
        [
            '/\{\{\{\s*(.*?)\s*\}\}\}/s' => '<?php echo htmlentities($1) ?>',
            '/\{\{(\s*.*?)\s*\}\}/s'     => '<?php echo $1 ?>',
        ];
    }

    /**
     * Protected change variables
     */
    protected static function changeVariables()
    {
        return 
        [
            '/\$this/' => '$instance'
        ];
    }

    /**
     * protected keywords
{'fraction_non_alphanumeric': 0.17530306631804135, 'fraction_numerical': 0.004991680532445923, 'mean_word_length': 1.7635467980295567, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '2870415', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2521, 'n_words': 513}