Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- interface String_Interface
- {
- public function toUpper();
- public function toLower();
- public function length();
- public function compareTo(String $string,
- Boolean $casesensitive = NULL);
- public function trim(String $replace = NULL);
- public function trimStart(String $replace = NULL);
- public function trimEnd(String $replace = NULL);
- public function leftJoin(String $string,
- String $separator = NULL);
- public function rightJoin(String $string,
- String $separator = NULL);
- public function insert(Integer $start,
- String $string);
- public function padLeft(Integer $chars,
- String $string = NULL);
- public function padRight(Integer $chars,
- String $string = NULL);
- public function contains(String $string,
- Boolean $casesensitive = NULL);
- public function endsWith(String $string,
- Boolean $casesensitive = NULL);
- public function startsWith(String $string,
- Boolean $casesensitive = NULL);
- public function indexOf(String $string,
- Boolean $casesensitive = NULL);
- public function lastIndexOf(String $string,
- Boolean $casesensitive = NULL);
- public function replace(String $search,
- String $replace = NULL);
- public function split(String $string = NULL);
- public function remove(Integer $start,
- Integer $length = NULL);
- }
- class String
- extends Object
- implements String_Interface
- {
- const Blank = '';
- public function equals(String $string) {
- return parent::_equals($string);
- }
- public function isUtf8() {
- $value = $this->get(); return new Boolean((bool) (utf8_encode(utf8_decode($value)) == $value));
- }
- public function isEmpty() {
- $value = $this->get(); return new Boolean((bool) empty($value));
- }
- public function isWhitespace() {
- $value = trim($this->get(),
- ' '); return new Boolean((bool) empty($value));
- }
- public function length() {
- return new Integer(strlen($this->get()));
- }
- public function toUpper() {
- return $this->setCopy(strtoupper($this->get()));
- }
- public function toLower() {
- return $this->setCopy(strtolower($this->get()));
- }
- public function toBoolean() {
- return new Boolean((bool) $this->get());
- }
- public function set($value) {
- if(NULL === $value)
- throw new ArgumentNullException;
- if(!is_string($value))
- throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
- parent::set((string) $value);
- return $this;
- }
- public function compareTo(String $string,
- Boolean $casesensitive = NULL) {
- return new Boolean(($casesensitive === NULL || $casesensitive->get() === FALSE)
- ? ($this->get() === $string->get())
- : ($this->getTemp()->toLower()->get() === $string->getTemp()->toLower()->get()));
- }
- public function trim(String $replace = NULL) {
- return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
- ? trim($this->get())
- : trim($this->get(),
- $replace->get()));
- }
- public function trimStart(String $replace = NULL) {
- return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
- ? ltrim($this->get())
- : ltrim($this->get(),
- $replace->get()));
- }
- public function trimEnd(String $replace = NULL) {
- return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
- ? rtrim($this->get())
- : rtrim($this->get(),
- $replace->get()));
- }
- public function leftJoin(String $string,
- String $separator = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return $this->setCopy(($separator === NULL)
- ? ($string->get().$this->get())
- : ($string->get().$separator->get().$this->get()));
- }
- public function rightJoin(String $string,
- String $separator = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return $this->setCopy(($separator === NULL)
- ? ($this->get().$string->get())
- : ($this->get().$separator->get().$string->get()));
- }
- public function insert(Integer $start,
- String $string) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- try
- {
- if($start->get() < 0)
- throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
- }
- catch(ArgumentOutOfRangeException $e)
- {
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
- }
- $value = $this->get();
- return $this->setCopy(substr($value,
- 0,
- $start->get()).$string->get().substr($value,
- $start->get(),
- $this->length()->get()));
- }
- /**
- * @todo str_pad is fuzzy. rewrite this
- */
- public function padLeft(Integer $chars,
- String $string = NULL) {
- try
- {
- if($chars->get() < 0)
- throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
- }
- catch(ArgumentOutOfRangeException $e)
- {
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
- }
- return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
- ? (str_pad($this->get(),
- $chars->get(),
- ' ',
- STR_PAD_LEFT))
- : (str_pad($this->get(),
- $chars->get(),
- $string->get(),
- STR_PAD_LEFT)));
- }
- /**
- * @todo str_pad is fuzzy. rewrite this
- */
- public function padRight(Integer $chars,
- String $string = NULL) {
- try
- {
- if($chars->get() < 0)
- throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
- }
- catch(ArgumentOutOfRangeException $e)
- {
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
- }
- return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
- ? (str_pad($this->get(),
- $chars->get(),
- ' ',
- STR_PAD_RIGHT))
- : (str_pad($this->get(),
- $chars->get(),
- $string->get(),
- STR_PAD_RIGHT)));
- }
- public function contains(String $string,
- Boolean $casesensitive = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
- ? (preg_match('#'.$string->get().'#msie',
- $this->get()))
- : (preg_match('#'.$string->get().'#mse',
- $this->get()))));
- }
- public function endsWith(String $string,
- Boolean $casesensitive = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
- ? ($string->get() == substr($this->get(),
- $this->length()->get() - $string->length()->get()))
- : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(),
- $this->length()->get() - $string->length()->get()))));
- }
- public function startsWith(String $string,
- Boolean $casesensitive = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
- ? ($string->get() == substr($this->get(),
- 0,
- $string->length()->get()))
- : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(),
- 0,
- $string->length()->get()))));
- }
- /**
- * @todo returns IntegerArray. how to map the results?
- */
- public function indexOf(String $string,
- Boolean $casesensitive = NULL) {
- throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
- }
- public function lastIndexOf(String $string,
- Boolean $casesensitive = NULL) {
- if(TRUE === $string->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- $result = ($casesensitive === NULL || $casesensitive->get() === FALSE)
- ? (strrpos($this->get(),
- $string->get()))
- : (strrpos($this->getTemp()->toLower()->get(),
- $string->getTemp()->toLower()->get()));
- try
- {
- return new Integer($result);
- }
- catch(DatatypeExpectedException $e)
- {
- return new Boolean($result);
- }
- }
- public function replace(String $search,
- String $replace = NULL) {
- if(TRUE === $search->isEmpty()->get())
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- return $this->setCopy(($replace === NULL || $replace->isEmpty() === TRUE)
- ? (str_replace($search->get(),
- '',
- $this->get()))
- : (str_replace($search->get(),
- $replace->get(),
- $this->get())));
- }
- /**
- * @todo check split-pattern
- */
- public function split(String $string = NULL) {
- return new StringArray(($string === NULL || $string->isEmpty()->get() === TRUE)
- ? (preg_split('/(?<!^)(?!$)/u',
- $this->get()))
- : (explode($string->get(),
- $this->get())));
- }
- /**
- * @todo returns IntegerArray. how to map the results?
- */
- public function remove(Integer $start,
- Integer $length = NULL) {
- throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
- }
- public function __construct($value = self::Blank) {
- try
- {
- try
- {
- parent::__construct($value);
- }
- catch(DatatypeExpectedException $e)
- {
- throw new DatatypeExpectedException($e->getCode());
- }
- catch(ArgumentNullException $e)
- {
- throw new ArgumentNullException($e->getCode());
- }
- }
- catch(SystemException $e)
- {
- throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement