Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- var_dump(new DataTypeChar('a'));
- ?>
- object(ILLI\System\DataTypeChar)#2 (11) {
- ["__dec":"ILLI\System\DataTypeChar":private]=>
- int(97)
- ["__dechex":"ILLI\System\DataTypeChar":private]=>
- string(2) "61"
- ["__uni":"ILLI\System\DataTypeChar":private]=>
- int(97)
- ["__unihex":"ILLI\System\DataTypeChar":private]=>
- string(2) "61"
- ["__codePoint":"ILLI\System\DataTypeChar":private]=>
- string(6) "U+0061"
- ["__bin":"ILLI\System\DataTypeChar":private]=>
- string(8) "01100001"
- ["__isMultibyte":"ILLI\System\DataTypeString":private]=>
- bool(false)
- ["__width":"ILLI\System\DataTypeString":private]=>
- NULL
- ["__length":"ILLI\System\DataTypeString":private]=>
- NULL
- ["__value":protected]=>
- string(1) "a"
- ["__initialValue":"ILLI\System\DataType":private]=>
- string(1) "a"
- }
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI_System
- * @package ILLI
- * @subpackage System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- NAMESPACE ILLI\System;
- /**
- * ILLI System Abstract Prototype
- *
- * @category ILLI_System
- * @package ILLI
- * @subpackage System
- * @namespace ILLI\System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- * @since 2.0.0-1
- * @version 2.0.0-1
- * @abstract
- */
- CLASS DataTypeString EXTENDS DataType IMPLEMENTS iDataTypeString
- {
- USE tDataTypeString;
- private $__isMultibyte = NULL;
- private $__width = NULL;
- private $__length = NULL;
- public function __construct($stringValue = NULL, Bits $BITS = NULL)
- {
- if(NULL !== $stringValue
- && FALSE === ($stringValue instanceOf DataTypeString)
- && FALSE === is_string($stringValue))
- throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
- if($stringValue instanceOf DataTypeString)
- $stringValue = $stringValue->get();
- parent::__construct($stringValue);
- }
- public function set($value)
- {
- if(NULL === $value)
- return $this;
- $value = ($value instanceOf DataTypeString)
- ? $value->get()
- : $value;
- $value = (TRUE === is_string($value))
- ? $this->normalize($value)
- : $value;
- parent::set($value);
- $this->__isMultibyte = $this->isMultiByte($this->__value);
- return $this;
- }
- public static function detectEncoding($value)
- {
- return mb_detect_encoding($value);
- }
- public static function isMultiByte($value)
- {
- return mb_strlen($value, DataType::ENCODING) !== strlen($value);
- }
- public static function normalize($value)
- {
- return (self::detectEncoding($value) !== DataType::ENCODING)
- ? mb_convert_encoding($value, DataType::ENCODING)
- : $value;
- }
- public function isNull()
- {
- return NULL === $this->__value;
- }
- public function trim()
- {
- $this->__value = preg_replace('/^[\pZ|\pC]+([\PZ|\PC]*)[\pZ|\pC]+$/u', '$1', $this->__value);
- return $this;
- }
- public function __toString()
- {
- return $this->__value;
- }
- public function width()
- {
- return mb_strwidth($this->__value, DataType::ENCODING);
- }
- public function length()
- {
- return mb_strlen($this->__value, DataType::ENCODING);
- }
- public function toCharArray()
- {
- $strlen = $this->length();
- $value = $this->__value;
- $result = [];
- while($strlen > 0)
- {
- $result[] = mb_substr($value, 0, 1, DataType::ENCODING);
- $value = mb_substr($value, 1, $strlen, DataType::ENCODING);
- $strlen = mb_strlen($value);
- }
- return new DataTypeCharArray($result);
- }
- }
- CLASS DataTypeChar EXTENDS DataTypeString IMPLEMENTS iDataTypeValueAccessSet
- {
- const CODE_POINT = 'U+';
- private $__dec = 0;
- private $__dechex = 0x0;
- private $__uni = 0;
- private $__unihex = 0x0;
- private $__codePoint = 0;
- private $__bin = 00000000;
- public function __construct($stringValue = NULL, Bits $BITS = NULL)
- {
- if(NULL !== $stringValue
- && FALSE === ($stringValue instanceOf DataTypeString)
- && FALSE === is_string($stringValue))
- throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
- if($stringValue instanceOf DataTypeChar)
- $stringValue = $stringValue->get();
- parent::__construct($stringValue);
- }
- public function set($value)
- {
- if(NULL === $value)
- return $this;
- $value = ($value instanceOf DataTypeString)
- ? $value->get()
- : $value;
- parent::set($value);
- if($this->length() !== 1)
- throw new ArgumentException(E::ARGUMENT_EXPECTED_CHAR_LENGTH_INVALID, ['string' => $this->__value]);
- $this->__dec = $this->dec($this->__value);
- $this->__bin = str_pad(decbin($this->__dec), 8, '0', STR_PAD_LEFT);
- $this->__dechex = dechex($this->__dec);
- $this->__uni = $this->uni($value);
- $this->__unihex = dechex($this->__uni);
- $this->__codePoint = self::CODE_POINT.str_pad(dechex($this->__uni), 4, '0', STR_PAD_LEFT);
- return $this;
- }
- public static function uni($c)
- {
- $ord0 = ord($c{0});
- if($ord0 >= 0 && $ord0 <= 127)
- return $ord0;
- $ord1 = ord($c{1});
- if($ord0>=192 && $ord0<=223)
- return ($ord0 - 192) * 64 + ($ord1 - 128);
- $ord2 = ord($c{2});
- if($ord0 >= 224 && $ord0 <= 239)
- return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
- $ord3 = ord($c{3});
- if($ord0 >= 240 && $ord0 <= 247)
- return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
- return false;
- }
- public static function dec($char)
- {
- $index = 0;
- $result = '';
- while(isset($char{$index}))
- {
- $result.= ord($char{$index});
- ++$index;
- }
- return (int) $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement