Skip to content

Commit 2347960

Browse files
committed
improvement
1 parent cd30703 commit 2347960

File tree

4 files changed

+459
-45
lines changed

4 files changed

+459
-45
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/LaraDB.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function getTables()
137137
*/
138138
public function getColumnsFullInfo($table)
139139
{
140+
// $query = 'show full columns from ' . $table;
140141
$query = 'show columns from ' . $table;
141142
$columns = $this->connection->select($query);
142143
$columnsInfo = [];
@@ -164,6 +165,10 @@ public function getColumnsFullInfo($table)
164165
*/
165166
public function getDBStructure()
166167
{
168+
// SELECT *
169+
// FROM INFORMATION_SCHEMA.COLUMNS
170+
//WHERE table_name = 'Address'
171+
167172
$query = sprintf("SELECT * FROM information_schema.columns WHERE table_schema ='%s'", env('DB_DATABASE'));
168173
$dbStructures = $this->connection->select($query);
169174
$tables = [];
@@ -178,6 +183,8 @@ public function getDBStructure()
178183
'extra' => $dbStructure->EXTRA,
179184
'unsigned' => str_contains($dbStructure->COLUMN_TYPE, 'unsigned') ? true : false,
180185
'column_type' => $dbStructure->COLUMN_TYPE,
186+
'comment' => $dbStructure->COLUMN_COMMENT,
187+
'key' => $dbStructure->COLUMN_KEY,
181188
];
182189
if ($dbStructure->CHARACTER_MAXIMUM_LENGTH) {
183190
$tables[$dbStructure->TABLE_NAME][$dbStructure->COLUMN_NAME]['length'] = $dbStructure->CHARACTER_MAXIMUM_LENGTH;

src/Str.php

Lines changed: 141 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace LaraSupport;
44

55
use Illuminate\Support\Str as BaseStr;
6-
use function PhpUtil\get_last_key;
76

87
class Str extends BaseStr
98
{
@@ -12,21 +11,23 @@ class Str extends BaseStr
1211
/**
1312
* @param $string
1413
* @param $search
14+
* @param bool $caseSensitive
1515
* @return array
1616
*/
17-
public static function positions($string, $search)
17+
public static function positions($string, $search, $caseSensitive = false)
1818
{
19-
return static::_position($string, $search);
20-
}
19+
$lastPos = 0;
20+
$positions = [];
21+
$length = strlen($search);
22+
$number = 1;
23+
$method = $caseSensitive ? 'stripos' : 'strpos';
2124

22-
/**
23-
* @param $string
24-
* @param $search
25-
* @return array
26-
*/
27-
public static function ipositions($string, $search)
28-
{
29-
return static::_position($string, $search, true);
25+
while (($lastPos = $method($string, $search, $lastPos))!== false) {
26+
$positions[$number++] = $lastPos;
27+
$lastPos = $lastPos + $length;
28+
}
29+
30+
return $positions;
3031
}
3132

3233
/**
@@ -42,10 +43,10 @@ public static function after($subject, $search, $occurrence = 1, $caseSensitive
4243
return $subject;
4344
}
4445

45-
$positions = $caseSensitive ? static::positions($subject, $search) : static::ipositions($subject, $search);
46+
$positions = static::positions($subject, $search, $caseSensitive);
4647

4748
if ($occurrence == self::LAST) {
48-
$occurrence = get_last_key($positions);
49+
$occurrence = Arr::lastKey($positions);
4950
}
5051

5152
if (empty($positions[$occurrence])) {
@@ -68,10 +69,10 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
6869
return $subject;
6970
}
7071

71-
$positions = $caseSensitive ? static::positions($subject, $search) : static::ipositions($subject, $search);
72+
$positions = static::positions($subject, $search, $caseSensitive);
7273

7374
if ($occurrence == self::LAST) {
74-
$occurrence = get_last_key($positions);
75+
$occurrence = Arr::lastKey($positions);
7576
}
7677

7778
if (empty($positions[$occurrence])) {
@@ -89,7 +90,7 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
8990
* @param string $occurenceEnd
9091
* @return bool|string
9192
*/
92-
public static function between($subject, $searchStart, $searchEnd, $occurentceStart = 1, $occurenceEnd = self::LAST)
93+
public static function between($subject, $searchStart, $searchEnd, $occurentceStart = 1, $occurenceEnd = self::LAST, $caseSensitive = false)
9394
{
9495
if ($searchStart == '' || $searchEnd == '') {
9596
return $subject;
@@ -112,24 +113,16 @@ public static function between($subject, $searchStart, $searchEnd, $occurentceSt
112113
* @param string $left
113114
* @param string $right
114115
* @param int $occurence
116+
* @param bool $caseSensitive
115117
* @return mixed
116118
*/
117-
public static function wrap($string, $search, $left = '', $right = '', $occurence = 1)
119+
public static function wrap($string, $search, $left = '', $right = '', $occurence = 1, $caseSensitive = false)
118120
{
119-
return str_replace($search , $left . $search . $right , $string);
120-
}
121+
if (!$caseSensitive) {
122+
return str_replace($search , $left . $search . $right , $string);
123+
}
121124

122-
/**
123-
* @param $string
124-
* @param $search
125-
* @param string $left
126-
* @param string $right
127-
* @param int $occurence
128-
* @return mixed
129-
*/
130-
public static function iwrap($string, $search, $left = '', $right = '', $occurence = 1)
131-
{
132-
$positions = self::ipositions($string, $search);
125+
$positions = self::positions($string, $search, $caseSensitive);
133126

134127
if (empty($positions)) {
135128
return $string;
@@ -148,24 +141,127 @@ public static function iwrap($string, $search, $left = '', $right = '', $occuren
148141
}
149142

150143
/**
151-
* @param $string
152-
* @param $search
153-
* @param bool $caseSensitive
154-
* @return array
144+
* @param $text
145+
* @param bool $double
146+
* @param null $charset
147+
* @return array|string
155148
*/
156-
protected static function _position($string, $search, $caseSensitive = false)
149+
public static function h($text, $double = true, $charset = null)
157150
{
158-
$lastPos = 0;
159-
$positions = [];
160-
$length = strlen($search);
161-
$number = 1;
162-
$method = $caseSensitive ? 'stripos' : 'strpos';
151+
if (is_string($text)) {
152+
//optimize for strings
153+
} elseif (is_array($text)) {
154+
$texts = [];
155+
foreach ($text as $k => $t) {
156+
$texts[$k] = h($t, $double, $charset);
157+
}
158+
return $texts;
159+
} elseif (is_object($text)) {
160+
if (method_exists($text, '__toString')) {
161+
$text = (string)$text;
162+
} else {
163+
$text = '(object)' . get_class($text);
164+
}
165+
} elseif (is_bool($text)) {
166+
return $text;
167+
}
163168

164-
while (($lastPos = $method($string, $search, $lastPos))!== false) {
165-
$positions[$number++] = $lastPos;
166-
$lastPos = $lastPos + $length;
169+
static $defaultCharset = false;
170+
171+
if ($defaultCharset === false) {
172+
$defaultCharset = mb_internal_encoding();
173+
if ($defaultCharset === null) {
174+
$defaultCharset = 'UTF-8';
175+
}
167176
}
168177

169-
return $positions;
178+
if (is_string($double)) {
179+
$charset = $double;
180+
}
181+
182+
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, ($charset) ? $charset : $defaultCharset, $double);
183+
}
184+
185+
/**
186+
* returns given amount of characters counting backwards
187+
*
188+
* @param string $str
189+
* @param int $count
190+
* @return string
191+
*/
192+
public static function lastChars($str, $count = 1)
193+
{
194+
return mb_substr($str, -$count, $count);
195+
}
196+
197+
/**
198+
* create slug from string
199+
*
200+
* @param string $str
201+
* @param string $symbol
202+
* @return string - e.g. in word1-word2-word3 format
203+
*/
204+
public static function createSlug($str = "", $symbol = "-")
205+
{
206+
// if not english
207+
$regex = '/^[ -~]+$/';
208+
if (!preg_match($regex, $str)) {
209+
$str = transliterator_transliterate('Any-Latin;Latin-ASCII;', $str);
210+
}
211+
212+
$str = mb_strtolower($str);
213+
$str = str_replace("'", "", $str);
214+
$str = str_replace('"', "", $str);
215+
$str = str_replace(".", $symbol, $str);
216+
$str = str_replace("\\", $symbol, $str);
217+
$str = str_replace("/", $symbol, $str);
218+
$str = preg_replace("/[~\:;\,\?\s\(\)\'\"\[\]\{\}#@&%\$\!\^\+\*=\!\<\>\|?`]/", $symbol, trim($str));
219+
220+
// everything but letters and numbers
221+
$str = preg_replace('/(.)\\1{2,}/', '$1', $str);
222+
223+
// letters replace only with 2+ repetition
224+
$str = preg_replace("/[-]{2,}/", $symbol, $str);
225+
$str = rtrim($str, $symbol);
226+
227+
return mb_strtolower($str);
228+
}
229+
230+
/**
231+
* @param $val
232+
* @return string
233+
*/
234+
public static function _humanize($val)
235+
{
236+
$val = str_replace("_", "", $val);
237+
$matches = preg_split('/(?=[A-Z])/', $val);
238+
return trim(implode(" ", $matches));
239+
}
240+
241+
/**
242+
* returns the short string based on $length if string's length is more than $length
243+
*
244+
* @param string $str
245+
* @param number $length
246+
* @param bool $raw
247+
* @return string
248+
*/
249+
public static function shorten($str = '', $length = null, $raw = false)
250+
{
251+
if ($length === null) {
252+
$length = defined('_PHP_UTIL_SHORTEN_LENGTH') ? _PHP_UTIL_SHORTEN_LENGTH : 50;
253+
}
254+
255+
if (mb_strlen($str) > $length) {
256+
$shortStr = mb_substr($str, 0, $length) . "...";
257+
258+
if ($raw) {
259+
return h($shortStr);
260+
}
261+
} else {
262+
return h($str);
263+
}
264+
265+
return '<span title="' . h(str_ireplace("/", "", $str)) . '">' . h($shortStr) . '</span>';
170266
}
171267
}

0 commit comments

Comments
 (0)