. */ /** * StringType object. * * Wraps around Multibyte string extension, offering multi-byte safe * string functions. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->trim()->replace('!', '.')->lower(); * * * @since 4.6.0 * @package Type */ namespace Textpattern\Type; class StringType implements TypeInterface { /** * The string. * * @var string */ protected $string; /** * Whether multibyte string extension is available. * * @var bool */ protected static $mbString = null; /** * Whether encoding functions are available. * * @var bool */ protected static $encode = null; /** * Expected encoding. * * @var string */ protected $encoding = 'UTF-8'; /** * Constructor. * * @param string $string The string */ public function __construct($string) { $this->string = (string)$string; if (self::$mbString === null) { self::$mbString = function_exists('mb_strlen'); } if (self::$encode === null) { self::$encode = function_exists('utf8_decode'); } } /** * Gets the string. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!'); * * * @return string * @see \Textpattern\Type\String::getString() */ public function __toString() { return (string)$this->string; } /** * Gets the string. * * * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->getString(); * * * @return string * @see \Textpattern\Type\String::_toString() */ public function getString() { return (string)$this->string; } /** * Gets string length. * * * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->getLength(); * * * @return int */ public function getLength() { if (self::$mbString) { return mb_strlen($this->string, $this->encoding); } if (self::$encode) { return strlen(utf8_decode($this->string)); } return strlen($this->string); } /** * Finds the first occurrence of a string in the string. * * * echo Txp::get('\Textpattern\Type\StringType', '#@language')->position('@'); * * * @param string $needle The string to find * @param int $offset The search offset * @return int|bool FALSE if the string does not contain results */ public function position($needle, $offset = 0) { if (self::$mbString) { return mb_strpos($this->string, $needle, $offset, $this->encoding); } return strpos($this->string, $needle, $offset); } /** * Gets substring count. * * * echo Txp::get('\Textpattern\Type\StringType', 'Hello World!')->count('ello'); * * * @param string $needle The string to find * @return int */ public function count($needle) { if (self::$mbString) { return mb_substr_count($this->string, $needle, $this->encoding); } return substr_count($this->string, $needle); } /** * Converts the string to a callback. * * * Txp::get('\Textpattern\Type\StringType', '\Textpattern\Password\Hash->hash')->toCallback(); * * * @return mixed Callable */ public function toCallback() { $callback = $this->string; if (strpos($this->string, '->')) { $callback = explode('->', $this->string); if (class_exists($callback[0])) { $callback[0] = new $callback[0]; } } elseif (strpos($this->string, '::')) { $callback = explode('::', $this->string); } return $callback; } /** * Add slashes. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Some "content" to slash.')->addSlashes(); * * * @return StringType */ public function addSlashes() { $this->string = addslashes($this->string); return $this; } /** * HTML encodes the string. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->html(); * * * @param int $flags A bitmask of one or more flags. The default is ENT_QUOTES * @param bool $double_encode When double_encode is turned off PHP will not encode existing HTML entities, the default is to convert everything * @return StringType */ public function html($flags = ENT_QUOTES, $double_encode = true) { $this->string = htmlspecialchars($this->string, $flags, $this->encoding, $double_encode); return $this; } /** * Splits part of the string. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->substring(2, 5); * * * @param int $start The start * @param int $length The length * @return StringType */ public function substring($start, $length = null) { if (self::$mbString) { $this->string = mb_substr($this->string, $start, $length, $this->encoding); } else { $this->string = substr($this->string, $start, $length); } return $this; } /** * Replaces all occurrences with replacements. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->replace('!', '.'); * * * @param mixed $from The needle to find * @param mixed $to The replacement * @return StringType */ public function replace($from, $to) { $this->string = str_replace($from, $to, $this->string); return $this; } /** * Translates substrings. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Article {title} deleted.') * ->tr('{title}', 'Hello {title} variable.'); * * * @param string $from StringType to find * @param string $to The replacement * @return StringType */ public function tr($from, $to = null) { $this->string = strtr($this->string, $from, $to); return $this; } /** * Trims surrounding whitespace or other characters. * * * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->trim(); * * * @param string $characters Character list * @return StringType */ public function trim($characters = "\t\n\r\0\x0B") { $this->string = trim($this->string, $characters); return $this; } /** * Trims whitespace or other characters from the beginning. * * * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->ltrim(); * * * @param string $characters Character list * @return StringType */ public function ltrim($characters = "\t\n\r\0\x0B") { $this->string = ltrim($this->string, $characters); return $this; } /** * Trims whitespace or other characters from the end. * * * echo (string) Txp::get('\Textpattern\Type\StringType', ' Hello World! ')->rtrim(); * * * @param string $characters Character list * @return StringType */ public function rtrim($characters = "\t\n\r\0\x0B") { $this->string = rtrim($this->string, $characters); return $this; } /** * Splits string to chunks. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->chunk(1); * * * @param int $length The chunk length * @param string $delimiter The delimiter * @return StringType */ public function chunk($length = 76, $delimiter = n) { $this->string = chunk_split($this->string, $length, $delimiter); return $this; } /** * Word wraps the string. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->wordWrap(); * * * @param int $length The line length * @param string $delimiter The line delimiter * @param bool $cut Cut off words * @return StringType */ public function wordWrap($length = 75, $delimiter = n, $cut = false) { $this->string = wordwrap($this->string, $length, $delimiter, $cut); return $this; } /** * Converts the string to lowercase. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->lower(); * * * @return StringType */ public function lower() { if (self::$mbString) { $this->string = mb_strtolower($this->string, $this->encoding); } else { $this->string = strtolower($this->string); } return $this; } /** * Converts the string to uppercase. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->upper(); * * * @return StringType */ public function upper() { if (self::$mbString) { $this->string = mb_strtoupper($this->string, $this->encoding); } else { $this->string = strtoupper($this->string); } return $this; } /** * Converts the string to titlecase. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'hello world!')->title(); * * * @return StringType */ public function title() { if (self::$mbString) { $this->string = mb_convert_case($this->string, MB_CASE_TITLE, $this->encoding); } else { $this->string = ucwords($this->string); } return $this; } /** * Uppercase the first letter. * * * echo (string) Txp::get('\Textpattern\Type\StringType', 'Hello World!')->ucfirst(); * * * @return StringType */ public function ucfirst() { if (self::$mbString) { $this->string = mb_strtoupper(mb_substr($this->string, 0, 1, $this->encoding), $this->encoding). mb_substr($this->string, 1, null, $this->encoding); } else { $this->string = ucfirst($this->string); } return $this; } }