PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/install/phing/classes/phing/util/StringHelper.php

https://github.com/chregu/fluxcms
PHP | 209 lines | 112 code | 23 blank | 74 comment | 25 complexity | 4082bb63caaa27c622fc82149312a221 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * String helper utility class.
  4. *
  5. * This class includes some Java-like functions for parsing strings,
  6. * as well as some functions for getting qualifiers / unqualifying phing-style
  7. * classpaths. (e.g. "phing.util.StringHelper").
  8. *
  9. * @author Hans Lellelid <hans@xmpl.org>
  10. * @package phing.system.util
  11. */
  12. class StringHelper {
  13. private static $TRUE_VALUES = array("on", "true", "t", "yes");
  14. private static $FALSE_VALUES = array("off", "false", "f", "no");
  15. /**
  16. * Replaces identifier tokens with corresponding text values in passed string.
  17. *
  18. * @params array $strings Array of strings to multiply. (If string is passed, will convert to array)
  19. * @params array $tokens The tokens to search for.
  20. * @params array $replacements The values with which to replace found tokens.
  21. * @return string
  22. */
  23. public function multiply($strings, $tokens, $replacements) {
  24. $strings = (array) $strings;
  25. $results = array();
  26. foreach ($strings as $string) {
  27. $results[] = str_replace($tokens, $replacements, $string);
  28. }
  29. return $results;
  30. }
  31. /**
  32. * Remove qualification to name.
  33. * E.g. eg.Cat -> Cat
  34. * @param string $qualifiedName
  35. * @param string $separator Character used to separate.
  36. */
  37. public function unqualify($qualifiedName, $separator = '.') {
  38. // if false, then will be 0
  39. $pos = strrpos($qualifiedName, $separator);
  40. if ($pos === false) {
  41. return $qualifiedName; // there is no '.' in the qualifed name
  42. } else {
  43. return substr($qualifiedName, $pos + 1); // start just after '.'
  44. }
  45. }
  46. /**
  47. * Converts a string to an indexed array of chars
  48. * There's really no reason for this to be used in PHP, since strings
  49. * are all accessible using the $string{0} notation.
  50. * @param string $string
  51. * @return array
  52. * @deprecated
  53. */
  54. function toCharArray($str) {
  55. $ret=array();
  56. $len=strlen($str);
  57. for ($i=0; $i < $len; $i++) {
  58. $ret[] = $str{$i};
  59. }
  60. return $ret;
  61. }
  62. /**
  63. * Get the qualifier part of a qualified name.
  64. * E.g. eg.Cat -> eg
  65. * @return string
  66. */
  67. public function qualifier($qualifiedName, $seperator = '.') {
  68. $pos = strrchr($qualifiedName, $seperator);
  69. if ($pos === false) {
  70. return '';
  71. } else {
  72. return substr($qualifiedName, 0, $pos);
  73. }
  74. }
  75. /**
  76. * @param array $columns String[]
  77. * @param string $prefix
  78. * @return array String[]
  79. */
  80. public function prefix( $columns, $prefix) {
  81. if ($prefix == null) return $columns;
  82. $qualified = array();
  83. foreach($columns as $key => $column) {
  84. $qualified[$key] = $prefix . $column;
  85. }
  86. return $qualified;
  87. }
  88. /**
  89. *
  90. * @return string
  91. */
  92. public function root($qualifiedName, $separator = '.') {
  93. $loc = strpos($qualifiedName, $separator);
  94. return ($loc === false) ? $qualifiedName : substr($qualifiedName, 0, $loc);
  95. }
  96. /**
  97. * @return int
  98. */
  99. public function hashCode($string) {
  100. return crc32($string);
  101. }
  102. /**
  103. * @return boolean
  104. */
  105. public function booleanValue($s) {
  106. if (is_bool($s)) {
  107. return $s; // it's already boolean (not a string)
  108. }
  109. // otherwise assume it's something like "true" or "t"
  110. $trimmed = strtolower(trim($s));
  111. return (boolean) in_array($trimmed, self::$TRUE_VALUES);
  112. }
  113. /** tests if a string is a representative of a boolean */
  114. public function isBoolean($s) {
  115. if (is_bool($s)) {
  116. return true; // it already is boolean
  117. }
  118. if ($s === "" || $s === null || !is_string($s)) {
  119. return false; // not a valid string for testing
  120. }
  121. $test = trim(strtolower($s));
  122. return (boolean) in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES));
  123. }
  124. /**
  125. * Creates a key based on any number of passed params.
  126. * @return string
  127. */
  128. public function key() {
  129. $args = func_get_args();
  130. return serialize($args);
  131. }
  132. /** tests if a string starts with a given string */
  133. function startsWith($check, $string) {
  134. if ($check === "" || $check === $string) {
  135. return true;
  136. } else {
  137. return (strpos($string, $check) === 0) ? true : false;
  138. }
  139. }
  140. /** tests if a string ends with a given string */
  141. function endsWith($check, $string) {
  142. if ($check === "" || $check === $string) {
  143. return true;
  144. } else {
  145. return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
  146. }
  147. }
  148. /**
  149. * a natural way of getting a subtring, php's circular string buffer and strange
  150. * return values suck if you want to program strict as of C or friends
  151. */
  152. function substring($string, $startpos, $endpos = -1) {
  153. $len = strlen($string);
  154. $endpos = (int) (($endpos === -1) ? $len-1 : $endpos);
  155. if ($startpos > $len-1 || $startpos < 0) {
  156. trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
  157. }
  158. if ($endpos > $len-1 || $endpos < $startpos) {
  159. trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR);
  160. }
  161. if ($startpos === $endpos) {
  162. return (string) $string{$startpos};
  163. } else {
  164. $len = $endpos-$startpos;
  165. }
  166. return substr($string, $startpos, $len+1);
  167. }
  168. /**
  169. * Does the value correspond to a slot variable?
  170. * @param string $value
  171. */
  172. function isSlotVar($value) {
  173. $value = trim($value);
  174. if ($value === "") return false;
  175. return preg_match('/^%\{([\w\.\-]+)\}$/', $value);
  176. }
  177. /**
  178. * Extracts the variable name for a slot var in the format %{task.current_file}
  179. * @param string $var The var from build file.
  180. * @return string Extracted name part.
  181. */
  182. function slotVar($var) {
  183. return trim($var, '%{} ');
  184. }
  185. }
  186. ?>