Welcome Guest, Not a member yet? Register   Sign In
Suggestion: Class to handle Strings
#1

A few times over the time I have worked with CodeIgniter on projects that involves East languages (chinese and japanese) ...
In most of the time there were no issues related to those languages but at a few cases where I had to make some "strange things", there were some problems related to using mb_ and not mb_ string functions at the framework. 

So for CI 4 instead of default using both mb_substr or substr I want to suggest making a class to do all the work.
It can be 1class only (I think this is enough) or it can be Abstract class (with all required methods) + Childs (Multibyte / Normal / other..) where 1 of the childs will be autoloaded using the config.
Best VPS Hosting : Digital Ocean
Reply
#2

And that class would do ... what?
Reply
#3

(This post was last modified: 09-09-2015, 02:22 AM by sv3tli0.)

Simple CI will use it at all places where string methods are required.
Developers from their hands will be able to Switch between Normal Strings methods, multibytes or whatever they want .
By this the Framework will be some steps more Open for the developers.

If for some reasons dev's don't want to use mb_ or they want to use only mb_ , they will be able just to switch between does 2 ..
If some one wants he can make even a mix where some of the methods are mb_ some are not mb_ and some are entirely custom written methods..

+ Such library can be and extended to provide some extra methods to developers in 1 place.

My believe is that 1 good Framework should provide options to devs, not to force them.

P.S. This will remove PHP extension dependency of the framework (MB) and at the same time will support it.

I don't know how will work exactly the autoloader and other things at CI 4, but such class can have both : static and none static method support.
So $CI->string can be preloaded and at the same time clients can call it using its namespace/Class::method()
Best VPS Hosting : Digital Ocean
Reply
#4

I've got a class that supports at least UTF-8 compatible string methods, ported a long time ago from Kohana. Maybe sv3tli0 means something similar. https://github.com/ivantcholakov/codeigniter-utf8
Reply
#5

(This post was last modified: 09-10-2015, 03:47 AM by sv3tli0.)

Here is an example:


PHP Code:
<?php


class StringLib
{
 
   // This should be Container not library variable..
 
   // But for tests is fine..
 
   static $lib;

 
   public function __construct($lib)
 
   {
 
       return $this->testChangeLib($lib);
 
   }

 
   // Method just for tests
 
   public static function testChangeLibStatic$lib )
 
   {
 
       $className $lib get_called_class();
 
       ifclass_exists$className ))
 
       {
 
           static::$lib = new $className;
 
       }
 
   }

 
   // Method just for tests
 
   public function testChangeLib$lib ) : StringLib
    
{
 
       $className $lib get_called_class();
 
       ifclass_exists$className ))
 
       {
 
           static::$lib = new $className;
 
           return $this;
 
       }
 
   }


 
   public function __callstring $method, array $args = [])
 
   {
 
       ifmethod_exists(static::$lib$method))
 
       {
 
           return call_user_func_array( array(static::$lib$method), $args);
 
       }
 
   }

 
   public static function __callStaticstring $method, array $args  = [])
 
   {
 
       ifmethod_exists(static::$lib$method))
 
       {
 
           return call_user_func_array(array(static::$lib$method), $args);
 
       }
 
   }
}

interface 
StringInterface
{
 
   public function strlenstring $string) : int;
}



class 
PHPStringLib implements StringInterface
{
 
   public function strlenstring $string) : int
    
{
 
       return strlen($string);
 
   }
}


class 
MBStringLib implements StringInterface
{
    
// This File may have error trigger if MB is disabled
 
   // This should be configurable
 
   static $charset 'UTF-8';

 
   public function strlenstring $string) : int
    
{
 
       return mb_strlen$stringself::$charset);
 
   }
}


class 
CustomStringLib
{
 
   public function strlen($string) : int
    
{
 
       // Just another way which again uses str_ function..
 
       return count(str_split($string));
 
   }
}

$teststring 'One two три';

$test = new StringLib'MB' );
echo 
"\n" $test->strlen$teststring ) . "\n";
$test->testChangeLib'PHP' );
echo 
"\n" $test->strlen$teststring ) . "\n";
$test->testChangeLib'Custom' );
echo 
"\n" $test->strlen$teststring ) . "\n";

StringLib::testChangeLibStatic('MB');
echo 
"\n" StringLib::strlen$teststring ) . "\n";
StringLib::testChangeLibStatic('PHP');
echo 
"\n" StringLib::strlen$teststring ) . "\n";
StringLib::testChangeLibStatic('Custom');
echo 
"\n" StringLib::strlen$teststring ) . "\n"

(clear view at gist > https://gist.github.com/sv3tli0/76ea9e81ee73abcc271d )
At the end client will have the best way to work with strings.
Best VPS Hosting : Digital Ocean
Reply
#6

(This post was last modified: 09-10-2015, 03:40 PM by ivantcholakov. Edit Reason: a typo )

@sv3tli0

It could be done with autodetection, the class could check for mb_string (faster, but less supported encoding), iconv (slower, more supported encodings), intl (I think, it has multibyte routines too, but they are likely not well documented). If none exists, then fallback PHP implementation (by method) could be done. For every method the best possible way can be picked up automatically.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB