helper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * The helper methods file of zin of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin;
  12. if(!function_exists('str_contains'))
  13. {
  14. /**
  15. * Determine if a string contains a given substring
  16. *
  17. * @param string $haystack
  18. * @param string $needle
  19. * @return bool
  20. */
  21. function str_contains($haystack, $needle)
  22. {
  23. return strpos($haystack, $needle) !== false;
  24. }
  25. }
  26. else
  27. {
  28. function str_contains($haystack, $needle)
  29. {
  30. return strpos($haystack, $needle) !== false;
  31. }
  32. }
  33. if(!function_exists('str_starts_with'))
  34. {
  35. /**
  36. * Checks if a string starts with a given substring
  37. *
  38. * @param string $haystack
  39. * @param string $needle
  40. * @return bool
  41. */
  42. function str_starts_with($haystack, $needle)
  43. {
  44. return strpos($haystack, $needle) === 0;
  45. }
  46. }
  47. else
  48. {
  49. function str_starts_with($haystack, $needle)
  50. {
  51. return strncmp($haystack, $needle, strlen($needle)) === 0;
  52. }
  53. }
  54. if(!function_exists('str_ends_with'))
  55. {
  56. /**
  57. * Checks if a string starts with a given substring.
  58. *
  59. * @param string $haystack
  60. * @param string $needle
  61. * @return bool
  62. */
  63. function str_ends_with($haystack, $needle)
  64. {
  65. $length = strlen($needle);
  66. if ($length === 0) return true;
  67. $position = strpos($haystack, $needle);
  68. return $position !== false && $position === strlen($haystack) - $length;
  69. }
  70. }
  71. else
  72. {
  73. function str_ends_with($haystack, $needle)
  74. {
  75. return substr_compare($haystack, $needle, -strlen($needle)) === 0;
  76. }
  77. }
  78. if(!function_exists('array_is_list'))
  79. {
  80. /**
  81. * Checks whether a given array is a list.
  82. *
  83. * @param array $array
  84. * @return bool
  85. */
  86. function array_is_list($array)
  87. {
  88. if ($array === []) {
  89. return true;
  90. }
  91. return array_keys($array) === range(0, count($array) - 1);
  92. }
  93. }
  94. else
  95. {
  96. function array_is_list($array)
  97. {
  98. $arrayIsList = function (array $array) : bool {
  99. if (function_exists('array_is_list')) {
  100. return array_is_list($array);
  101. }
  102. if ($array === []) {
  103. return true;
  104. }
  105. $current_key = 0;
  106. foreach ($array as $key => $noop) {
  107. if ($key !== $current_key) {
  108. return false;
  109. }
  110. ++$current_key;
  111. }
  112. return true;
  113. };
  114. return $arrayIsList($array);
  115. }
  116. }
  117. function uncamelize($camelCaps, $separator = '-')
  118. {
  119. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  120. }
  121. function isHTML($string)
  122. {
  123. return $string !== strip_tags($string) ? true : false;
  124. }
  125. /**
  126. * Check if any element in an array passes a test.
  127. *
  128. * @param array $array
  129. * @param callable $fn
  130. * @return bool
  131. */
  132. function array_some($array, $fn)
  133. {
  134. foreach ($array as $value)
  135. {
  136. if($fn($value)) return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Check if all elements in an array pass a test.
  142. *
  143. * @param array $array
  144. * @param callable $fn
  145. * @return bool
  146. */
  147. function array_every($array, $fn)
  148. {
  149. foreach ($array as $value)
  150. {
  151. if(!$fn($value)) return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Filter null values from an array.
  157. *
  158. * @param array $array
  159. * @return array
  160. */
  161. function array_filter_null($array)
  162. {
  163. return array_filter($array, function($value)
  164. {
  165. return $value !== null;
  166. });
  167. }