Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement