PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/ManiaLivePlugins/Standard/TeamSpeak/TeamSpeak3/Helper/String.php

http://manialive.googlecode.com/
PHP | 692 lines | 317 code | 88 blank | 287 comment | 26 complexity | 2371ddae1cc9fe4a75da85f19985fc6b MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: String.php 9/26/2011 7:06:29 scp@orilla $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.8-beta
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. namespace ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper;
  27. /**
  28. * @class String
  29. * @brief Helper class for string handling.
  30. */
  31. class String implements \ArrayAccess, \Iterator, \Countable
  32. {
  33. /**
  34. * Stores the original string.
  35. *
  36. * @var string
  37. */
  38. protected $string;
  39. /**
  40. * @ignore
  41. */
  42. protected $position = 0;
  43. /**
  44. * The String constructor.
  45. *
  46. * @param string $string
  47. * @return String
  48. */
  49. public function __construct($string)
  50. {
  51. $this->string = strval($string);
  52. }
  53. /**
  54. * Returns a String object for thegiven string.
  55. *
  56. * @param string $string
  57. * @return String
  58. */
  59. public static function factory($string)
  60. {
  61. return new self($string);
  62. }
  63. /**
  64. * Replaces every occurrence of the string $search with the string $replace.
  65. *
  66. * @param string $search
  67. * @param string $replace
  68. * @param boolean $caseSensitivity
  69. * @return String
  70. */
  71. public function replace($search, $replace, $caseSensitivity = TRUE)
  72. {
  73. if($caseSensitivity)
  74. {
  75. $this->string = str_replace($search, $replace, $this->string);
  76. }
  77. else
  78. {
  79. $this->string = str_ireplace($search, $replace, $this->string);
  80. }
  81. return $this;
  82. }
  83. /**
  84. * This function replaces indexed or associative signs with given values.
  85. *
  86. * @param array $args
  87. * @param string $char
  88. * @return String
  89. */
  90. public function arg(array $args, $char = "%")
  91. {
  92. $args = array_reverse($args, TRUE);
  93. foreach($args as $key => $val)
  94. {
  95. $args[$char . $key] = $val;
  96. unset($args[$key]);
  97. }
  98. $this->string = strtr($this->string, $args);
  99. return $this;
  100. }
  101. /**
  102. * Returns true if the string starts with $pattern.
  103. *
  104. * @param string $pattern
  105. * @return boolean
  106. */
  107. public function startsWith($pattern)
  108. {
  109. return (substr($this->string, 0, strlen($pattern)) == $pattern) ? TRUE : FALSE;
  110. }
  111. /**
  112. * Returns true if the string ends with $pattern.
  113. *
  114. * @param string $pattern
  115. * @return boolean
  116. */
  117. public function endsWith($pattern)
  118. {
  119. return (substr($this->string, strlen($pattern)*-1) == $pattern) ? TRUE : FALSE;
  120. }
  121. /**
  122. * Returns the position of the first occurrence of a char in a string.
  123. *
  124. * @param string $needle
  125. * @return integer
  126. */
  127. public function findFirst($needle)
  128. {
  129. return strpos($this->string, $needle);
  130. }
  131. /**
  132. * Returns the position of the last occurrence of a char in a string.
  133. *
  134. * @param string $needle
  135. * @return integer
  136. */
  137. public function findLast($needle)
  138. {
  139. return strrpos($this->string, $needle);
  140. }
  141. /**
  142. * Returns the lowercased string.
  143. *
  144. * @return String
  145. */
  146. public function toLower()
  147. {
  148. return new self(strtolower($this->string));
  149. }
  150. /**
  151. * Returns the uppercased string.
  152. *
  153. * @return String
  154. */
  155. public function toUpper()
  156. {
  157. return new self(strtoupper($this->string));
  158. }
  159. /**
  160. * Returns true if the string contains $pattern.
  161. *
  162. * @param string $pattern
  163. * @param booean $regexp
  164. * @return boolean
  165. */
  166. public function contains($pattern, $regexp = FALSE)
  167. {
  168. if(empty($pattern))
  169. {
  170. return TRUE;
  171. }
  172. if($regexp)
  173. {
  174. return (preg_match("/" . $pattern . "/i", $this->string)) ? TRUE : FALSE;
  175. }
  176. else
  177. {
  178. return (stristr($this->string, $pattern) !== FALSE) ? TRUE : FALSE;
  179. }
  180. }
  181. /**
  182. * Returns part of a string.
  183. *
  184. * @param integer $start
  185. * @param integer $length
  186. * @return String
  187. */
  188. public function substr($start, $length = null)
  189. {
  190. $string = ($length !== null) ? substr($this->string, $start, $length) : substr($this->string, $start);
  191. return new self($string);
  192. }
  193. /**
  194. * Splits the string into substrings wherever $separator occurs.
  195. *
  196. * @param string $separator
  197. * @param integer $limit
  198. * @return array
  199. */
  200. public function split($separator, $limit = 0)
  201. {
  202. $parts = explode($separator, $this->string, ($limit) ? intval($limit) : $this->count());
  203. foreach($parts as $key => $val)
  204. {
  205. $parts[$key] = new self($val);
  206. }
  207. return $parts;
  208. }
  209. /**
  210. * Appends $part to the string.
  211. *
  212. * @param string $part
  213. * @return String
  214. */
  215. public function append($part)
  216. {
  217. $this->string = $this->string . strval($part);
  218. return $this;
  219. }
  220. /**
  221. * Prepends $part to the string.
  222. *
  223. * @param string $part
  224. * @return String
  225. */
  226. public function prepend($part)
  227. {
  228. $this->string = strval($part) . $this->string;
  229. return $this;
  230. }
  231. /**
  232. * Returns a section of the string.
  233. *
  234. * @param string $separator
  235. * @param integer $first
  236. * @param integer $last
  237. * @return String
  238. */
  239. public function section($separator, $first = 0, $last = 0)
  240. {
  241. $sections = explode($separator, $this->string);
  242. $total = count($sections);
  243. $first = intval($first);
  244. $last = intval($last);
  245. if($first > $total) return null;
  246. if($first > $last) $last = $first;
  247. for($i = 0; $i < $total; $i++)
  248. {
  249. if($i < $first || $i > $last)
  250. {
  251. unset($sections[$i]);
  252. }
  253. }
  254. $string = implode($separator, $sections);
  255. return new self($string);
  256. }
  257. /**
  258. * Sets the size of the string to $size characters.
  259. *
  260. * @param integer $size
  261. * @param string $char
  262. * @return String
  263. */
  264. public function resize($size, $char = "\0")
  265. {
  266. $chars = ($size - $this->count());
  267. if($chars < 0)
  268. {
  269. $this->string = substr($this->string, 0, $chars);
  270. }
  271. elseif($chars > 0)
  272. {
  273. $this->string = str_pad($this->string, $size, strval($char));
  274. }
  275. return $this;
  276. }
  277. /**
  278. * Strips whitespaces (or other characters) from the beginning and end of the string.
  279. *
  280. * @return String
  281. */
  282. public function trim()
  283. {
  284. $this->string = trim($this->string);
  285. return $this;
  286. }
  287. /**
  288. * Escapes a string using the TeamSpeak 3 escape patterns.
  289. *
  290. * @return String
  291. */
  292. public function escape()
  293. {
  294. foreach(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::getEscapePatterns() as $search => $replace)
  295. {
  296. $this->string = str_replace($search, $replace, $this->string);
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Unescapes a string using the TeamSpeak 3 escape patterns.
  302. *
  303. * @return String
  304. */
  305. public function unescape()
  306. {
  307. $this->string = strtr($this->string, array_flip(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::getEscapePatterns()));
  308. return $this;
  309. }
  310. /**
  311. * Removes any non alphanumeric characters from the string.
  312. *
  313. * @return String
  314. */
  315. public function filterAlnum()
  316. {
  317. $this->string = preg_replace("/[^[:alnum:]]/", "", $this->string);
  318. return $this;
  319. }
  320. /**
  321. * Removes any non alphabetic characters from the string.
  322. *
  323. * @return String
  324. */
  325. public function filterAlpha()
  326. {
  327. $this->string = preg_replace("/[^[:alpha:]]/", "", $this->string);
  328. return $this;
  329. }
  330. /**
  331. * Removes any non numeric characters from the string.
  332. *
  333. * @return String
  334. */
  335. public function filterDigits()
  336. {
  337. $this->string = preg_replace("/[^[:digit:]]/", "", $this->string);
  338. return $this;
  339. }
  340. /**
  341. * Returns TRUE if the string is a numeric value.
  342. *
  343. * @return boolean
  344. */
  345. public function isInt()
  346. {
  347. return (is_numeric($this->string) && !$this->contains(".")) ? TRUE : FALSE;
  348. }
  349. /**
  350. * Returns the integer value of the string.
  351. *
  352. * @return float
  353. * @return integer
  354. */
  355. public function toInt()
  356. {
  357. if($this->string == pow(2, 63) || $this->string == pow(2, 64))
  358. {
  359. return -1;
  360. }
  361. return ($this->string > pow(2, 31)) ? floatval($this->string) : intval($this->string);
  362. }
  363. /**
  364. * Calculates and returns the crc32 polynomial of the string.
  365. *
  366. * @return string
  367. */
  368. public function toCrc32()
  369. {
  370. return crc32($this->string);
  371. }
  372. /**
  373. * Calculates and returns the md5 checksum of the string.
  374. *
  375. * @return string
  376. */
  377. public function toMd5()
  378. {
  379. return md5($this->string);
  380. }
  381. /**
  382. * Calculates and returns the sha1 checksum of the string.
  383. *
  384. * @return string
  385. */
  386. public function toSha1()
  387. {
  388. return sha1($this->string);
  389. }
  390. /**
  391. * Returns TRUE if the string is UTF-8 encoded.
  392. *
  393. * @return boolean
  394. */
  395. public function isUtf8()
  396. {
  397. return (utf8_encode(utf8_decode($this->string)) == $this->string) ? TRUE : FALSE;
  398. }
  399. /**
  400. * Converts the string to UTF-8.
  401. *
  402. * @return String
  403. */
  404. public function toUtf8()
  405. {
  406. if(!$this->isUtf8())
  407. {
  408. $this->string = utf8_encode($this->string);
  409. }
  410. return $this;
  411. }
  412. /**
  413. * Encodes the string with MIME base64 and returns the result.
  414. *
  415. * @return string
  416. */
  417. public function toBase64()
  418. {
  419. return base64_encode($this->string);
  420. }
  421. /**
  422. * Decodes the string with MIME base64 and returns the result as an String
  423. *
  424. * @param string
  425. * @return String
  426. */
  427. public static function fromBase64($base64)
  428. {
  429. return new self(base64_decode($base64));
  430. }
  431. /**
  432. * Returns the hexadecimal value of the string.
  433. *
  434. * @return string
  435. */
  436. public function toHex()
  437. {
  438. $hex = "";
  439. foreach($this as $char)
  440. {
  441. $hex .= $char->toHex();
  442. }
  443. return $hex;
  444. }
  445. /**
  446. * Returns the String based on a given hex value.
  447. *
  448. * @param string
  449. * @throws Exception
  450. * @return String
  451. */
  452. public static function fromHex($hex)
  453. {
  454. $string = "";
  455. if(strlen($hex)%2 == 1)
  456. {
  457. throw new Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
  458. }
  459. foreach(str_split($hex, 2) as $chunk)
  460. {
  461. $string .= chr(hexdec($chunk));
  462. }
  463. return new self($string);
  464. }
  465. /**
  466. * Replaces space characters with percent encoded strings.
  467. *
  468. * @return string
  469. */
  470. public function spaceToPercent()
  471. {
  472. return str_replace(" ", "%20", $this->string);
  473. }
  474. /**
  475. * Returns the string as a standard string
  476. *
  477. * @return string
  478. */
  479. public function toString()
  480. {
  481. return $this->string;
  482. }
  483. /**
  484. * Magical function that allows you to call PHP's built-in string functions on the String object.
  485. *
  486. * @param string $function
  487. * @param array $args
  488. * @throws Exception
  489. * @return String
  490. */
  491. public function __call($function, $args)
  492. {
  493. if(!function_exists($function))
  494. {
  495. throw new Exception("cannot call undefined function '" . $function . "' on this object");
  496. }
  497. if(count($args))
  498. {
  499. if(($key = array_search($this, $args, TRUE)) !== FALSE)
  500. {
  501. $args[$key] = $this->string;
  502. }
  503. else
  504. {
  505. throw new Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
  506. }
  507. $return = call_user_func_array($function, $args);
  508. }
  509. else
  510. {
  511. $return = call_user_func($function, $this->string);
  512. }
  513. if(is_string($return))
  514. {
  515. $this->string = $return;
  516. }
  517. else
  518. {
  519. return $return;
  520. }
  521. return $this;
  522. }
  523. /**
  524. * Returns the character as a standard string.
  525. *
  526. * @return string
  527. */
  528. public function __toString()
  529. {
  530. return (string) $this->string;
  531. }
  532. /**
  533. * @ignore
  534. */
  535. public function count()
  536. {
  537. return strlen($this->string);
  538. }
  539. /**
  540. * @ignore
  541. */
  542. public function rewind()
  543. {
  544. $this->position = 0;
  545. }
  546. /**
  547. * @ignore
  548. */
  549. public function valid()
  550. {
  551. return $this->position < $this->count();
  552. }
  553. /**
  554. * @ignore
  555. */
  556. public function key()
  557. {
  558. return $this->position;
  559. }
  560. /**
  561. * @ignore
  562. */
  563. public function current()
  564. {
  565. return new Char($this->string{$this->position});
  566. }
  567. /**
  568. * @ignore
  569. */
  570. public function next()
  571. {
  572. $this->position++;
  573. }
  574. /**
  575. * @ignore
  576. */
  577. public function offsetExists($offset)
  578. {
  579. return ($offset < strlen($this->string)) ? TRUE : FALSE;
  580. }
  581. /**
  582. * @ignore
  583. */
  584. public function offsetGet($offset)
  585. {
  586. return ($this->offsetExists($offset)) ? new Char($this->string{$offset}) : null;
  587. }
  588. /**
  589. * @ignore
  590. */
  591. public function offsetSet($offset, $value)
  592. {
  593. if(!$this->offsetExists($offset)) return;
  594. $this->string{$offset} = strval($value);
  595. }
  596. /**
  597. * @ignore
  598. */
  599. public function offsetUnset($offset)
  600. {
  601. if(!$this->offsetExists($offset)) return;
  602. $this->string = substr_replace($this->string, "", $offset, 1);
  603. }
  604. }