<?php
    include_once "Reflection.php";
    function isRegex($string)
    {
        $mathes = [];
        $execute = preg_match(
            "/^\/.*\/[imsxADSUXJu]+$/",
            $string,
            $mathes
        );
        return $execute;
    }
    class tstring
    {
        use ReflectionHook;
        protected $data = "";
        static function isstring($data)
        {
            if(gettype($data) == "string")
            {
                return true;
            }
            if(gettype($data) == "object")
            {
                return method_exists($data, "__toString");
            }
            return false;
        }
        static function from($data)
        {
            return new tstring($data);
        }
        public function __construct($data = "")
        {
            $this->data = $data;
        }
        public function getLengthAttribute()
        {
            return strlen($this->data);
        }
        public function at($index) : string
        {
            return $this->data[$index] ?? "";
        }
        public function charAt($index) : string
        {
            return $this->at($index);
        }
        public function concat(...$string) : tstring
        {
            return new tstring(
                implode('',[
                    $this->data,
                    ...$string
                ])
            );
        }
        public function endsWith($string) : bool
        {
            if(PHP_MAJOR_VERSION >= 8)
            {
                return str_ends_with($this->data, $string);
            };
            $size = strlen($string);
            if(substr($this->data, -$size) == $string)
            {
                return true;
            }else{
                return false;
            }
        }
        public function startsWith($string) : bool
        {
            if(PHP_MAJOR_VERSION >= 8)
            {
                return str_starts_with($this->data, $string);
            };
            $size = strlen($string);
            if(substr($this->data, 0, $size) == $string)
            {
                return true;
            }else{
                return false;
            }
        }
        public function match($pattern) : array|bool
        {
            $mathes = [];
            $matched = preg_match(
                $pattern,
                $this->data,
                $mathes
            );
            return $matched ? $mathes : false;
        }
        public function matchAll($pattern) : array
        {
            $mathes = [];
            preg_match_all(
                $pattern,
                $this->data,
                $mathes
            );
            return $mathes;
        }
        public function padStart($length, $padString = " ")
        {
            return new tstring(
                str_pad(
                    $this->data,
                    $length,
                    $padString,
                    STR_PAD_RIGHT
                )
            );
        }
        public function padEnd($length, $padString = " ")
        {
            return new tstring(
                str_pad(
                    $this->data,
                    $length,
                    $padString,
                    STR_PAD_LEFT
                )
            );
        }
        public function repeat($count)
        {
            return new tstring(
                str_repeat(
                    $this->data,
                    $count
                )
            );
        }
        public function replace($pattern, $subject = "")
        {
            if(gettype($pattern) == "array")
            {
                $a = [];
                $b = [];
                foreach ($pattern as $key => $value) {
                    $a[] = $key;
                    $b[] = $value ?? "";
                };
                return new tstring(
                    str_replace($a, $b, $this->data)
                );
            }else if(gettype($pattern) == "string" && isRegex($pattern))
            {
                return new tstring(
                    preg_replace(
                        $pattern,
                        $subject,
                        $this->data
                    )
                );
            }else{
                return new tstring(
                    str_replace(
                        $pattern,
                        $subject,
                        $this->data
                    )
                );
            }
        }
        public function ireplace($pattern, $subject = "")
        {
            if(gettype($pattern) == "array")
            {
                $a = [];
                $b = [];
                foreach ($pattern as $key => $value) {
                    $a[] = $key;
                    $b[] = $value ?? "";
                };
                return new tstring(
                    str_ireplace($a, $b, $this->data)
                );
            }else if(gettype($pattern) == "string" && isRegex($pattern))
            {
                return new tstring(
                    preg_replace(
                        $pattern,
                        $subject,
                        $this->data
                    )
                );
            }else{
                return new tstring(
                    str_ireplace(
                        $pattern,
                        $subject,
                        $this->data
                    )
                );
            }
        }
        public function search($string)
        {
            return strpos($this->data, $string);
        }
        public function isearch($string)
        {
            return stripos($this->data, $string);
        }
        public function slice($start = 0, $end = null)
        {
            return new tstring(
                substr(
                    $this->data,
                    $start,
                    $end
                )
            );
        }
        public function split($delimiter)
        {
            return array_map(
                function($e){
                    return new tstring($e);
                },
                explode($delimiter, $this->data)
            );
        }
        public function splitjoin($delimiter, $glue)
        {
            return new tstring(
                implode(
                    $glue,
                    explode($delimiter, $this->data)
                )
            );
        }
        public function wrap($delimiter, $glue, $callback)
        {
            return new tstring(
                implode(
                    $glue,
                    array_map(
                        function($item) use($callback)
                        {
                            return $callback(new tstring($item));
                        },
                        explode($delimiter, $this->data)
                    )
                )
            );
        }
        public function toLowerCase()
        {
            return new tstring(
                strtolower($this->data)
            );
        }
        public function toUpperCase()
        {
            return new tstring(
                strtoupper($this->data)
            );
        }
        public function trim()
        {
            return new tstring(
                trim($this->data)
            );
        }
        public function __toString()
        {
            return $this->data;
        }
        public function raw()
        {
            return $this->data;
        }
    }
    function dd($data){
        highlight_string("<?=\n\n" . var_export($data, true) . "\n\n?>");
        die();
    }

    function includeAllSubFiles($path)
    {
        $files = scandir($path);
        foreach ($files as $file) {
            if(in_array($file,['.','..']))
            {
                continue;
            }
            $filepath = realpath("$path/$file");
            require_once $filepath;
        }
    }

    class Helper
    {
        public static function slugify($text) {
            // Türkçe karakterleri Latin alfabesi karakterlerine dönüştür
            $text = str_replace(array('ı', 'İ', 'ş', 'Ş', 'ğ', 'Ğ', 'ü', 'Ü', 'ö', 'Ö', 'ç', 'Ç'), 
                                array('i', 'i', 's', 's', 'g', 'g', 'u', 'u', 'o', 'o', 'c', 'c'), $text);
          
            // Unicode karakterleri değiştir
            $text = preg_replace('/\p{M}/u', '', $text);
            
            // Boşlukları tire ile değiştir
            $text = str_replace(' ', '-', $text);
            
            // Küçük harfe dönüştür
            $text = strtolower($text);
            
            return $text;
        }
    }