Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Type/StringType.php - 492 lines - 11708 bytes - Summary - Text - Print

Description: StringType object.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * StringType object.
  26   *
  27   * Wraps around Multibyte string extension, offering multi-byte safe
  28   * string functions.
  29   *
  30   * <code>
  31   * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->trim()->replace('!', '.')->lower();
  32   * </code>
  33   *
  34   * @since   4.6.0
  35   * @package Type
  36   */
  37  
  38  namespace Textpattern\Type;
  39  
  40  class StringType implements TypeInterface
  41  {
  42      /**
  43       * The string.
  44       *
  45       * @var string
  46       */
  47  
  48      protected $string;
  49  
  50      /**
  51       * Whether multibyte string extension is available.
  52       *
  53       * @var bool
  54       */
  55  
  56      protected static $mbString = null;
  57  
  58      /**
  59       * Whether encoding functions are available.
  60       *
  61       * @var bool
  62       */
  63  
  64      protected static $encode = null;
  65  
  66      /**
  67       * Expected encoding.
  68       *
  69       * @var string
  70       */
  71  
  72      protected $encoding = 'UTF-8';
  73  
  74      /**
  75       * Constructor.
  76       *
  77       * @param string $string The string
  78       */
  79  
  80      public function __construct($string)
  81      {
  82          $this->string = (string)$string;
  83  
  84          if (self::$mbString === null) {
  85              self::$mbString = function_exists('mb_strlen');
  86          }
  87  
  88          if (self::$encode === null) {
  89              self::$encode = function_exists('utf8_decode');
  90          }
  91      }
  92  
  93      /**
  94       * Gets the string.
  95       *
  96       * <code>
  97       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!');
  98       * </code>
  99       *
 100       * @return string
 101       * @see    \Textpattern\Type\String::getString()
 102       */
 103  
 104      public function __toString()
 105      {
 106          return (string)$this->string;
 107      }
 108  
 109      /**
 110       * Gets the string.
 111       *
 112       * <code>
 113       * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->getString();
 114       * </code>
 115       *
 116       * @return string
 117       * @see    \Textpattern\Type\String::_toString()
 118       */
 119  
 120      public function getString()
 121      {
 122          return (string)$this->string;
 123      }
 124  
 125      /**
 126       * Gets string length.
 127       *
 128       * <code>
 129       * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->getLength();
 130       * </code>
 131       *
 132       * @return int
 133       */
 134  
 135      public function getLength()
 136      {
 137          if (self::$mbString) {
 138              return mb_strlen($this->string, $this->encoding);
 139          }
 140  
 141          if (self::$encode) {
 142              return strlen(utf8_decode($this->string));
 143          }
 144  
 145          return strlen($this->string);
 146      }
 147  
 148      /**
 149       * Finds the first occurrence of a string in the string.
 150       *
 151       * <code>
 152       * echo Txp::get('\Textpattern\Type\StringType', '#@language')->position('@');
 153       * </code>
 154       *
 155       * @param  string $needle The string to find
 156       * @param  int    $offset The search offset
 157       * @return int|bool FALSE if the string does not contain results
 158       */
 159  
 160      public function position($needle, $offset = 0)
 161      {
 162          if (self::$mbString) {
 163              return mb_strpos($this->string, $needle, $offset, $this->encoding);
 164          }
 165  
 166          return strpos($this->string, $needle, $offset);
 167      }
 168  
 169      /**
 170       * Gets substring count.
 171       *
 172       * <code>
 173       * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->count('ello');
 174       * </code>
 175       *
 176       * @param  string $needle The string to find
 177       * @return int
 178       */
 179  
 180      public function count($needle)
 181      {
 182          if (self::$mbString) {
 183              return mb_substr_count($this->string, $needle, $this->encoding);
 184          }
 185  
 186          return substr_count($this->string, $needle);
 187      }
 188  
 189      /**
 190       * Converts the string to a callback.
 191       *
 192       * <code>
 193       * Txp::get('\Textpattern\Type\StringType', '\Textpattern\Password\Hash->hash')->toCallback();
 194       * </code>
 195       *
 196       * @return mixed Callable
 197       */
 198  
 199      public function toCallback()
 200      {
 201          $callback = $this->string;
 202  
 203          if (strpos($this->string, '->')) {
 204              $callback = explode('->', $this->string);
 205  
 206              if (class_exists($callback[0])) {
 207                  $callback[0] = new $callback[0];
 208              }
 209          } elseif (strpos($this->string, '::')) {
 210              $callback = explode('::', $this->string);
 211          }
 212  
 213          return $callback;
 214      }
 215  
 216      /**
 217       * Add slashes.
 218       *
 219       * <code>
 220       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Some "content" to slash.')->addSlashes();
 221       * </code>
 222       *
 223       * @return StringType
 224       */
 225  
 226      public function addSlashes()
 227      {
 228          $this->string = addslashes($this->string);
 229  
 230          return $this;
 231      }
 232  
 233      /**
 234       * HTML encodes the string.
 235       *
 236       * <code>
 237       * echo (string) Txp::get('\Textpattern\Type\StringType', '<strong>Hello World!</strong>')->html();
 238       * </code>
 239       *
 240       * @param  int  $flags         A bitmask of one or more flags. The default is ENT_QUOTES
 241       * @param  bool $double_encode When double_encode is turned off PHP will not encode existing HTML entities, the default is to convert everything
 242       * @return StringType
 243       */
 244  
 245      public function html($flags = ENT_QUOTES, $double_encode = true)
 246      {
 247          $this->string = htmlspecialchars($this->string, $flags, $this->encoding, $double_encode);
 248  
 249          return $this;
 250      }
 251  
 252      /**
 253       * Splits part of the string.
 254       *
 255       * <code>
 256       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->substring(2, 5);
 257       * </code>
 258       *
 259       * @param  int $start  The start
 260       * @param  int $length The length
 261       * @return StringType
 262       */
 263  
 264      public function substring($start, $length = null)
 265      {
 266          if (self::$mbString) {
 267              $this->string = mb_substr($this->string, $start, $length, $this->encoding);
 268          } else {
 269              $this->string = substr($this->string, $start, $length);
 270          }
 271  
 272          return $this;
 273      }
 274  
 275      /**
 276       * Replaces all occurrences with replacements.
 277       *
 278       * <code>
 279       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->replace('!', '.');
 280       * </code>
 281       *
 282       * @param  mixed $from The needle to find
 283       * @param  mixed $to   The replacement
 284       * @return StringType
 285       */
 286  
 287      public function replace($from, $to)
 288      {
 289          $this->string = str_replace($from, $to, $this->string);
 290  
 291          return $this;
 292      }
 293  
 294      /**
 295       * Translates substrings.
 296       *
 297       * <code>
 298       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Article <strong>{title}</strong> deleted.')
 299       *     ->tr('{title}', 'Hello {title} variable.');
 300       * </code>
 301       *
 302       * @param  string $from StringType to find
 303       * @param  string $to   The replacement
 304       * @return StringType
 305       */
 306  
 307      public function tr($from, $to = null)
 308      {
 309          $this->string = strtr($this->string, $from, $to);
 310  
 311          return $this;
 312      }
 313  
 314      /**
 315       * Trims surrounding whitespace or other characters.
 316       *
 317       * <code>
 318       * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->trim();
 319       * </code>
 320       *
 321       * @param  string $characters Character list
 322       * @return StringType
 323       */
 324  
 325      public function trim($characters = "\t\n\r\0\x0B")
 326      {
 327          $this->string = trim($this->string, $characters);
 328  
 329          return $this;
 330      }
 331  
 332      /**
 333       * Trims whitespace or other characters from the beginning.
 334       *
 335       * <code>
 336       * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->ltrim();
 337       * </code>
 338       *
 339       * @param  string $characters Character list
 340       * @return StringType
 341       */
 342  
 343      public function ltrim($characters = "\t\n\r\0\x0B")
 344      {
 345          $this->string = ltrim($this->string, $characters);
 346  
 347          return $this;
 348      }
 349  
 350      /**
 351       * Trims whitespace or other characters from the end.
 352       *
 353       * <code>
 354       * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->rtrim();
 355       * </code>
 356       *
 357       * @param  string $characters Character list
 358       * @return StringType
 359       */
 360  
 361      public function rtrim($characters = "\t\n\r\0\x0B")
 362      {
 363          $this->string = rtrim($this->string, $characters);
 364  
 365          return $this;
 366      }
 367  
 368      /**
 369       * Splits string to chunks.
 370       *
 371       * <code>
 372       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->chunk(1);
 373       * </code>
 374       *
 375       * @param  int    $length    The chunk length
 376       * @param  string $delimiter The delimiter
 377       * @return StringType
 378       */
 379  
 380      public function chunk($length = 76, $delimiter = n)
 381      {
 382          $this->string = chunk_split($this->string, $length, $delimiter);
 383  
 384          return $this;
 385      }
 386  
 387      /**
 388       * Word wraps the string.
 389       *
 390       * <code>
 391       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->wordWrap();
 392       * </code>
 393       *
 394       * @param  int    $length    The line length
 395       * @param  string $delimiter The line delimiter
 396       * @param  bool   $cut       Cut off words
 397       * @return StringType
 398       */
 399  
 400      public function wordWrap($length = 75, $delimiter = n, $cut = false)
 401      {
 402          $this->string = wordwrap($this->string, $length, $delimiter, $cut);
 403  
 404          return $this;
 405      }
 406  
 407      /**
 408       * Converts the string to lowercase.
 409       *
 410       * <code>
 411       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->lower();
 412       * </code>
 413       *
 414       * @return StringType
 415       */
 416  
 417      public function lower()
 418      {
 419          if (self::$mbString) {
 420              $this->string = mb_strtolower($this->string, $this->encoding);
 421          } else {
 422              $this->string = strtolower($this->string);
 423          }
 424  
 425          return $this;
 426      }
 427  
 428      /**
 429       * Converts the string to uppercase.
 430       *
 431       * <code>
 432       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->upper();
 433       * </code>
 434       *
 435       * @return StringType
 436       */
 437  
 438      public function upper()
 439      {
 440          if (self::$mbString) {
 441              $this->string = mb_strtoupper($this->string, $this->encoding);
 442          } else {
 443              $this->string = strtoupper($this->string);
 444          }
 445  
 446          return $this;
 447      }
 448  
 449      /**
 450       * Converts the string to titlecase.
 451       *
 452       * <code>
 453       * echo (string) Txp::get('\Textpattern\Type\StringType', 'hello world!')->title();
 454       * </code>
 455       *
 456       * @return StringType
 457       */
 458  
 459      public function title()
 460      {
 461          if (self::$mbString) {
 462              $this->string = mb_convert_case($this->string, MB_CASE_TITLE, $this->encoding);
 463          } else {
 464              $this->string = ucwords($this->string);
 465          }
 466  
 467          return $this;
 468      }
 469  
 470      /**
 471       * Uppercase the first letter.
 472       *
 473       * <code>
 474       * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->ucfirst();
 475       * </code>
 476       *
 477       * @return StringType
 478       */
 479  
 480      public function ucfirst()
 481      {
 482          if (self::$mbString) {
 483              $this->string =
 484                  mb_strtoupper(mb_substr($this->string, 0, 1, $this->encoding), $this->encoding).
 485                  mb_substr($this->string, 1, null, $this->encoding);
 486          } else {
 487              $this->string = ucfirst($this->string);
 488          }
 489  
 490          return $this;
 491      }
 492  }

title

Description

title

Description

title

Description

title

title

Body