PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/util/wrapper/String.class.php

https://github.com/franc015/open-miage-util
PHP | 338 lines | 156 code | 46 blank | 136 comment | 35 complexity | 519ffa74565e677f0a0c47da42a88e4a MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. Import::php('util.time.Date.DateParseInt');
  3. Import::php("util.wrapper.RegExp");
  4. /**
  5. * Class wrapper of string
  6. * @package OpenM
  7. * @subpackage util/wrapper
  8. * @license http://www.apache.org/licenses/LICENSE-2.0 Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * @link http://www.open-miage.org
  20. * @author Gael SAUNIER
  21. */
  22. class String {
  23. /**
  24. * @desc string natif de php
  25. * @var string
  26. */
  27. protected $string;
  28. public function __construct($string = null) {
  29. if ($string == null)
  30. $string = "";
  31. else if ($string instanceof String)
  32. $string = "$string";
  33. else if (!is_string($string))
  34. throw new InvalidArgumentException("string must be a String");
  35. $this->string = $string;
  36. }
  37. /**
  38. * @ignore
  39. */
  40. public function charAt($index) {
  41. if (!is_int($index))
  42. throw new InvalidArgumentException("le paramètre doit être un int.");
  43. return substr($this->string, $index - 1, 1);
  44. }
  45. /**
  46. * used to concat two String
  47. * @param string $string to concat with source
  48. * @return String concatenated String result
  49. */
  50. public function concat($string) {
  51. if (!self::isString($string))
  52. throw new InvalidArgumentException("string must be a string");
  53. return new String($this->string . $string);
  54. }
  55. /**
  56. * @ignore
  57. */
  58. public function contains($CharSequence) {
  59. if (!self::isString($CharSequence))
  60. throw new InvalidArgumentException("le paramètre doit être un String.");
  61. return (bool) (strpos($this->string, $CharSequence) !== false);
  62. }
  63. /**
  64. * @ignore
  65. */
  66. public function contentEquals($CharSequence) {
  67. if (!self::isString($CharSequence))
  68. throw new InvalidArgumentException("le paramètre doit être un String.");
  69. return $this->equals(($CharSequence instanceOf String) ? $CharSequence : new String($CharSequence));
  70. }
  71. /**
  72. * @ignore
  73. */
  74. public function endsWith($suffix) {
  75. if (!self::isString($suffix))
  76. throw new InvalidArgumentException("le paramètre doit être un String.");
  77. return RegExp::ereg("$suffix$", $this->string);
  78. }
  79. /**
  80. * @ignore
  81. */
  82. public function equals($string) {
  83. return $this->compareTo($string) == 0;
  84. }
  85. /**
  86. * @ignore
  87. */
  88. public function equalsIgnoreCase($anotherString) {
  89. if (!self::isString($anotherString))
  90. throw new InvalidArgumentException("le paramètre doit être un String.");
  91. return $this->compareToIgnoreCase($anotherString) == 0;
  92. }
  93. /**
  94. * @ignore
  95. */
  96. public function indexOf($string, $fromIndex = 1) {
  97. if (!self::isString($string))
  98. throw new InvalidArgumentException("le premier paramètre doit être un String.");
  99. if (!is_int($fromIndex))
  100. throw new InvalidArgumentException("le second paramètre doit être un int.");
  101. return $fromIndex - 1 + strpos(substr($this->string, $fromIndex - 1), ($string instanceOf String) ? $string->__toString() : $string) + 1;
  102. }
  103. /**
  104. * used to recover the length of String
  105. * @return int number of characters
  106. */
  107. public function length() {
  108. return strlen($this->string);
  109. }
  110. /**
  111. * @ignore
  112. */
  113. public function replace($oldChar, $newChar) {
  114. return $this->replaceAll($oldChar, $newChar);
  115. }
  116. /**
  117. * @ignore
  118. */
  119. public function replaceAll($regex, $replacement) {
  120. if (!self::isString($regex))
  121. throw new InvalidArgumentException("le premier paramètre doit être un String.");
  122. if (!self::isString($replacement))
  123. throw new InvalidArgumentException("le second paramètre doit être un String.");
  124. return new String(str_replace(($regex instanceOf String) ? $regex->__toString() : $regex, ($replacement instanceOf String) ? $replacement->__toString() : $replacement, $this->string));
  125. }
  126. /**
  127. * @ignore
  128. */
  129. public function replaceFirst($regex, $replacement) {
  130. if (!self::isString($regex))
  131. throw new InvalidArgumentException("le premier paramètre doit être un String.");
  132. if (!self::isString($replacement))
  133. throw new InvalidArgumentException("le second paramètre doit être un String.");
  134. $debut = strpos(($regex instanceOf String) ? $regex->__toString() : $regex, $this->string);
  135. $longueur = ($regex instanceOf String) ? $regex->length() : strlen($regex);
  136. return new String(substr($this->string, 0, $debut) . $replacement . substr($this->string, $debut + $longueur));
  137. }
  138. /**
  139. * @ignore
  140. */
  141. public function split($regex, $limit = null) {
  142. if (!self::isString($regex))
  143. throw new InvalidArgumentException("regex must be a string");
  144. if (!is_int($limit) && $limit !== null)
  145. throw new InvalidArgumentException("limit must be an int");
  146. if ($limit === null)
  147. return explode(($regex instanceOf String) ? $regex->string : $regex, $this->string);
  148. else
  149. return explode(($regex instanceOf String) ? $regex->string : $regex, $this->string, $limit);
  150. }
  151. /**
  152. * @ignore
  153. */
  154. public function startsWith($prefix, $toffset = null) {
  155. if (!self::isString($prefix))
  156. throw new InvalidArgumentException("le paramètre doit être un String.");
  157. if (!is_int($toffset) && $limit !== null)
  158. throw new InvalidArgumentException("le paramètre doit être un int.");
  159. return RegExp::ereg("^$prefix", ($toffset == null) ? $this->string : substr($this->string, $toffset - 1));
  160. }
  161. /**
  162. * wrapper function of substr
  163. * @param int $beginIndex
  164. * @param int $length
  165. * @return String
  166. * @ignore
  167. */
  168. public function substring($beginIndex, $length = null) {
  169. if (!is_int($beginIndex))
  170. throw new InvalidArgumentException("le premier paramètre doit être un int.");
  171. if (!is_int($length) && $length !== null)
  172. throw new InvalidArgumentException("le paramètre doit être un int.");
  173. if ($length === null)
  174. return new String(substr($this->string, $beginIndex - 1));
  175. else
  176. return new String(substr($this->string, $beginIndex - 1, $length));
  177. }
  178. /**
  179. * used to recover the String in lowercase
  180. * @return String String in lowercase result
  181. */
  182. public function toLowerCase() {
  183. return new String(strtolower($this->string));
  184. }
  185. /**
  186. * @ignore
  187. */
  188. public function toString() {
  189. return $this;
  190. }
  191. /**
  192. * used to recover the String in uppercase
  193. * @return String String in uppercase result
  194. */
  195. public function toUpperCase() {
  196. return new String(strtoupper($this->string));
  197. }
  198. /**
  199. * used to trim String (remove spaces after and before)
  200. * @return String trim String result
  201. */
  202. public function trim() {
  203. return new String(trim($this->string));
  204. }
  205. /**
  206. * used to cast it to int
  207. * @return int
  208. */
  209. public function toInt() {
  210. return (int) $this->string;
  211. }
  212. /**
  213. * used to cast it to float
  214. * @return float
  215. */
  216. public function toFloat() {
  217. return (float) $this->string;
  218. }
  219. /**
  220. * used to cast it to boolean
  221. * @return boolean
  222. */
  223. public function toBool() {
  224. return (bool) $this->string;
  225. }
  226. /**
  227. * @ignore
  228. */
  229. public function __toString() {
  230. return $this->string;
  231. }
  232. /**
  233. * @ignore
  234. */
  235. public function sqlSecure() {
  236. return new String(trim(htmlspecialchars(addslashes($this->string))));
  237. }
  238. /**
  239. * @ignore
  240. */
  241. public function iso() {
  242. return new String(utf8_decode($this->string));
  243. }
  244. /**
  245. * @ignore
  246. */
  247. public function utf8() {
  248. return new String(utf8_encode($this->string));
  249. }
  250. /**
  251. * @ignore
  252. */
  253. public function entiteHTML() {
  254. return new String(htmlentities($this->string));
  255. }
  256. /**
  257. * used to copy this String
  258. * @return String copy of String source
  259. */
  260. public function copy() {
  261. return new String($this->string);
  262. }
  263. /**
  264. * used to know if given string is a native string or a String
  265. * @param String|string $string is element to check
  266. * @return boolean true if it is a native string or a String, else false
  267. */
  268. public static function isString($string) {
  269. return is_string($string) || ($string instanceof String);
  270. }
  271. /**
  272. * used to know if given string is a native string or a String or null
  273. * @param String|string|null $string is element to check
  274. * @return boolean true if it is a native string or a String or null, else false
  275. * @uses self::isString
  276. */
  277. public static function isStringOrNull($string) {
  278. if ($string == null)
  279. return true;
  280. else
  281. return self::isString($string);
  282. }
  283. /**
  284. * convert value to string
  285. * @param type $value
  286. * @return String
  287. */
  288. public static function cast($value) {
  289. return (string) $value;
  290. }
  291. }
  292. ?>