Skip to content

Commit ec46add

Browse files
committed
v3.1.1
1 parent 1300fa7 commit ec46add

File tree

2 files changed

+311
-0
lines changed

2 files changed

+311
-0
lines changed

src/Arr.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace LaraSupport;
4+
5+
use Illuminate\Support\Arr as BaseArr;
6+
7+
class Arr extends BaseArr
8+
{
9+
/**
10+
* returns array with options for select box
11+
*
12+
* @param $min
13+
* @param $max
14+
* @param int $step
15+
* @return array
16+
*/
17+
public static function range($min, $max, $step = 1)
18+
{
19+
return array_combine(range($min, $max, $step), range($min, $max, $step));
20+
}
21+
22+
23+
/**
24+
* returns the first key of the array
25+
*
26+
* @param array $array
27+
* @return mixed
28+
*/
29+
public static function firstKey(array $array = [])
30+
{
31+
reset($array);
32+
return key($array);
33+
}
34+
35+
/**
36+
* returns the last key of the array
37+
*
38+
* @param array $array
39+
* @return mixed
40+
*/
41+
public static function lastKey(array $array)
42+
{
43+
$array = array_reverse($array, true);
44+
reset($array);
45+
return key($array);
46+
}
47+
48+
/**
49+
* unsets array's items by value
50+
*
51+
* @param array $array - the original array
52+
* @param array|string - the value or array of values to be unset
53+
* @return array - the processed array
54+
*/
55+
public static function unset(array $array, $values = [])
56+
{
57+
$values = (array) $values;
58+
return array_diff($array, $values);
59+
}
60+
61+
/**
62+
* case-insensitive array_unique
63+
*
64+
* @param array
65+
* @return array
66+
* @link http://stackoverflow.com/a/2276400/932473
67+
*/
68+
public static function iUnique(array $array)
69+
{
70+
$lowered = array_map('mb_strtolower', $array);
71+
return array_intersect_key($array, array_unique($lowered));
72+
}
73+
74+
/**
75+
* case-insensitive in_array
76+
*
77+
* @param string $needle
78+
* @param array $haystack
79+
* @return bool
80+
*
81+
* @link http://us2.php.net/manual/en/function.in-array.php#89256
82+
* @link https://stackoverflow.com/a/2166524
83+
* @link https://stackoverflow.com/a/2166522
84+
*/
85+
public static function inArrayI($needle, $haystack)
86+
{
87+
return in_array(strtolower($needle), array_map('strtolower', $haystack));
88+
}
89+
90+
/**
91+
* check if array's keys are all numeric
92+
*
93+
* @param array
94+
* @return bool
95+
* @link https://codereview.stackexchange.com/q/201/32948
96+
*/
97+
public static function isNumeric($array)
98+
{
99+
foreach ($array as $k => $v) {
100+
if (!is_int($k)) {
101+
return false;
102+
}
103+
}
104+
105+
return true;
106+
}
107+
108+
}

src/helpers.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)