Advertisement
fruffl

Untitled

Sep 16th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.57 KB | None | 0 0
  1. <?PHP
  2.  
  3. interface String_Interface
  4. {
  5.     public function toUpper();
  6.     public function toLower();
  7.     public function length();
  8.     public function compareTo(String $string,
  9.                               Boolean $casesensitive = NULL);
  10.     public function trim(String $replace = NULL);
  11.     public function trimStart(String $replace = NULL);
  12.     public function trimEnd(String $replace = NULL);
  13.     public function leftJoin(String $string,
  14.                              String $separator = NULL);
  15.     public function rightJoin(String $string,
  16.                               String $separator = NULL);
  17.     public function insert(Integer $start,
  18.                            String $string);
  19.     public function padLeft(Integer $chars,
  20.                             String $string = NULL);
  21.     public function padRight(Integer $chars,
  22.                              String $string = NULL);
  23.     public function contains(String $string,
  24.                              Boolean $casesensitive = NULL);
  25.     public function endsWith(String $string,
  26.                              Boolean $casesensitive = NULL);
  27.     public function startsWith(String $string,
  28.                                Boolean $casesensitive = NULL);
  29.     public function indexOf(String $string,
  30.                             Boolean $casesensitive = NULL);
  31.     public function lastIndexOf(String $string,
  32.                                 Boolean $casesensitive = NULL);
  33.     public function replace(String $search,
  34.                             String $replace = NULL);
  35.     public function split(String $string = NULL);
  36.     public function remove(Integer $start,
  37.                            Integer $length = NULL);
  38. }
  39. class String
  40.         extends Object
  41.         implements String_Interface
  42. {
  43.     const Blank = '';
  44.     public function equals(String $string) {
  45.         return parent::_equals($string);
  46.     }
  47.     public function isUtf8() {
  48.         $value = $this->get(); return new Boolean((bool) (utf8_encode(utf8_decode($value)) == $value));
  49.     }
  50.     public function isEmpty() {
  51.         $value = $this->get(); return new Boolean((bool) empty($value));
  52.     }
  53.     public function isWhitespace() {
  54.         $value = trim($this->get(),
  55.                       ' '); return new Boolean((bool) empty($value));
  56.     }
  57.     public function length() {
  58.         return new Integer(strlen($this->get()));
  59.     }
  60.     public function toUpper() {
  61.         return $this->setCopy(strtoupper($this->get()));
  62.     }
  63.     public function toLower() {
  64.         return $this->setCopy(strtolower($this->get()));
  65.     }
  66.     public function toBoolean() {
  67.         return new Boolean((bool) $this->get());
  68.     }
  69.     public function set($value) {
  70.         if(NULL === $value)
  71.             throw new ArgumentNullException;
  72.         if(!is_string($value))
  73.             throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
  74.         parent::set((string) $value);
  75.         return $this;
  76.     }
  77.     public function compareTo(String $string,
  78.                               Boolean $casesensitive = NULL) {
  79.         return new Boolean(($casesensitive === NULL || $casesensitive->get() === FALSE)
  80.                                 ? ($this->get() === $string->get())
  81.                                 : ($this->getTemp()->toLower()->get() === $string->getTemp()->toLower()->get()));
  82.     }
  83.     public function trim(String $replace = NULL) {
  84.         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  85.                                 ? trim($this->get())
  86.                                 : trim($this->get(),
  87.                                        $replace->get()));
  88.     }
  89.     public function trimStart(String $replace = NULL) {
  90.         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  91.                                 ? ltrim($this->get())
  92.                                 : ltrim($this->get(),
  93.                                         $replace->get()));
  94.     }
  95.     public function trimEnd(String $replace = NULL) {
  96.         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  97.                                 ? rtrim($this->get())
  98.                                 : rtrim($this->get(),
  99.                                         $replace->get()));
  100.     }
  101.     public function leftJoin(String $string,
  102.                              String $separator = NULL) {
  103.         if(TRUE === $string->isEmpty()->get())
  104.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  105.         return $this->setCopy(($separator === NULL)
  106.                                 ? ($string->get().$this->get())
  107.                                 : ($string->get().$separator->get().$this->get()));
  108.     }
  109.     public function rightJoin(String $string,
  110.                               String $separator = NULL) {
  111.         if(TRUE === $string->isEmpty()->get())
  112.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  113.         return $this->setCopy(($separator === NULL)
  114.                                 ? ($this->get().$string->get())
  115.                                 : ($this->get().$separator->get().$string->get()));
  116.     }
  117.     public function insert(Integer $start,
  118.                            String $string) {
  119.         if(TRUE === $string->isEmpty()->get())
  120.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  121.  
  122.         try
  123.         {
  124.             if($start->get() < 0)
  125.                 throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
  126.         }
  127.         catch(ArgumentOutOfRangeException $e)
  128.         {
  129.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
  130.         }
  131.  
  132.         $value = $this->get();
  133.         return $this->setCopy(substr($value,
  134.                                      0,
  135.                                      $start->get()).$string->get().substr($value,
  136.                                                                           $start->get(),
  137.                                                                           $this->length()->get()));
  138.     }
  139.     /**
  140.      * @todo str_pad is fuzzy. rewrite this
  141.      */
  142.     public function padLeft(Integer $chars,
  143.                             String $string = NULL) {
  144.         try
  145.         {
  146.             if($chars->get() < 0)
  147.                 throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
  148.         }
  149.         catch(ArgumentOutOfRangeException $e)
  150.         {
  151.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
  152.         }
  153.  
  154.         return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
  155.                                 ? (str_pad($this->get(),
  156.                                            $chars->get(),
  157.                                            ' ',
  158.                                            STR_PAD_LEFT))
  159.                                 : (str_pad($this->get(),
  160.                                            $chars->get(),
  161.                                            $string->get(),
  162.                                            STR_PAD_LEFT)));
  163.     }
  164.     /**
  165.      * @todo str_pad is fuzzy. rewrite this
  166.      */
  167.     public function padRight(Integer $chars,
  168.                              String $string = NULL) {
  169.         try
  170.         {
  171.             if($chars->get() < 0)
  172.                 throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);
  173.         }
  174.         catch(ArgumentOutOfRangeException $e)
  175.         {
  176.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);
  177.         }
  178.  
  179.         return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
  180.                                 ? (str_pad($this->get(),
  181.                                            $chars->get(),
  182.                                            ' ',
  183.                                            STR_PAD_RIGHT))
  184.                                 : (str_pad($this->get(),
  185.                                            $chars->get(),
  186.                                            $string->get(),
  187.                                            STR_PAD_RIGHT)));
  188.     }
  189.     public function contains(String $string,
  190.                              Boolean $casesensitive = NULL) {
  191.         if(TRUE === $string->isEmpty()->get())
  192.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  193.         return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
  194.                                 ? (preg_match('#'.$string->get().'#msie',
  195.                                               $this->get()))
  196.                                 : (preg_match('#'.$string->get().'#mse',
  197.                                               $this->get()))));
  198.     }
  199.     public function endsWith(String $string,
  200.                              Boolean $casesensitive = NULL) {
  201.         if(TRUE === $string->isEmpty()->get())
  202.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  203.         return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
  204.                                 ? ($string->get() == substr($this->get(),
  205.                                                             $this->length()->get() - $string->length()->get()))
  206.                                 : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(),
  207.                                                                                   $this->length()->get() - $string->length()->get()))));
  208.     }
  209.     public function startsWith(String $string,
  210.                                Boolean $casesensitive = NULL) {
  211.         if(TRUE === $string->isEmpty()->get())
  212.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  213.         return new Boolean((bool) (($casesensitive === NULL || $casesensitive->get() === FALSE)
  214.                                 ? ($string->get() == substr($this->get(),
  215.                                                             0,
  216.                                                             $string->length()->get()))
  217.                                 : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(),
  218.                                                                                   0,
  219.                                                                                   $string->length()->get()))));
  220.     }
  221.     /**
  222.      * @todo returns IntegerArray. how to map the results?
  223.      */
  224.     public function indexOf(String $string,
  225.                             Boolean $casesensitive = NULL) {
  226.         throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
  227.     }
  228.     public function lastIndexOf(String $string,
  229.                                 Boolean $casesensitive = NULL) {
  230.         if(TRUE === $string->isEmpty()->get())
  231.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  232.  
  233.         $result = ($casesensitive === NULL || $casesensitive->get() === FALSE)
  234.                 ? (strrpos($this->get(),
  235.                            $string->get()))
  236.                 : (strrpos($this->getTemp()->toLower()->get(),
  237.                            $string->getTemp()->toLower()->get()));
  238.  
  239.         try
  240.         {
  241.             return new Integer($result);
  242.         }
  243.         catch(DatatypeExpectedException $e)
  244.         {
  245.             return new Boolean($result);
  246.         }
  247.     }
  248.     public function replace(String $search,
  249.                             String $replace = NULL) {
  250.         if(TRUE === $search->isEmpty()->get())
  251.             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  252.         return $this->setCopy(($replace === NULL || $replace->isEmpty() === TRUE)
  253.                                 ? (str_replace($search->get(),
  254.                                                '',
  255.                                                $this->get()))
  256.                                 : (str_replace($search->get(),
  257.                                                $replace->get(),
  258.                                                $this->get())));
  259.     }
  260.     /**
  261.      * @todo check split-pattern
  262.      */
  263.     public function split(String $string = NULL) {
  264.         return new StringArray(($string === NULL || $string->isEmpty()->get() === TRUE)
  265.                                 ? (preg_split('/(?<!^)(?!$)/u',
  266.                                               $this->get()))
  267.                                 : (explode($string->get(),
  268.                                            $this->get())));
  269.     }
  270.     /**
  271.      * @todo returns IntegerArray. how to map the results?
  272.      */
  273.     public function remove(Integer $start,
  274.                            Integer $length = NULL) {
  275.         throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
  276.     }
  277.     public function __construct($value = self::Blank) {
  278.         try
  279.         {
  280.             try
  281.             {
  282.                 parent::__construct($value);
  283.             }
  284.             catch(DatatypeExpectedException $e)
  285.             {
  286.                 throw new DatatypeExpectedException($e->getCode());
  287.             }
  288.             catch(ArgumentNullException $e)
  289.             {
  290.                 throw new ArgumentNullException($e->getCode());
  291.             }
  292.         }
  293.         catch(SystemException $e)
  294.         {
  295.             throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);
  296.         }
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement