String.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Helper_String
  25. * @brief Helper class for string handling.
  26. */
  27. class TeamSpeak3_Helper_String implements ArrayAccess, Iterator, Countable, JsonSerializable
  28. {
  29. /**
  30. * Stores the original string.
  31. *
  32. * @var string
  33. */
  34. protected $string;
  35. /**
  36. * @ignore
  37. */
  38. protected $position = 0;
  39. /**
  40. * The TeamSpeak3_Helper_String constructor.
  41. *
  42. * @param string $string
  43. * @return TeamSpeak3_Helper_String
  44. */
  45. public function __construct($string)
  46. {
  47. $this->string = (string) $string;
  48. }
  49. /**
  50. * Returns a TeamSpeak3_Helper_String object for thegiven string.
  51. *
  52. * @param string $string
  53. * @return TeamSpeak3_Helper_String
  54. */
  55. public static function factory($string)
  56. {
  57. return new self($string);
  58. }
  59. /**
  60. * Replaces every occurrence of the string $search with the string $replace.
  61. *
  62. * @param string $search
  63. * @param string $replace
  64. * @param boolean $caseSensitivity
  65. * @return TeamSpeak3_Helper_String
  66. */
  67. public function replace($search, $replace, $caseSensitivity = TRUE)
  68. {
  69. if($caseSensitivity)
  70. {
  71. $this->string = str_replace($search, $replace, $this->string);
  72. }
  73. else
  74. {
  75. $this->string = str_ireplace($search, $replace, $this->string);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * This function replaces indexed or associative signs with given values.
  81. *
  82. * @param array $args
  83. * @param string $char
  84. * @return TeamSpeak3_Helper_String
  85. */
  86. public function arg(array $args, $char = "%")
  87. {
  88. $args = array_reverse($args, TRUE);
  89. foreach($args as $key => $val)
  90. {
  91. $args[$char . $key] = $val;
  92. unset($args[$key]);
  93. }
  94. $this->string = strtr($this->string, $args);
  95. return $this;
  96. }
  97. /**
  98. * Returns true if the string starts with $pattern.
  99. *
  100. * @param string $pattern
  101. * @return boolean
  102. */
  103. public function startsWith($pattern)
  104. {
  105. return (substr($this->string, 0, strlen($pattern)) == $pattern) ? TRUE : FALSE;
  106. }
  107. /**
  108. * Returns true if the string ends with $pattern.
  109. *
  110. * @param string $pattern
  111. * @return boolean
  112. */
  113. public function endsWith($pattern)
  114. {
  115. return (substr($this->string, strlen($pattern)*-1) == $pattern) ? TRUE : FALSE;
  116. }
  117. /**
  118. * Returns the position of the first occurrence of a char in a string.
  119. *
  120. * @param string $needle
  121. * @return integer
  122. */
  123. public function findFirst($needle)
  124. {
  125. return strpos($this->string, $needle);
  126. }
  127. /**
  128. * Returns the position of the last occurrence of a char in a string.
  129. *
  130. * @param string $needle
  131. * @return integer
  132. */
  133. public function findLast($needle)
  134. {
  135. return strrpos($this->string, $needle);
  136. }
  137. /**
  138. * Returns the lowercased string.
  139. *
  140. * @return TeamSpeak3_Helper_String
  141. */
  142. public function toLower()
  143. {
  144. return new self(strtolower($this->string));
  145. }
  146. /**
  147. * Returns the uppercased string.
  148. *
  149. * @return TeamSpeak3_Helper_String
  150. */
  151. public function toUpper()
  152. {
  153. return new self(strtoupper($this->string));
  154. }
  155. /**
  156. * Returns true if the string contains $pattern.
  157. *
  158. * @param string $pattern
  159. * @param boolean $regexp
  160. * @return boolean
  161. */
  162. public function contains($pattern, $regexp = FALSE)
  163. {
  164. if(empty($pattern))
  165. {
  166. return TRUE;
  167. }
  168. if($regexp)
  169. {
  170. return (preg_match("/" . preg_quote($pattern, '/') . "/i", $this->string)) ? TRUE : FALSE;
  171. }
  172. else
  173. {
  174. return (stristr($this->string, $pattern) !== FALSE) ? TRUE : FALSE;
  175. }
  176. }
  177. /**
  178. * Returns part of a string.
  179. *
  180. * @param integer $start
  181. * @param integer $length
  182. * @return TeamSpeak3_Helper_String
  183. */
  184. public function substr($start, $length = null)
  185. {
  186. $string = ($length !== null) ? substr($this->string, $start, $length) : substr($this->string, $start);
  187. return new self($string);
  188. }
  189. /**
  190. * Splits the string into substrings wherever $separator occurs.
  191. *
  192. * @param string $separator
  193. * @param integer $limit
  194. * @return array
  195. */
  196. public function split($separator, $limit = 0)
  197. {
  198. $parts = explode($separator, $this->string, ($limit) ? intval($limit) : $this->count());
  199. foreach($parts as $key => $val)
  200. {
  201. $parts[$key] = new self($val);
  202. }
  203. return $parts;
  204. }
  205. /**
  206. * Appends $part to the string.
  207. *
  208. * @param string $part
  209. * @return TeamSpeak3_Helper_String
  210. */
  211. public function append($part)
  212. {
  213. $this->string = $this->string . strval($part);
  214. return $this;
  215. }
  216. /**
  217. * Prepends $part to the string.
  218. *
  219. * @param string $part
  220. * @return TeamSpeak3_Helper_String
  221. */
  222. public function prepend($part)
  223. {
  224. $this->string = strval($part) . $this->string;
  225. return $this;
  226. }
  227. /**
  228. * Returns a section of the string.
  229. *
  230. * @param string $separator
  231. * @param integer $first
  232. * @param integer $last
  233. * @return TeamSpeak3_Helper_String
  234. */
  235. public function section($separator, $first = 0, $last = 0)
  236. {
  237. $sections = explode($separator, $this->string);
  238. $total = count($sections);
  239. $first = intval($first);
  240. $last = intval($last);
  241. if($first > $total) return null;
  242. if($first > $last) $last = $first;
  243. for($i = 0; $i < $total; $i++)
  244. {
  245. if($i < $first || $i > $last)
  246. {
  247. unset($sections[$i]);
  248. }
  249. }
  250. $string = implode($separator, $sections);
  251. return new self($string);
  252. }
  253. /**
  254. * Sets the size of the string to $size characters.
  255. *
  256. * @param integer $size
  257. * @param string $char
  258. * @return TeamSpeak3_Helper_String
  259. */
  260. public function resize($size, $char = "\0")
  261. {
  262. $chars = ($size - $this->count());
  263. if($chars < 0)
  264. {
  265. $this->string = substr($this->string, 0, $chars);
  266. }
  267. elseif($chars > 0)
  268. {
  269. $this->string = str_pad($this->string, $size, strval($char));
  270. }
  271. return $this;
  272. }
  273. /**
  274. * Strips whitespaces (or other characters) from the beginning and end of the string.
  275. *
  276. * @return TeamSpeak3_Helper_String
  277. */
  278. public function trim()
  279. {
  280. $this->string = trim($this->string);
  281. return $this;
  282. }
  283. /**
  284. * Escapes a string using the TeamSpeak 3 escape patterns.
  285. *
  286. * @return TeamSpeak3_Helper_String
  287. */
  288. public function escape()
  289. {
  290. foreach(TeamSpeak3::getEscapePatterns() as $search => $replace)
  291. {
  292. $this->string = str_replace($search, $replace, $this->string);
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Unescapes a string using the TeamSpeak 3 escape patterns.
  298. *
  299. * @return TeamSpeak3_Helper_String
  300. */
  301. public function unescape()
  302. {
  303. $this->string = strtr($this->string, array_flip(TeamSpeak3::getEscapePatterns()));
  304. return $this;
  305. }
  306. /**
  307. * Removes any non alphanumeric characters from the string.
  308. *
  309. * @return TeamSpeak3_Helper_String
  310. */
  311. public function filterAlnum()
  312. {
  313. $this->string = preg_replace("/[^[:alnum:]]/", "", $this->string);
  314. return $this;
  315. }
  316. /**
  317. * Removes any non alphabetic characters from the string.
  318. *
  319. * @return TeamSpeak3_Helper_String
  320. */
  321. public function filterAlpha()
  322. {
  323. $this->string = preg_replace("/[^[:alpha:]]/", "", $this->string);
  324. return $this;
  325. }
  326. /**
  327. * Removes any non numeric characters from the string.
  328. *
  329. * @return TeamSpeak3_Helper_String
  330. */
  331. public function filterDigits()
  332. {
  333. $this->string = preg_replace("/[^[:digit:]]/", "", $this->string);
  334. return $this;
  335. }
  336. /**
  337. * Returns TRUE if the string is a numeric value.
  338. *
  339. * @return boolean
  340. */
  341. public function isInt()
  342. {
  343. return (is_numeric($this->string) && !$this->contains(".") && !$this->contains("x")) ? TRUE : FALSE;
  344. }
  345. /**
  346. * Returns the integer value of the string.
  347. *
  348. * @return float
  349. * @return integer
  350. */
  351. public function toInt()
  352. {
  353. if($this->string == pow(2, 63) || $this->string == pow(2, 64))
  354. {
  355. return -1;
  356. }
  357. return ($this->string > pow(2, 31)) ? floatval($this->string) : intval($this->string);
  358. }
  359. /**
  360. * Calculates and returns the crc32 polynomial of the string.
  361. *
  362. * @return string
  363. */
  364. public function toCrc32()
  365. {
  366. return crc32($this->string);
  367. }
  368. /**
  369. * Calculates and returns the md5 checksum of the string.
  370. *
  371. * @return string
  372. */
  373. public function toMd5()
  374. {
  375. return md5($this->string);
  376. }
  377. /**
  378. * Calculates and returns the sha1 checksum of the string.
  379. *
  380. * @return string
  381. */
  382. public function toSha1()
  383. {
  384. return sha1($this->string);
  385. }
  386. /**
  387. * Returns TRUE if the string is UTF-8 encoded. This method searches for non-ascii multibyte
  388. * sequences in the UTF-8 range.
  389. *
  390. * @return boolean
  391. */
  392. public function isUtf8()
  393. {
  394. $pattern = array();
  395. $pattern[] = "[\xC2-\xDF][\x80-\xBF]"; // non-overlong 2-byte
  396. $pattern[] = "\xE0[\xA0-\xBF][\x80-\xBF]"; // excluding overlongs
  397. $pattern[] = "[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}"; // straight 3-byte
  398. $pattern[] = "\xED[\x80-\x9F][\x80-\xBF]"; // excluding surrogates
  399. $pattern[] = "\xF0[\x90-\xBF][\x80-\xBF]{2}"; // planes 1-3
  400. $pattern[] = "[\xF1-\xF3][\x80-\xBF]{3}"; // planes 4-15
  401. $pattern[] = "\xF4[\x80-\x8F][\x80-\xBF]{2}"; // plane 16
  402. return (bool) preg_match("%(?:" . implode("|", $pattern) . ")+%xs", $this->string);
  403. }
  404. /**
  405. * Converts the string to UTF-8.
  406. *
  407. * @return TeamSpeak3_Helper_String
  408. */
  409. public function toUtf8()
  410. {
  411. if(!$this->isUtf8())
  412. {
  413. $this->string = utf8_encode($this->string);
  414. }
  415. return $this;
  416. }
  417. /**
  418. * Encodes the string with MIME base64 and returns the result.
  419. *
  420. * @return string
  421. */
  422. public function toBase64()
  423. {
  424. return base64_encode($this->string);
  425. }
  426. /**
  427. * Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String
  428. *
  429. * @param string $base64
  430. * @return TeamSpeak3_Helper_String
  431. */
  432. public static function fromBase64($base64)
  433. {
  434. return new self(base64_decode($base64));
  435. }
  436. /**
  437. * Returns the hexadecimal value of the string.
  438. *
  439. * @return string
  440. */
  441. public function toHex()
  442. {
  443. $hex = "";
  444. foreach($this as $char)
  445. {
  446. $hex .= $char->toHex();
  447. }
  448. return $hex;
  449. }
  450. /**
  451. * Returns the TeamSpeak3_Helper_String based on a given hex value.
  452. *
  453. * @param string $hex
  454. * @throws TeamSpeak3_Helper_Exception
  455. * @return TeamSpeak3_Helper_String
  456. */
  457. public static function fromHex($hex)
  458. {
  459. $string = "";
  460. if(strlen($hex)%2 == 1)
  461. {
  462. throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
  463. }
  464. foreach(str_split($hex, 2) as $chunk)
  465. {
  466. $string .= chr(hexdec($chunk));
  467. }
  468. return new self($string);
  469. }
  470. /**
  471. * Returns the string transliterated from UTF-8 to Latin.
  472. *
  473. * @return TeamSpeak3_Helper_String
  474. */
  475. public function transliterate()
  476. {
  477. $utf8_accents = array(
  478. "à" => "a",
  479. "ô" => "o",
  480. "ď" => "d",
  481. "ḟ" => "f",
  482. "ë" => "e",
  483. "š" => "s",
  484. "ơ" => "o",
  485. "ß" => "ss",
  486. "ă" => "a",
  487. "ř" => "r",
  488. "ț" => "t",
  489. "ň" => "n",
  490. "ā" => "a",
  491. "ķ" => "k",
  492. "ŝ" => "s",
  493. "ỳ" => "y",
  494. "ņ" => "n",
  495. "ĺ" => "l",
  496. "ħ" => "h",
  497. "ṗ" => "p",
  498. "ó" => "o",
  499. "ú" => "u",
  500. "ě" => "e",
  501. "é" => "e",
  502. "ç" => "c",
  503. "ẁ" => "w",
  504. "ċ" => "c",
  505. "õ" => "o",
  506. "ṡ" => "s",
  507. "ø" => "o",
  508. "ģ" => "g",
  509. "ŧ" => "t",
  510. "ș" => "s",
  511. "ė" => "e",
  512. "ĉ" => "c",
  513. "ś" => "s",
  514. "î" => "i",
  515. "ű" => "u",
  516. "ć" => "c",
  517. "ę" => "e",
  518. "ŵ" => "w",
  519. "ṫ" => "t",
  520. "ū" => "u",
  521. "č" => "c",
  522. "ö" => "oe",
  523. "è" => "e",
  524. "ŷ" => "y",
  525. "ą" => "a",
  526. "ł" => "l",
  527. "ų" => "u",
  528. "ů" => "u",
  529. "ş" => "s",
  530. "ğ" => "g",
  531. "ļ" => "l",
  532. "ƒ" => "f",
  533. "ž" => "z",
  534. "ẃ" => "w",
  535. "ḃ" => "b",
  536. "å" => "a",
  537. "ì" => "i",
  538. "ï" => "i",
  539. "ḋ" => "d",
  540. "ť" => "t",
  541. "ŗ" => "r",
  542. "ä" => "ae",
  543. "í" => "i",
  544. "ŕ" => "r",
  545. "ê" => "e",
  546. "ü" => "ue",
  547. "ò" => "o",
  548. "ē" => "e",
  549. "ñ" => "n",
  550. "ń" => "n",
  551. "ĥ" => "h",
  552. "ĝ" => "g",
  553. "đ" => "d",
  554. "ĵ" => "j",
  555. "ÿ" => "y",
  556. "ũ" => "u",
  557. "ŭ" => "u",
  558. "ư" => "u",
  559. "ţ" => "t",
  560. "ý" => "y",
  561. "ő" => "o",
  562. "â" => "a",
  563. "ľ" => "l",
  564. "ẅ" => "w",
  565. "ż" => "z",
  566. "ī" => "i",
  567. "ã" => "a",
  568. "ġ" => "g",
  569. "ṁ" => "m",
  570. "ō" => "o",
  571. "ĩ" => "i",
  572. "ù" => "u",
  573. "į" => "i",
  574. "ź" => "z",
  575. "á" => "a",
  576. "û" => "u",
  577. "þ" => "th",
  578. "ð" => "dh",
  579. "æ" => "ae",
  580. "µ" => "u",
  581. "ĕ" => "e",
  582. "œ" => "oe",
  583. "À" => "A",
  584. "Ô" => "O",
  585. "Ď" => "D",
  586. "Ḟ" => "F",
  587. "Ë" => "E",
  588. "Š" => "S",
  589. "Ơ" => "O",
  590. "Ă" => "A",
  591. "Ř" => "R",
  592. "Ț" => "T",
  593. "Ň" => "N",
  594. "Ā" => "A",
  595. "Ķ" => "K",
  596. "Ŝ" => "S",
  597. "Ỳ" => "Y",
  598. "Ņ" => "N",
  599. "Ĺ" => "L",
  600. "Ħ" => "H",
  601. "Ṗ" => "P",
  602. "Ó" => "O",
  603. "Ú" => "U",
  604. "Ě" => "E",
  605. "É" => "E",
  606. "Ç" => "C",
  607. "Ẁ" => "W",
  608. "Ċ" => "C",
  609. "Õ" => "O",
  610. "Ṡ" => "S",
  611. "Ø" => "O",
  612. "Ģ" => "G",
  613. "Ŧ" => "T",
  614. "Ș" => "S",
  615. "Ė" => "E",
  616. "Ĉ" => "C",
  617. "Ś" => "S",
  618. "Î" => "I",
  619. "Ű" => "U",
  620. "Ć" => "C",
  621. "Ę" => "E",
  622. "Ŵ" => "W",
  623. "Ṫ" => "T",
  624. "Ū" => "U",
  625. "Č" => "C",
  626. "Ö" => "Oe",
  627. "È" => "E",
  628. "Ŷ" => "Y",
  629. "Ą" => "A",
  630. "Ł" => "L",
  631. "Ų" => "U",
  632. "Ů" => "U",
  633. "Ş" => "S",
  634. "Ğ" => "G",
  635. "Ļ" => "L",
  636. "Ƒ" => "F",
  637. "Ž" => "Z",
  638. "Ẃ" => "W",
  639. "Ḃ" => "B",
  640. "Å" => "A",
  641. "Ì" => "I",
  642. "Ï" => "I",
  643. "Ḋ" => "D",
  644. "Ť" => "T",
  645. "Ŗ" => "R",
  646. "Ä" => "Ae",
  647. "Í" => "I",
  648. "Ŕ" => "R",
  649. "Ê" => "E",
  650. "Ü" => "Ue",
  651. "Ò" => "O",
  652. "Ē" => "E",
  653. "Ñ" => "N",
  654. "Ń" => "N",
  655. "Ĥ" => "H",
  656. "Ĝ" => "G",
  657. "Đ" => "D",
  658. "Ĵ" => "J",
  659. "Ÿ" => "Y",
  660. "Ũ" => "U",
  661. "Ŭ" => "U",
  662. "Ư" => "U",
  663. "Ţ" => "T",
  664. "Ý" => "Y",
  665. "Ő" => "O",
  666. "Â" => "A",
  667. "Ľ" => "L",
  668. "Ẅ" => "W",
  669. "Ż" => "Z",
  670. "Ī" => "I",
  671. "Ã" => "A",
  672. "Ġ" => "G",
  673. "Ṁ" => "M",
  674. "Ō" => "O",
  675. "Ĩ" => "I",
  676. "Ù" => "U",
  677. "Į" => "I",
  678. "Ź" => "Z",
  679. "Á" => "A",
  680. "Û" => "U",
  681. "Þ" => "Th",
  682. "Ð" => "Dh",
  683. "Æ" => "Ae",
  684. "Ĕ" => "E",
  685. "Œ" => "Oe",
  686. );
  687. return new self($this->toUtf8()->replace(array_keys($utf8_accents), array_values($utf8_accents)));
  688. }
  689. /**
  690. * Processes the string and replaces all accented UTF-8 characters by unaccented ASCII-7 "equivalents",
  691. * whitespaces are replaced by a pre-defined spacer and the string is lowercase.
  692. *
  693. * @param string $spacer
  694. * @return TeamSpeak3_Helper_String
  695. */
  696. public function uriSafe($spacer = "-")
  697. {
  698. $this->string = str_replace($spacer, " ", $this->string);
  699. $this->string = $this->transliterate();
  700. $this->string = preg_replace("/(\s|[^A-Za-z0-9\-])+/", $spacer, trim(strtolower($this->string)));
  701. $this->string = trim($this->string, $spacer);
  702. return new self($this->string);
  703. }
  704. /**
  705. * Replaces space characters with percent encoded strings.
  706. *
  707. * @return string
  708. */
  709. public function spaceToPercent()
  710. {
  711. return str_replace(" ", "%20", $this->string);
  712. }
  713. /**
  714. * Returns the string as a standard string
  715. *
  716. * @return string
  717. */
  718. public function toString()
  719. {
  720. return $this->string;
  721. }
  722. /**
  723. * Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.
  724. *
  725. * @param string $function
  726. * @param array $args
  727. * @throws TeamSpeak3_Helper_Exception
  728. * @return TeamSpeak3_Helper_String
  729. */
  730. public function __call($function, $args)
  731. {
  732. if(!function_exists($function))
  733. {
  734. throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' on this object");
  735. }
  736. if(count($args))
  737. {
  738. if(($key = array_search($this, $args, TRUE)) !== FALSE)
  739. {
  740. $args[$key] = $this->string;
  741. }
  742. else
  743. {
  744. throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
  745. }
  746. $return = call_user_func_array($function, $args);
  747. }
  748. else
  749. {
  750. $return = call_user_func($function, $this->string);
  751. }
  752. if(is_string($return))
  753. {
  754. $this->string = $return;
  755. }
  756. else
  757. {
  758. return $return;
  759. }
  760. return $this;
  761. }
  762. /**
  763. * Returns the character as a standard string.
  764. *
  765. * @return string
  766. */
  767. public function __toString()
  768. {
  769. return (string) $this->string;
  770. }
  771. /**
  772. * Return UTF-8 encoded string to for serializing to JSON.
  773. *
  774. * @return string
  775. */
  776. public function jsonSerialize()
  777. {
  778. return $this->toUtf8()->string;
  779. }
  780. /**
  781. * @ignore
  782. */
  783. public function count()
  784. {
  785. return strlen($this->string);
  786. }
  787. /**
  788. * @ignore
  789. */
  790. public function rewind()
  791. {
  792. $this->position = 0;
  793. }
  794. /**
  795. * @ignore
  796. */
  797. public function valid()
  798. {
  799. return $this->position < $this->count();
  800. }
  801. /**
  802. * @ignore
  803. */
  804. public function key()
  805. {
  806. return $this->position;
  807. }
  808. /**
  809. * @ignore
  810. */
  811. public function current()
  812. {
  813. return new TeamSpeak3_Helper_Char($this->string[$this->position]);
  814. }
  815. /**
  816. * @ignore
  817. */
  818. public function next()
  819. {
  820. $this->position++;
  821. }
  822. /**
  823. * @ignore
  824. */
  825. public function offsetExists($offset)
  826. {
  827. return ($offset < strlen($this->string)) ? TRUE : FALSE;
  828. }
  829. /**
  830. * @ignore
  831. */
  832. public function offsetGet($offset)
  833. {
  834. return ($this->offsetExists($offset)) ? new TeamSpeak3_Helper_Char($this->string[$offset]) : null;
  835. }
  836. /**
  837. * @ignore
  838. */
  839. public function offsetSet($offset, $value)
  840. {
  841. if(!$this->offsetExists($offset)) return;
  842. $this->string[$offset] = strval($value);
  843. }
  844. /**
  845. * @ignore
  846. */
  847. public function offsetUnset($offset)
  848. {
  849. if(!$this->offsetExists($offset)) return;
  850. $this->string = substr_replace($this->string, "", $offset, 1);
  851. }
  852. }