|
| 1 | +<?php |
| 2 | + |
| 3 | +/*********************************************************************************** |
| 4 | + * String helpers * |
| 5 | + ***********************************************************************************/ |
| 6 | + |
| 7 | +if (!function_exists('h')) { |
| 8 | + /** |
| 9 | + * Convenience method for htmlspecialchars. |
| 10 | + * |
| 11 | + * @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects. |
| 12 | + * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they |
| 13 | + * implement a `__toString` method. Otherwise the class name will be used. |
| 14 | + * @param bool $double Encode existing html entities. |
| 15 | + * @param string|null $charset Character set to use when escaping. Defaults to config value in `mb_internal_encoding()` |
| 16 | + * or 'UTF-8'. |
| 17 | + * @return string Wrapped text. |
| 18 | + * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h |
| 19 | + */ |
| 20 | + function h($text, $double = true, $charset = null) |
| 21 | + { |
| 22 | + return \LaraSupport\Str::h($text, $double, $charset); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +if (!function_exists('last_chars')) { |
| 27 | + /** |
| 28 | + * returns given amount of characters counting backwards |
| 29 | + * |
| 30 | + * @param string $str |
| 31 | + * @param int $count |
| 32 | + * @return string |
| 33 | + */ |
| 34 | + function last_chars($str, $count = 1) |
| 35 | + { |
| 36 | + return \LaraSupport\Str::lastChars($str, $count); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +if (!function_exists('create_slug')) { |
| 42 | + /** |
| 43 | + * create slug from string |
| 44 | + * |
| 45 | + * @param string $str |
| 46 | + * @param string $symbol |
| 47 | + * @return string - e.g. in word1-word2-word3 format |
| 48 | + */ |
| 49 | + function create_slug($str = "", $symbol = "-") |
| 50 | + { |
| 51 | + return \LaraSupport\Str::createSlug($str, $symbol); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +if (!function_exists('_humanize')) { |
| 56 | + /** |
| 57 | + * @param $val |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + function _humanize($val) |
| 61 | + { |
| 62 | + $val = str_replace("_", "", $val); |
| 63 | + $matches = preg_split('/(?=[A-Z])/', $val); |
| 64 | + return trim(implode(" ", $matches)); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +if (!function_exists('shorten')) { |
| 70 | + /** |
| 71 | + * returns the short string based on $length if string's length is more than $length |
| 72 | + * |
| 73 | + * @param string $str |
| 74 | + * @param number $length |
| 75 | + * @param bool $raw |
| 76 | + * @return string |
| 77 | + */ |
| 78 | + function shorten($str = '', $length = null, $raw = false) |
| 79 | + { |
| 80 | + if ($length === null) { |
| 81 | + $length = defined('_PHP_UTIL_SHORTEN_LENGTH') ? _PHP_UTIL_SHORTEN_LENGTH : 50; |
| 82 | + } |
| 83 | + |
| 84 | + if (mb_strlen($str) > $length) { |
| 85 | + $shortStr = mb_substr($str, 0, $length) . "..."; |
| 86 | + |
| 87 | + if ($raw) { |
| 88 | + return h($shortStr); |
| 89 | + } |
| 90 | + } else { |
| 91 | + return h($str); |
| 92 | + } |
| 93 | + |
| 94 | + return '<span title="' . h(str_ireplace("/", "", $str)) . '">' . h($shortStr) . '</span>'; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/*********************************************************************************** |
| 99 | + * Array helpers * |
| 100 | + ***********************************************************************************/ |
| 101 | + |
| 102 | +if (!function_exists('get_range')) { |
| 103 | + /** |
| 104 | + * returns array with options for select box |
| 105 | + * |
| 106 | + * @param $min |
| 107 | + * @param $max |
| 108 | + * @param int $step |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + function get_range($min, $max, $step = 1) |
| 112 | + { |
| 113 | + return \LaraSupport\Arr::range($min, $max, $step); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +if (!function_exists('first_key')) { |
| 119 | + /** |
| 120 | + * returns the first key of the array |
| 121 | + * |
| 122 | + * @param array $array |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + function first_key(array $array = []) |
| 126 | + { |
| 127 | + return \LaraSupport\Arr::firstKey($array); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +if (!function_exists('last_key')) { |
| 132 | + /** |
| 133 | + * returns the last key of the array |
| 134 | + * |
| 135 | + * @param array $array |
| 136 | + * @return mixed |
| 137 | + */ |
| 138 | + function get_last_key(array $array) |
| 139 | + { |
| 140 | + return \LaraSupport\Arr::lastKey($array); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +if (!function_exists('array_unset')) { |
| 145 | + /** |
| 146 | + * unsets array's items by value |
| 147 | + * |
| 148 | + * @param array $array - the original array |
| 149 | + * @param array|string - the value or array of values to be unset |
| 150 | + * @return array - the processed array |
| 151 | + */ |
| 152 | + function array_unset(array $array, $values = []) |
| 153 | + { |
| 154 | + |
| 155 | + return \LaraSupport\Arr::unset($array, $values); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +if (!function_exists('array_i_unique')) { |
| 160 | + /** |
| 161 | + * case-insensitive array_unique |
| 162 | + * |
| 163 | + * @param array |
| 164 | + * @return array |
| 165 | + * @link http://stackoverflow.com/a/2276400/932473 |
| 166 | + */ |
| 167 | + function array_i_unique(array $array) |
| 168 | + { |
| 169 | + return \LaraSupport\Arr::iUnique($array); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +if (!function_exists('in_array_i')) { |
| 174 | + /** |
| 175 | + * case-insensitive in_array |
| 176 | + * |
| 177 | + * @param string $needle |
| 178 | + * @param array $haystack |
| 179 | + * @return bool |
| 180 | + * |
| 181 | + * @link http://us2.php.net/manual/en/function.in-array.php#89256 |
| 182 | + * @link https://stackoverflow.com/a/2166524 |
| 183 | + * @link https://stackoverflow.com/a/2166522 |
| 184 | + */ |
| 185 | + function in_array_i($needle, $haystack) |
| 186 | + { |
| 187 | + return \LaraSupport\Arr::inArrayI($needle, $haystack); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +if (!function_exists('is_numeric_array')) { |
| 192 | + /** |
| 193 | + * check if array's keys are all numeric |
| 194 | + * |
| 195 | + * @param array |
| 196 | + * @return bool |
| 197 | + * @link https://codereview.stackexchange.com/q/201/32948 |
| 198 | + */ |
| 199 | + function is_numeric_array($array) |
| 200 | + { |
| 201 | + return \LaraSupport\Arr::isNumeric($array); |
| 202 | + } |
| 203 | +} |
0 commit comments