Advertisement
fruffl

Untitled

May 12th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @subpackage System
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\System;
  13.  
  14.     /**
  15.      * ILLI System Abstract Prototype
  16.      *
  17.      * @category   ILLI_System
  18.      * @package    ILLI
  19.      * @subpackage System
  20.      * @namespace  ILLI\System
  21.      * @link       http://illi.be
  22.      * @license    http://l.illi.be
  23.      * @copyright  ILLI Conference
  24.      * @since      2.0.0-1
  25.      * @version    2.0.0-1
  26.      * @abstract
  27.      */
  28.     CLASS DataTypeString EXTENDS DataType IMPLEMENTS iDataTypeString
  29.     {
  30.         USE tDataTypeString;
  31.        
  32.         private $__isMultibyte  = NULL;
  33.         private $__width    = NULL;
  34.         private $__length   = NULL;
  35.        
  36.         public function __construct($stringValue = NULL, Bits $BITS = NULL)
  37.         {
  38.             if(NULL !== $stringValue
  39.             && FALSE === ($stringValue instanceOf DataTypeString)
  40.             && FALSE === is_string($stringValue))
  41.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  42.            
  43.            
  44.             if($stringValue instanceOf DataTypeString)
  45.                 $stringValue = $stringValue->get();
  46.            
  47.             parent::__construct($stringValue);
  48.         }
  49.        
  50.         public function set($value)
  51.         {
  52.             if(NULL === $value)
  53.                 return $this;
  54.                
  55.             $value = ($value instanceOf DataTypeString)
  56.                 ? $value->get()
  57.                 : $value;
  58.                
  59.             $value = (TRUE === is_string($value))
  60.                 ? $this->normalize($value)
  61.                 : $value;
  62.            
  63.             parent::set($value);
  64.            
  65.             $this->__isMultibyte = $this->isMultiByte($this->__value);
  66.            
  67.             return $this;
  68.         }
  69.        
  70.         public static function detectEncoding($value)
  71.         {
  72.             return mb_detect_encoding($value);
  73.         }
  74.        
  75.         public static function isMultiByte($value)
  76.         {
  77.             return mb_strlen($value, DataType::ENCODING) !== strlen($value);
  78.         }
  79.        
  80.         public static function normalize($value)
  81.         {
  82.             return (self::detectEncoding($value) !== DataType::ENCODING)
  83.                 ? mb_convert_encoding($value, DataType::ENCODING)
  84.                 : $value;
  85.         }
  86.        
  87.         public function isNull()
  88.         {
  89.             return NULL === $this->__value;
  90.         }
  91.        
  92.         public function trim()
  93.         {
  94.             $this->__value = preg_replace('/^[\pZ|\pC]+([\PZ|\PC]*)[\pZ|\pC]+$/u', '$1', $this->__value);
  95.             return $this;
  96.         }
  97.        
  98.         public function __toString()
  99.         {
  100.             return $this->__value;
  101.         }
  102.        
  103.         public function width()
  104.         {
  105.             return mb_strwidth($this->__value, DataType::ENCODING);
  106.         }
  107.        
  108.         public function length()
  109.         {
  110.             return mb_strlen($this->__value, DataType::ENCODING);
  111.         }
  112.        
  113.         public function toCharArray()
  114.         {
  115.             $strlen = $this->length();
  116.             $value  = $this->__value;
  117.            
  118.             $result = [];
  119.            
  120.             while($strlen > 0)
  121.             {
  122.                 $result[]   = mb_substr($value, 0, 1,   DataType::ENCODING);
  123.                 $value      = mb_substr($value, 1, $strlen, DataType::ENCODING);
  124.                 $strlen     = mb_strlen($value);
  125.             }
  126.            
  127.             return new DataTypeCharArray($result);
  128.         }
  129.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement