PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Jabiru/Component/Element/ElementLiteral.php

https://gitlab.com/src-run/scrible
PHP | 310 lines | 134 code | 39 blank | 137 comment | 9 complexity | f035a82b4343395f8c3fbc76385c624a MD5 | raw file
  1. <?php
  2. namespace Scribe\Jabiru\Component\Element;
  3. use Scribe\Jabiru\Component\Collection\Collection;
  4. class ElementLiteral implements \Serializable
  5. {
  6. /**
  7. * @var string
  8. */
  9. private $text;
  10. /**
  11. * @param string $text
  12. */
  13. public function __construct($text = '')
  14. {
  15. $this->text = (string) $text;
  16. }
  17. /**
  18. * @param string $text
  19. *
  20. * @return Text
  21. */
  22. public function append($text)
  23. {
  24. $this->text .= (string) $text;
  25. return $this;
  26. }
  27. /**
  28. * @param string $text
  29. *
  30. * @return Text
  31. */
  32. public function prepend($text)
  33. {
  34. $this->text = (string) $text . $this->text;
  35. return $this;
  36. }
  37. /**
  38. * Surround text with given string
  39. *
  40. * @param string $start
  41. * @param string $end
  42. *
  43. * @return Text
  44. */
  45. public function wrap($start, $end)
  46. {
  47. $this->text = (string) $start . $this->text . $end;
  48. return $this;
  49. }
  50. /**
  51. * Make a string lowercase
  52. *
  53. * @return Text
  54. */
  55. public function lower()
  56. {
  57. $this->text = strtolower($this->text);
  58. return $this;
  59. }
  60. /**
  61. * Make a string uppercase
  62. *
  63. * @return Text
  64. */
  65. public function upper()
  66. {
  67. $this->text = strtoupper($this->text);
  68. return $this;
  69. }
  70. /**
  71. * Strip whitespace (or other characters) from the beginning and end of a string
  72. *
  73. * @param string $charList Optionally, the stripped characters can also be specified using the charlist parameter.
  74. * Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
  75. *
  76. * @return Text
  77. */
  78. public function trim($charList = null)
  79. {
  80. if (is_null($charList)) {
  81. $this->text = trim($this->text);
  82. } else {
  83. $this->text = trim($this->text, $charList);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Strip whitespace (or other characters) from the end of a string
  89. *
  90. * @param string $charList You can also specify the characters you want to strip, by means of the charlist parameter.
  91. * Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
  92. *
  93. * @return Text
  94. */
  95. public function rtrim($charList = null)
  96. {
  97. if (is_null($charList)) {
  98. $this->text = rtrim($this->text);
  99. } else {
  100. $this->text = rtrim($this->text, $charList);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * @param string $charList You can also specify the characters you want to strip, by means of the charlist parameter.
  106. * Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
  107. *
  108. * @return Text
  109. */
  110. public function ltrim($charList = null)
  111. {
  112. if (is_null($charList)) {
  113. $this->text = ltrim($this->text);
  114. } else {
  115. $this->text = ltrim($this->text, $charList);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * @return bool
  121. */
  122. public function isEmpty()
  123. {
  124. return $this->text === '' ? true : false;
  125. }
  126. /**
  127. * Checks whether the text contains $text
  128. *
  129. * @param string $text A string to test
  130. *
  131. * @return bool True if the text contains $text
  132. */
  133. public function contains($text)
  134. {
  135. return strpos($this->text, $text) !== false ? true : false;
  136. }
  137. /**
  138. * Convert special characters to HTML entities
  139. *
  140. * @param int $option
  141. *
  142. * @return Text
  143. */
  144. public function escapeHtml($option = ENT_QUOTES)
  145. {
  146. $this->text = htmlspecialchars($this->text, $option, 'UTF-8', false);
  147. return $this;
  148. }
  149. /**
  150. * Perform a regular expression search and replace
  151. *
  152. * @param string $pattern The pattern to search for. It can be either a string or an array with strings.
  153. * @param string|callable $replacement The string or an array with strings to replace.
  154. * If $replacement is the callable, a callback that will be called and passed an array of matched elements in the subject string.
  155. *
  156. * @return Text
  157. */
  158. public function replace($pattern, $replacement)
  159. {
  160. if (is_callable($replacement)) {
  161. $this->text = preg_replace_callback($pattern, function ($matches) use ($replacement) {
  162. $args = array_map(function ($item) {
  163. return new ElementLiteral($item);
  164. }, $matches);
  165. return call_user_func_array($replacement, $args);
  166. }, $this->text);
  167. } else {
  168. $this->text = preg_replace($pattern, $replacement, $this->text);
  169. }
  170. return $this;
  171. }
  172. /**
  173. * Replace all occurrences of the search string with the replacement string
  174. *
  175. * @param string|array $search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
  176. * @param string|array $replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
  177. *
  178. * @return Text
  179. */
  180. public function replaceString($search, $replace)
  181. {
  182. $this->text = str_replace($search, $replace, $this->text);
  183. return $this;
  184. }
  185. /**
  186. * Perform a regular expression match
  187. *
  188. * @param string $pattern The pattern to search for, as a string.
  189. * @param array|null $matches If matches is provided, then it is filled with the results of search.
  190. *
  191. * @return boolean
  192. */
  193. public function match($pattern, &$matches = null)
  194. {
  195. return (bool) preg_match($pattern, $this->text, $matches) > 0 ? true : false;
  196. }
  197. /**
  198. * Split string by a regular expression
  199. *
  200. * @param string $pattern The pattern to search for, as a string.
  201. * @param int $flags [optional] PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE
  202. *
  203. * @return Text[]|Collection
  204. */
  205. public function split($pattern, $flags = PREG_SPLIT_DELIM_CAPTURE)
  206. {
  207. return new Collection(
  208. array_map(function ($item) {
  209. return new static($item);
  210. }, preg_split($pattern, $this->text, -1, $flags))
  211. );
  212. }
  213. /**
  214. * Gets the length of a string
  215. *
  216. * @return int Returns the number of characters in string str having character encoding encoding.
  217. * A multi-byte character is counted as 1.
  218. */
  219. public function getLength()
  220. {
  221. if (function_exists('mb_strlen')) {
  222. return mb_strlen($this->text, 'UTF-8');
  223. }
  224. // @codeCoverageIgnoreStart
  225. return preg_match_all("/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/", $this->text);
  226. // @codeCoverageIgnoreEnd
  227. }
  228. /**
  229. * Set internal string value
  230. *
  231. * @param Text
  232. */
  233. public function setString($text)
  234. {
  235. $this->text = (string) $text;
  236. }
  237. /**
  238. * Returns internal string value
  239. *
  240. * @return string
  241. */
  242. public function getString()
  243. {
  244. return $this->text;
  245. }
  246. /**
  247. * @return string
  248. */
  249. public function __toString()
  250. {
  251. return $this->getString();
  252. }
  253. /**
  254. * String representation of object
  255. *
  256. * @return string the string representation of the object or null
  257. */
  258. public function serialize()
  259. {
  260. return serialize($this->text);
  261. }
  262. /**
  263. * Constructs the object
  264. *
  265. * @param string $serialized The string representation of the object.
  266. */
  267. public function unserialize($serialized)
  268. {
  269. $this->text = unserialize($serialized);
  270. }
  271. }