PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phing/phing/classes/phing/util/StringHelper.php

https://gitlab.com/Isaki/le331.fr
PHP | 304 lines | 131 code | 29 blank | 144 comment | 25 complexity | 355179ba4e8666b5108f387cdacaf003 MD5 | raw file
  1. <?php
  2. /**
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information please see
  17. * <http://phing.info>.
  18. */
  19. /**
  20. * String helper utility class.
  21. *
  22. * This class includes some Java-like functions for parsing strings,
  23. * as well as some functions for getting qualifiers / unqualifying phing-style
  24. * classpaths. (e.g. "phing.util.StringHelper").
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org>
  27. *
  28. * @package phing.system.util
  29. */
  30. class StringHelper
  31. {
  32. /** @var array */
  33. private static $TRUE_VALUES = array("on", "true", "t", "yes");
  34. /** @var array */
  35. private static $FALSE_VALUES = array("off", "false", "f", "no");
  36. /**
  37. * Replaces identifier tokens with corresponding text values in passed string.
  38. *
  39. * @param array $strings Array of strings to multiply. (If string is passed, will convert to array)
  40. * @param array $tokens The tokens to search for.
  41. * @param array $replacements The values with which to replace found tokens.
  42. *
  43. * @return string
  44. */
  45. public static function multiply($strings, $tokens, $replacements)
  46. {
  47. $strings = (array) $strings;
  48. $results = array();
  49. foreach ($strings as $string) {
  50. $results[] = str_replace($tokens, $replacements, $string);
  51. }
  52. return $results;
  53. }
  54. /**
  55. * Remove qualification to name.
  56. * E.g. eg.Cat -> Cat
  57. *
  58. * @param string $qualifiedName
  59. * @param string $separator Character used to separate.
  60. *
  61. * @return string
  62. */
  63. public static function unqualify($qualifiedName, $separator = '.')
  64. {
  65. // if false, then will be 0
  66. $pos = strrpos($qualifiedName, $separator);
  67. if ($pos === false) {
  68. return $qualifiedName; // there is no '.' in the qualifed name
  69. } else {
  70. return substr($qualifiedName, $pos + 1); // start just after '.'
  71. }
  72. }
  73. /**
  74. * Converts a string to an indexed array of chars
  75. * There's really no reason for this to be used in PHP, since strings
  76. * are all accessible using the $string{0} notation.
  77. *
  78. * @param string $str
  79. *
  80. * @return array
  81. *
  82. * @deprecated
  83. */
  84. public static function toCharArray($str)
  85. {
  86. $ret = array();
  87. $len = strlen($str);
  88. for ($i = 0; $i < $len; $i++) {
  89. $ret[] = $str{$i};
  90. }
  91. return $ret;
  92. }
  93. /**
  94. * Get the qualifier part of a qualified name.
  95. * E.g. eg.Cat -> eg
  96. *
  97. * @param $qualifiedName
  98. * @param string $separator
  99. *
  100. * @return string
  101. */
  102. public static function qualifier($qualifiedName, $separator = '.')
  103. {
  104. $pos = strrchr($qualifiedName, $separator);
  105. if ($pos === false) {
  106. return '';
  107. } else {
  108. return substr($qualifiedName, 0, $pos);
  109. }
  110. }
  111. /**
  112. * @param array $columns String[]
  113. * @param string $prefix
  114. *
  115. * @return array String[]
  116. */
  117. public static function prefix($columns, $prefix)
  118. {
  119. if ($prefix == null) {
  120. return $columns;
  121. }
  122. $qualified = array();
  123. foreach ($columns as $key => $column) {
  124. $qualified[$key] = $prefix . $column;
  125. }
  126. return $qualified;
  127. }
  128. /**
  129. * @param $qualifiedName
  130. * @param string $separator
  131. *
  132. * @return string
  133. */
  134. public static function root($qualifiedName, $separator = '.')
  135. {
  136. $loc = strpos($qualifiedName, $separator);
  137. return ($loc === false) ? $qualifiedName : substr($qualifiedName, 0, $loc);
  138. }
  139. /**
  140. * @param $string
  141. *
  142. * @return int
  143. */
  144. public static function hashCode($string)
  145. {
  146. return crc32($string);
  147. }
  148. /**
  149. * @param bool|string $s
  150. *
  151. * @return boolean
  152. */
  153. public static function booleanValue($s)
  154. {
  155. if (is_bool($s)) {
  156. return $s; // it's already boolean (not a string)
  157. }
  158. // otherwise assume it's something like "true" or "t"
  159. $trimmed = strtolower(trim($s));
  160. return (boolean) in_array($trimmed, self::$TRUE_VALUES);
  161. }
  162. /**
  163. * tests if a string is a representative of a boolean
  164. *
  165. * @param bool|string $s
  166. *
  167. * @return bool
  168. */
  169. public static function isBoolean($s)
  170. {
  171. if (is_bool($s)) {
  172. return true; // it already is boolean
  173. }
  174. if ($s === "" || $s === null || !is_string($s)) {
  175. return false; // not a valid string for testing
  176. }
  177. $test = trim(strtolower($s));
  178. return (boolean) in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES));
  179. }
  180. /**
  181. * Creates a key based on any number of passed params.
  182. *
  183. * @return string
  184. */
  185. public static function key()
  186. {
  187. $args = func_get_args();
  188. return serialize($args);
  189. }
  190. /**
  191. * tests if a string starts with a given string
  192. *
  193. * @param $check
  194. * @param $string
  195. *
  196. * @return bool
  197. */
  198. public static function startsWith($check, $string)
  199. {
  200. if ($check === "" || $check === $string) {
  201. return true;
  202. } else {
  203. return (strpos($string, $check) === 0) ? true : false;
  204. }
  205. }
  206. /**
  207. * tests if a string ends with a given string
  208. *
  209. * @param $check
  210. * @param $string
  211. *
  212. * @return bool
  213. */
  214. public static function endsWith($check, $string)
  215. {
  216. if ($check === "" || $check === $string) {
  217. return true;
  218. } else {
  219. return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
  220. }
  221. }
  222. /**
  223. * a natural way of getting a subtring, php's circular string buffer and strange
  224. * return values suck if you want to program strict as of C or friends
  225. *
  226. * @param string $string
  227. * @param int $startpos
  228. * @param int $endpos
  229. *
  230. * @return string
  231. */
  232. public static function substring($string, $startpos, $endpos = -1)
  233. {
  234. $len = strlen($string);
  235. $endpos = (int) (($endpos === -1) ? $len - 1 : $endpos);
  236. if ($startpos > $len - 1 || $startpos < 0) {
  237. trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
  238. }
  239. if ($endpos > $len - 1 || $endpos < $startpos) {
  240. trigger_error("substring(), Endindex out of bounds must be $startpos<n<" . ($len - 1), E_USER_ERROR);
  241. }
  242. if ($startpos === $endpos) {
  243. return (string) $string{$startpos};
  244. } else {
  245. $len = $endpos - $startpos;
  246. }
  247. return substr($string, $startpos, $len + 1);
  248. }
  249. /**
  250. * Does the value correspond to a slot variable?
  251. *
  252. * @param string $value
  253. *
  254. * @return bool|int
  255. */
  256. public static function isSlotVar($value)
  257. {
  258. $value = trim($value);
  259. if ($value === "") {
  260. return false;
  261. }
  262. return preg_match('/^%\{([\w\.\-]+)\}$/', $value);
  263. }
  264. /**
  265. * Extracts the variable name for a slot var in the format %{task.current_file}
  266. *
  267. * @param string $var The var from build file.
  268. *
  269. * @return string Extracted name part.
  270. */
  271. public static function slotVar($var)
  272. {
  273. return trim($var, '%{} ');
  274. }
  275. }