Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TString.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\String\StringHelper;

class TString
{
    public $string; // String or Array of Strings
    
    public function __construct($str = '')
    {
        $this->string = $str;
    }

    public function set_string($str)
    {
        $this->string = $str;
    }
    
    // $data - string or array of string
    public function process($function, $params = [])
    {
        // $params -> upto two
        
        switch ($function)
        {
            // One argument - data, no parameter
            case 'strtolower' :
            case 'utf8_strtolower' :
            case 'strtoupper' :
            case 'utf8_strtoupper' :
            case 'ucfirst' :
            case 'utf8_ucfirst' :
            case 'lcfirst' :
            case 'utf8_lcfirst' :
            case 'ucwords' :
            case 'utf8_ucwords' : 
            case 'strlen' :
            case 'utf8_strlen' :
            case 'trim' :
            case 'utf8_trim' :
            case 'ltrim' :
            case 'utf8_ltrim' :
            case 'rtrim' :
            case 'utf8_rtrim' :
            case 'strrev' :
            case 'utf8_strrev' :
            case 'str_word_count' :
            case 'htmlspecialchars_decode' :
            case 'htmlspecialchars' :
            case 'parse_url' :
            case 'str_getcsv' :
            case 'str_shuffle' :
            case 'strip_tags' :
            case 'base64_decode' :
            case 'base64_encode' :
            case 'urldecode' :
            case 'urlencode' :
            case 'html_entity_decode' :
            case 'htmlentities' :
            case 'addslashes' :
            case 'stripslashes' :
            case 'bin2hex' :
            case 'hex2bin' :
            case 'count_chars' :
            case 'md5' :
            case 'nl2br' :
            case 'str_decrement' :
            case 'str_increment' :
                $result = $this->one($function);
                break;
                
            // Two arguments - data, 1 parameter
            case 'str_repeat' :
            case 'strtok' :
            case 'rand' :
            case 'str_contains' :
            case 'str_ends_with' :
            case 'str_starts_with' :
            case 'strstr' :
            case 'stristr' :
            case 'utf8_stristr' :
            case 'number_format' :
            case 'str_split' :
                $result = $this->two($function, $params[0]);
                break;
            
            // Three arguments - data, 2 parameters
            case 'substr' :
            case 'substr_replace' :
            case 'utf8_substr' :
            case 'wordwrap' :
            case 'str_pad' :
            case 'chunk_split' :
                $result = $this->three($function, $params[0], $params[1]);
                break;
                
            // String as second argument
            case 'explode' : // Returns array
            case 'preg_match' :
            case 'hash' :
                $result = $function($params[0], $this->string);
                break;
                
            // String as third arguument
            case 'str_replace' :
            case 'str_ireplace' :
            case 'preg_replace' :
                $result = $function($params[0], $params[1], $this->string);
                break;

            // Special Case
            case 'parse_str' :
                parse_str($this->string, $result);
                break;

            // Custom function to generate random string
            case 'str_random' :
                $result = $this->str_random($params[0], $params[1]);
                break;

            case 'pre_concat' :
            case 'post_concat' :
                $result = $this->$function($this->string, $params[0]);
                break;
        }
        
        return $result;
    }
    
    public function one($function)
    {
        if (\is_array($this->string))
        {
            foreach ($this->string as $k => $str)
            {
                $this->string[$k] = $function($str);
            }
        }
        else
        {
            $this->string = $function($this->string);
        }
        
        return $this->string;
    }
    
    public function two($function, $param)
    {
        if (\is_array($this->string))
        {
            foreach ($this->string as $k => $str)
            {
                $this->string[$k] = $function($str, $param);
            }
        }
        else
        {
            $this->string = $function($this->string, $param);
        }
        
        return $this->string;
    }
    
    public function three($function, $param1, $param2)
    {
        if (\is_array($this->string))
        {
            foreach ($this->string as $k => $str)
            {
                $this->string[$k] = $function($str, $param1, $param2);
            }
        }
        else
        {
            $this->string = $function($this->string, $param1, $param2);
        }
        
        return $this->string;
    }

    // Custom Methods
    public function str_random($length = 8, $characters = '0123456789abcdefghijklmnopqrstuvwxyz')
    {
        if (!$characters)
        {
            $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        }
        
        $len = strlen($characters);

        $random = '';
        for ($i = 0; $i < $length; $i++) 
        {
            $random .= $characters[random_int(0, $len - 1)];
        }
        
        return $random;
    }

    public function pre_concat($string, $prefix)
    {
        return $prefix . $string;
    }

    public function post_concat($string, $suffix)
    {
        return $string . $suffix;
    }
}