Advertisement
fruffl

Char 'a'

Apr 25th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <?PHP
  2.     var_dump(new DataTypeChar('a'));
  3. ?>
  4.  
  5. object(ILLI\System\DataTypeChar)#2 (11) {
  6.   ["__dec":"ILLI\System\DataTypeChar":private]=>
  7.   int(97)
  8.   ["__dechex":"ILLI\System\DataTypeChar":private]=>
  9.   string(2) "61"
  10.   ["__uni":"ILLI\System\DataTypeChar":private]=>
  11.   int(97)
  12.   ["__unihex":"ILLI\System\DataTypeChar":private]=>
  13.   string(2) "61"
  14.   ["__codePoint":"ILLI\System\DataTypeChar":private]=>
  15.   string(6) "U+0061"
  16.   ["__bin":"ILLI\System\DataTypeChar":private]=>
  17.   string(8) "01100001"
  18.   ["__isMultibyte":"ILLI\System\DataTypeString":private]=>
  19.   bool(false)
  20.   ["__width":"ILLI\System\DataTypeString":private]=>
  21.   NULL
  22.   ["__length":"ILLI\System\DataTypeString":private]=>
  23.   NULL
  24.   ["__value":protected]=>
  25.   string(1) "a"
  26.   ["__initialValue":"ILLI\System\DataType":private]=>
  27.   string(1) "a"
  28. }
  29.  
  30. <?PHP
  31.  
  32.     /**
  33.      * ILLI
  34.      *
  35.      * @category   ILLI_System
  36.      * @package    ILLI
  37.      * @subpackage System
  38.      * @link       http://illi.be
  39.      * @license    http://l.illi.be
  40.      * @copyright  ILLI Conference
  41.      */
  42.     NAMESPACE ILLI\System;
  43.  
  44.     /**
  45.      * ILLI System Abstract Prototype
  46.      *
  47.      * @category   ILLI_System
  48.      * @package    ILLI
  49.      * @subpackage System
  50.      * @namespace  ILLI\System
  51.      * @link       http://illi.be
  52.      * @license    http://l.illi.be
  53.      * @copyright  ILLI Conference
  54.      * @since      2.0.0-1
  55.      * @version    2.0.0-1
  56.      * @abstract
  57.      */
  58.     CLASS DataTypeString EXTENDS DataType IMPLEMENTS iDataTypeString
  59.     {
  60.         USE tDataTypeString;
  61.        
  62.         private $__isMultibyte  = NULL;
  63.         private $__width    = NULL;
  64.         private $__length   = NULL;
  65.        
  66.         public function __construct($stringValue = NULL, Bits $BITS = NULL)
  67.         {
  68.             if(NULL !== $stringValue
  69.             && FALSE === ($stringValue instanceOf DataTypeString)
  70.             && FALSE === is_string($stringValue))
  71.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  72.            
  73.            
  74.             if($stringValue instanceOf DataTypeString)
  75.                 $stringValue = $stringValue->get();
  76.            
  77.             parent::__construct($stringValue);
  78.         }
  79.        
  80.         public function set($value)
  81.         {
  82.             if(NULL === $value)
  83.                 return $this;
  84.                
  85.             $value = ($value instanceOf DataTypeString)
  86.                 ? $value->get()
  87.                 : $value;
  88.                
  89.             $value = (TRUE === is_string($value))
  90.                 ? $this->normalize($value)
  91.                 : $value;
  92.            
  93.             parent::set($value);
  94.            
  95.             $this->__isMultibyte = $this->isMultiByte($this->__value);
  96.            
  97.             return $this;
  98.         }
  99.        
  100.         public static function detectEncoding($value)
  101.         {
  102.             return mb_detect_encoding($value);
  103.         }
  104.        
  105.         public static function isMultiByte($value)
  106.         {
  107.             return mb_strlen($value, DataType::ENCODING) !== strlen($value);
  108.         }
  109.        
  110.         public static function normalize($value)
  111.         {
  112.             return (self::detectEncoding($value) !== DataType::ENCODING)
  113.                 ? mb_convert_encoding($value, DataType::ENCODING)
  114.                 : $value;
  115.         }
  116.        
  117.         public function isNull()
  118.         {
  119.             return NULL === $this->__value;
  120.         }
  121.        
  122.         public function trim()
  123.         {
  124.             $this->__value = preg_replace('/^[\pZ|\pC]+([\PZ|\PC]*)[\pZ|\pC]+$/u', '$1', $this->__value);
  125.             return $this;
  126.         }
  127.        
  128.         public function __toString()
  129.         {
  130.             return $this->__value;
  131.         }
  132.        
  133.         public function width()
  134.         {
  135.             return mb_strwidth($this->__value, DataType::ENCODING);
  136.         }
  137.        
  138.         public function length()
  139.         {
  140.             return mb_strlen($this->__value, DataType::ENCODING);
  141.         }
  142.        
  143.         public function toCharArray()
  144.         {
  145.             $strlen = $this->length();
  146.             $value  = $this->__value;
  147.            
  148.             $result = [];
  149.            
  150.             while($strlen > 0)
  151.             {
  152.                 $result[]   = mb_substr($value, 0, 1,   DataType::ENCODING);
  153.                 $value      = mb_substr($value, 1, $strlen, DataType::ENCODING);
  154.                 $strlen     = mb_strlen($value);
  155.             }
  156.            
  157.             return new DataTypeCharArray($result);
  158.         }
  159.     }
  160.  
  161.     CLASS DataTypeChar EXTENDS DataTypeString IMPLEMENTS iDataTypeValueAccessSet
  162.     {
  163.         const CODE_POINT    = 'U+';
  164.        
  165.         private $__dec      = 0;
  166.         private $__dechex   = 0x0;
  167.         private $__uni      = 0;
  168.         private $__unihex   = 0x0;
  169.         private $__codePoint    = 0;
  170.         private $__bin      = 00000000;
  171.        
  172.         public function __construct($stringValue = NULL, Bits $BITS = NULL)
  173.         {
  174.             if(NULL !== $stringValue
  175.             && FALSE === ($stringValue instanceOf DataTypeString)
  176.             && FALSE === is_string($stringValue))
  177.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  178.            
  179.             if($stringValue instanceOf DataTypeChar)
  180.                 $stringValue = $stringValue->get();
  181.            
  182.             parent::__construct($stringValue);
  183.         }
  184.        
  185.         public function set($value)
  186.         {
  187.             if(NULL === $value)
  188.                 return $this;
  189.                
  190.             $value = ($value instanceOf DataTypeString)
  191.                 ? $value->get()
  192.                 : $value;
  193.                
  194.             parent::set($value);
  195.            
  196.             if($this->length() !== 1)
  197.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_CHAR_LENGTH_INVALID, ['string' => $this->__value]);
  198.                
  199.             $this->__dec        = $this->dec($this->__value);
  200.             $this->__bin        = str_pad(decbin($this->__dec), 8, '0', STR_PAD_LEFT);
  201.             $this->__dechex     = dechex($this->__dec);
  202.             $this->__uni        = $this->uni($value);
  203.             $this->__unihex     = dechex($this->__uni);
  204.             $this->__codePoint  = self::CODE_POINT.str_pad(dechex($this->__uni), 4, '0', STR_PAD_LEFT);
  205.            
  206.             return $this;
  207.         }
  208.        
  209.         public static function uni($c)
  210.         {
  211.             $ord0 = ord($c{0});
  212.            
  213.             if($ord0 >= 0 && $ord0 <= 127)
  214.                 return $ord0;
  215.                
  216.             $ord1 = ord($c{1});
  217.            
  218.             if($ord0>=192 && $ord0<=223)
  219.                 return ($ord0 - 192) * 64 + ($ord1 - 128);
  220.                
  221.             $ord2 = ord($c{2});
  222.            
  223.             if($ord0 >= 224 && $ord0 <= 239)
  224.                 return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  225.                
  226.             $ord3 = ord($c{3});
  227.            
  228.             if($ord0 >= 240 && $ord0 <= 247)
  229.                 return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
  230.            
  231.             return false;
  232.         }
  233.        
  234.         public static function dec($char)
  235.         {
  236.             $index  = 0;
  237.             $result = '';
  238.            
  239.             while(isset($char{$index}))
  240.             {
  241.                     $result.= ord($char{$index});
  242.                     ++$index;
  243.                 }
  244.                
  245.                 return (int) $result;
  246.         }
  247.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement