Skip to content

Commit 1300fa7

Browse files
committed
version 2.1.0
1 parent c11bc33 commit 1300fa7

File tree

6 files changed

+173
-54
lines changed

6 files changed

+173
-54
lines changed

.idea/php.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
}
1010
],
1111
"require": {
12-
"laravel/framework": "5.6.*",
13-
"omnicode/php-util": "3.0.*"
12+
"laravel/framework": "5.5.*",
13+
"omnicode/php-util": "0.0.*"
1414
},
1515
"require-dev": {
1616
"mockery/mockery": "~1.0",
17-
"phpunit/phpunit": "~7.0",
18-
"omnicode/lara-test": "~0.0",
17+
"phpunit/phpunit": "~6.0",
18+
"omnicode/lara-test": "~2.1",
1919
"orchestra/testbench": "~3.0"
2020

2121
},
@@ -28,5 +28,16 @@
2828
"psr-4": {
2929
"Tests\\": "tests/"
3030
}
31+
},
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"LaraSupport\\ServiceProvider\\LaraSupportServiceProvider"
36+
],
37+
"aliases": {
38+
"LaraDB": "LaraSupport\\Facades\\LaraDB",
39+
"LaraPassword": "LaraSupport\\Facades\\LaraPassword"
40+
}
41+
}
3142
}
3243
}

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/ServiceProvider/LaraSupportServiceProvider.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public function boot()
2121
*/
2222
public function register()
2323
{
24-
$this->registerAliases(
25-
[
26-
'LaraDB' => \LaraSupport\Facades\LaraDB::class,
27-
'LaraPassword' => \LaraSupport\Facades\LaraPassword::class,
28-
]
29-
);
3024
$this->registerSingletons([
3125
'lara-db' => LaraDB::class,
3226
'lara-password' => LaraPassword::class,

src/Str.php

Lines changed: 141 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ class Str extends BaseStr
1111
/**
1212
* @param $string
1313
* @param $search
14+
* @param bool $caseSensitive
1415
* @return array
1516
*/
16-
public static function positions($string, $search)
17+
public static function positions($string, $search, $caseSensitive = false)
1718
{
18-
return static::_position($string, $search);
19-
}
19+
$lastPos = 0;
20+
$positions = [];
21+
$length = strlen($search);
22+
$number = 1;
23+
$method = $caseSensitive ? 'stripos' : 'strpos';
2024

21-
/**
22-
* @param $string
23-
* @param $search
24-
* @return array
25-
*/
26-
public static function ipositions($string, $search)
27-
{
28-
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;
2931
}
3032

3133
/**
@@ -41,10 +43,10 @@ public static function after($subject, $search, $occurrence = 1, $caseSensitive
4143
return $subject;
4244
}
4345

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

4648
if ($occurrence == self::LAST) {
47-
$occurrence = getLastKey($positions);
49+
$occurrence = Arr::lastKey($positions);
4850
}
4951

5052
if (empty($positions[$occurrence])) {
@@ -67,10 +69,10 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
6769
return $subject;
6870
}
6971

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

7274
if ($occurrence == self::LAST) {
73-
$occurrence = getLastKey($positions);
75+
$occurrence = Arr::lastKey($positions);
7476
}
7577

7678
if (empty($positions[$occurrence])) {
@@ -88,7 +90,7 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
8890
* @param string $occurenceEnd
8991
* @return bool|string
9092
*/
91-
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)
9294
{
9395
if ($searchStart == '' || $searchEnd == '') {
9496
return $subject;
@@ -111,24 +113,16 @@ public static function between($subject, $searchStart, $searchEnd, $occurentceSt
111113
* @param string $left
112114
* @param string $right
113115
* @param int $occurence
116+
* @param bool $caseSensitive
114117
* @return mixed
115118
*/
116-
public static function wrap($string, $search, $left = '', $right = '', $occurence = 1)
119+
public static function wrap($string, $search, $left = '', $right = '', $occurence = 1, $caseSensitive = false)
117120
{
118-
return str_replace($search , $left . $search . $right , $string);
119-
}
121+
if (!$caseSensitive) {
122+
return str_replace($search , $left . $search . $right , $string);
123+
}
120124

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

133127
if (empty($positions)) {
134128
return $string;
@@ -147,24 +141,127 @@ public static function iwrap($string, $search, $left = '', $right = '', $occuren
147141
}
148142

149143
/**
150-
* @param $string
151-
* @param $search
152-
* @param bool $caseSensitive
153-
* @return array
144+
* @param $text
145+
* @param bool $double
146+
* @param null $charset
147+
* @return array|string
154148
*/
155-
protected static function _position($string, $search, $caseSensitive = false)
149+
public static function h($text, $double = true, $charset = null)
156150
{
157-
$lastPos = 0;
158-
$positions = [];
159-
$length = strlen($search);
160-
$number = 1;
161-
$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+
}
162168

163-
while (($lastPos = $method($string, $search, $lastPos))!== false) {
164-
$positions[$number++] = $lastPos;
165-
$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+
}
166176
}
167177

168-
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>';
169266
}
170267
}

0 commit comments

Comments
 (0)