Ctype.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Ctype;
  11. /**
  12. * Ctype implementation through regex.
  13. *
  14. * @internal
  15. *
  16. * @author Gert de Pagter <BackEndTea@gmail.com>
  17. */
  18. final class Ctype
  19. {
  20. /**
  21. * Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.
  22. *
  23. * @see https://php.net/ctype-alnum
  24. *
  25. * @param string|int $text
  26. *
  27. * @return bool
  28. */
  29. public static function ctype_alnum($text)
  30. {
  31. $text = self::convert_int_to_char_for_ctype($text);
  32. return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z0-9]/', $text);
  33. }
  34. /**
  35. * Returns TRUE if every character in text is a letter, FALSE otherwise.
  36. *
  37. * @see https://php.net/ctype-alpha
  38. *
  39. * @param string|int $text
  40. *
  41. * @return bool
  42. */
  43. public static function ctype_alpha($text)
  44. {
  45. $text = self::convert_int_to_char_for_ctype($text);
  46. return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z]/', $text);
  47. }
  48. /**
  49. * Returns TRUE if every character in text is a control character from the current locale, FALSE otherwise.
  50. *
  51. * @see https://php.net/ctype-cntrl
  52. *
  53. * @param string|int $text
  54. *
  55. * @return bool
  56. */
  57. public static function ctype_cntrl($text)
  58. {
  59. $text = self::convert_int_to_char_for_ctype($text);
  60. return \is_string($text) && '' !== $text && !preg_match('/[^\x00-\x1f\x7f]/', $text);
  61. }
  62. /**
  63. * Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
  64. *
  65. * @see https://php.net/ctype-digit
  66. *
  67. * @param string|int $text
  68. *
  69. * @return bool
  70. */
  71. public static function ctype_digit($text)
  72. {
  73. $text = self::convert_int_to_char_for_ctype($text);
  74. return \is_string($text) && '' !== $text && !preg_match('/[^0-9]/', $text);
  75. }
  76. /**
  77. * Returns TRUE if every character in text is printable and actually creates visible output (no white space), FALSE otherwise.
  78. *
  79. * @see https://php.net/ctype-graph
  80. *
  81. * @param string|int $text
  82. *
  83. * @return bool
  84. */
  85. public static function ctype_graph($text)
  86. {
  87. $text = self::convert_int_to_char_for_ctype($text);
  88. return \is_string($text) && '' !== $text && !preg_match('/[^!-~]/', $text);
  89. }
  90. /**
  91. * Returns TRUE if every character in text is a lowercase letter.
  92. *
  93. * @see https://php.net/ctype-lower
  94. *
  95. * @param string|int $text
  96. *
  97. * @return bool
  98. */
  99. public static function ctype_lower($text)
  100. {
  101. $text = self::convert_int_to_char_for_ctype($text);
  102. return \is_string($text) && '' !== $text && !preg_match('/[^a-z]/', $text);
  103. }
  104. /**
  105. * Returns TRUE if every character in text will actually create output (including blanks). Returns FALSE if text contains control characters or characters that do not have any output or control function at all.
  106. *
  107. * @see https://php.net/ctype-print
  108. *
  109. * @param string|int $text
  110. *
  111. * @return bool
  112. */
  113. public static function ctype_print($text)
  114. {
  115. $text = self::convert_int_to_char_for_ctype($text);
  116. return \is_string($text) && '' !== $text && !preg_match('/[^ -~]/', $text);
  117. }
  118. /**
  119. * Returns TRUE if every character in text is printable, but neither letter, digit or blank, FALSE otherwise.
  120. *
  121. * @see https://php.net/ctype-punct
  122. *
  123. * @param string|int $text
  124. *
  125. * @return bool
  126. */
  127. public static function ctype_punct($text)
  128. {
  129. $text = self::convert_int_to_char_for_ctype($text);
  130. return \is_string($text) && '' !== $text && !preg_match('/[^!-\/\:-@\[-`\{-~]/', $text);
  131. }
  132. /**
  133. * Returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.
  134. *
  135. * @see https://php.net/ctype-space
  136. *
  137. * @param string|int $text
  138. *
  139. * @return bool
  140. */
  141. public static function ctype_space($text)
  142. {
  143. $text = self::convert_int_to_char_for_ctype($text);
  144. return \is_string($text) && '' !== $text && !preg_match('/[^\s]/', $text);
  145. }
  146. /**
  147. * Returns TRUE if every character in text is an uppercase letter.
  148. *
  149. * @see https://php.net/ctype-upper
  150. *
  151. * @param string|int $text
  152. *
  153. * @return bool
  154. */
  155. public static function ctype_upper($text)
  156. {
  157. $text = self::convert_int_to_char_for_ctype($text);
  158. return \is_string($text) && '' !== $text && !preg_match('/[^A-Z]/', $text);
  159. }
  160. /**
  161. * Returns TRUE if every character in text is a hexadecimal 'digit', that is a decimal digit or a character from [A-Fa-f] , FALSE otherwise.
  162. *
  163. * @see https://php.net/ctype-xdigit
  164. *
  165. * @param string|int $text
  166. *
  167. * @return bool
  168. */
  169. public static function ctype_xdigit($text)
  170. {
  171. $text = self::convert_int_to_char_for_ctype($text);
  172. return \is_string($text) && '' !== $text && !preg_match('/[^A-Fa-f0-9]/', $text);
  173. }
  174. /**
  175. * Converts integers to their char versions according to normal ctype behaviour, if needed.
  176. *
  177. * If an integer between -128 and 255 inclusive is provided,
  178. * it is interpreted as the ASCII value of a single character
  179. * (negative values have 256 added in order to allow characters in the Extended ASCII range).
  180. * Any other integer is interpreted as a string containing the decimal digits of the integer.
  181. *
  182. * @param string|int $int
  183. *
  184. * @return mixed
  185. */
  186. private static function convert_int_to_char_for_ctype($int)
  187. {
  188. if (!\is_int($int)) {
  189. return $int;
  190. }
  191. if ($int < -128 || $int > 255) {
  192. return (string) $int;
  193. }
  194. if ($int < 0) {
  195. $int += 256;
  196. }
  197. return \chr($int);
  198. }
  199. }