diff --git a/composer.json b/composer.json index a670901..d6f11eb 100644 --- a/composer.json +++ b/composer.json @@ -9,13 +9,12 @@ } ], "require": { - "laravel/framework": "5.6.*", - "omnicode/php-util": "3.0.*" + "laravel/framework": "5.4.*", + "omnicode/php-util": "0.1.*" }, "require-dev": { "mockery/mockery": "~1.0", - "phpunit/phpunit": "~7.0", - "omnicode/lara-test": "~0.0", + "phpunit/phpunit": "~6.0", "orchestra/testbench": "~3.0" }, @@ -28,5 +27,16 @@ "psr-4": { "Tests\\": "tests/" } + }, + "extra": { + "laravel": { + "providers": [ + "LaraSupport\\ServiceProvider\\LaraSupportServiceProvider" + ], + "aliases": { + "LaraDB": "LaraSupport\\Facades\\LaraDB", + "LaraPassword": "LaraSupport\\Facades\\LaraPassword" + } + } } } diff --git a/src/Arr.php b/src/Arr.php new file mode 100644 index 0000000..a277ff9 --- /dev/null +++ b/src/Arr.php @@ -0,0 +1,108 @@ + $v) { + if (!is_int($k)) { + return false; + } + } + + return true; + } + +} diff --git a/src/LaraDB.php b/src/LaraDB.php index b3f6aa5..a92ba91 100644 --- a/src/LaraDB.php +++ b/src/LaraDB.php @@ -137,6 +137,7 @@ public function getTables() */ public function getColumnsFullInfo($table) { +// $query = 'show full columns from ' . $table; $query = 'show columns from ' . $table; $columns = $this->connection->select($query); $columnsInfo = []; @@ -164,6 +165,10 @@ public function getColumnsFullInfo($table) */ public function getDBStructure() { +// SELECT * +// FROM INFORMATION_SCHEMA.COLUMNS +//WHERE table_name = 'Address' + $query = sprintf("SELECT * FROM information_schema.columns WHERE table_schema ='%s'", env('DB_DATABASE')); $dbStructures = $this->connection->select($query); $tables = []; @@ -178,6 +183,8 @@ public function getDBStructure() 'extra' => $dbStructure->EXTRA, 'unsigned' => str_contains($dbStructure->COLUMN_TYPE, 'unsigned') ? true : false, 'column_type' => $dbStructure->COLUMN_TYPE, + 'comment' => $dbStructure->COLUMN_COMMENT, + 'key' => $dbStructure->COLUMN_KEY, ]; if ($dbStructure->CHARACTER_MAXIMUM_LENGTH) { $tables[$dbStructure->TABLE_NAME][$dbStructure->COLUMN_NAME]['length'] = $dbStructure->CHARACTER_MAXIMUM_LENGTH; diff --git a/src/ServiceProvider/LaraSupportServiceProvider.php b/src/ServiceProvider/LaraSupportServiceProvider.php index df57898..69dff28 100644 --- a/src/ServiceProvider/LaraSupportServiceProvider.php +++ b/src/ServiceProvider/LaraSupportServiceProvider.php @@ -21,12 +21,6 @@ public function boot() */ public function register() { - $this->registerAliases( - [ - 'LaraDB' => \LaraSupport\Facades\LaraDB::class, - 'LaraPassword' => \LaraSupport\Facades\LaraPassword::class, - ] - ); $this->registerSingletons([ 'lara-db' => LaraDB::class, 'lara-password' => LaraPassword::class, diff --git a/src/Str.php b/src/Str.php index 221214b..7cef2cf 100644 --- a/src/Str.php +++ b/src/Str.php @@ -11,21 +11,23 @@ class Str extends BaseStr /** * @param $string * @param $search + * @param bool $caseSensitive * @return array */ - public static function positions($string, $search) + public static function positions($string, $search, $caseSensitive = false) { - return static::_position($string, $search); - } + $lastPos = 0; + $positions = []; + $length = strlen($search); + $number = 1; + $method = $caseSensitive ? 'stripos' : 'strpos'; - /** - * @param $string - * @param $search - * @return array - */ - public static function ipositions($string, $search) - { - return static::_position($string, $search, true); + while (($lastPos = $method($string, $search, $lastPos))!== false) { + $positions[$number++] = $lastPos; + $lastPos = $lastPos + $length; + } + + return $positions; } /** @@ -41,10 +43,10 @@ public static function after($subject, $search, $occurrence = 1, $caseSensitive return $subject; } - $positions = $caseSensitive ? static::positions($subject, $search) : static::ipositions($subject, $search); + $positions = static::positions($subject, $search, $caseSensitive); if ($occurrence == self::LAST) { - $occurrence = getLastKey($positions); + $occurrence = Arr::lastKey($positions); } if (empty($positions[$occurrence])) { @@ -67,10 +69,10 @@ public static function before($subject, $search, $occurrence = self::LAST, $case return $subject; } - $positions = $caseSensitive ? static::positions($subject, $search) : static::ipositions($subject, $search); + $positions = static::positions($subject, $search, $caseSensitive); if ($occurrence == self::LAST) { - $occurrence = getLastKey($positions); + $occurrence = Arr::lastKey($positions); } if (empty($positions[$occurrence])) { @@ -88,7 +90,7 @@ public static function before($subject, $search, $occurrence = self::LAST, $case * @param string $occurenceEnd * @return bool|string */ - public static function between($subject, $searchStart, $searchEnd, $occurentceStart = 1, $occurenceEnd = self::LAST) + public static function between($subject, $searchStart, $searchEnd, $occurentceStart = 1, $occurenceEnd = self::LAST, $caseSensitive = false) { if ($searchStart == '' || $searchEnd == '') { return $subject; @@ -111,24 +113,16 @@ public static function between($subject, $searchStart, $searchEnd, $occurentceSt * @param string $left * @param string $right * @param int $occurence + * @param bool $caseSensitive * @return mixed */ - public static function wrap($string, $search, $left = '', $right = '', $occurence = 1) + public static function wrap($string, $search, $left = '', $right = '', $occurence = 1, $caseSensitive = false) { - return str_replace($search , $left . $search . $right , $string); - } + if (!$caseSensitive) { + return str_replace($search , $left . $search . $right , $string); + } - /** - * @param $string - * @param $search - * @param string $left - * @param string $right - * @param int $occurence - * @return mixed - */ - public static function iwrap($string, $search, $left = '', $right = '', $occurence = 1) - { - $positions = self::ipositions($string, $search); + $positions = self::positions($string, $search, $caseSensitive); if (empty($positions)) { return $string; @@ -147,24 +141,127 @@ public static function iwrap($string, $search, $left = '', $right = '', $occuren } /** - * @param $string - * @param $search - * @param bool $caseSensitive - * @return array + * @param $text + * @param bool $double + * @param null $charset + * @return array|string */ - protected static function _position($string, $search, $caseSensitive = false) + public static function h($text, $double = true, $charset = null) { - $lastPos = 0; - $positions = []; - $length = strlen($search); - $number = 1; - $method = $caseSensitive ? 'stripos' : 'strpos'; + if (is_string($text)) { + //optimize for strings + } elseif (is_array($text)) { + $texts = []; + foreach ($text as $k => $t) { + $texts[$k] = h($t, $double, $charset); + } + return $texts; + } elseif (is_object($text)) { + if (method_exists($text, '__toString')) { + $text = (string)$text; + } else { + $text = '(object)' . get_class($text); + } + } elseif (is_bool($text)) { + return $text; + } - while (($lastPos = $method($string, $search, $lastPos))!== false) { - $positions[$number++] = $lastPos; - $lastPos = $lastPos + $length; + static $defaultCharset = false; + + if ($defaultCharset === false) { + $defaultCharset = mb_internal_encoding(); + if ($defaultCharset === null) { + $defaultCharset = 'UTF-8'; + } } - return $positions; + if (is_string($double)) { + $charset = $double; + } + + return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, ($charset) ? $charset : $defaultCharset, $double); + } + + /** + * returns given amount of characters counting backwards + * + * @param string $str + * @param int $count + * @return string + */ + public static function lastChars($str, $count = 1) + { + return mb_substr($str, -$count, $count); + } + + /** + * create slug from string + * + * @param string $str + * @param string $symbol + * @return string - e.g. in word1-word2-word3 format + */ + public static function createSlug($str = "", $symbol = "-") + { + // if not english + $regex = '/^[ -~]+$/'; + if (!preg_match($regex, $str)) { + $str = transliterator_transliterate('Any-Latin;Latin-ASCII;', $str); + } + + $str = mb_strtolower($str); + $str = str_replace("'", "", $str); + $str = str_replace('"', "", $str); + $str = str_replace(".", $symbol, $str); + $str = str_replace("\\", $symbol, $str); + $str = str_replace("/", $symbol, $str); + $str = preg_replace("/[~\:;\,\?\s\(\)\'\"\[\]\{\}#@&%\$\!\^\+\*=\!\<\>\|?`]/", $symbol, trim($str)); + + // everything but letters and numbers + $str = preg_replace('/(.)\\1{2,}/', '$1', $str); + + // letters replace only with 2+ repetition + $str = preg_replace("/[-]{2,}/", $symbol, $str); + $str = rtrim($str, $symbol); + + return mb_strtolower($str); + } + + /** + * @param $val + * @return string + */ + public static function _humanize($val) + { + $val = str_replace("_", "", $val); + $matches = preg_split('/(?=[A-Z])/', $val); + return trim(implode(" ", $matches)); + } + + /** + * returns the short string based on $length if string's length is more than $length + * + * @param string $str + * @param number $length + * @param bool $raw + * @return string + */ + public static function shorten($str = '', $length = null, $raw = false) + { + if ($length === null) { + $length = defined('_PHP_UTIL_SHORTEN_LENGTH') ? _PHP_UTIL_SHORTEN_LENGTH : 50; + } + + if (mb_strlen($str) > $length) { + $shortStr = mb_substr($str, 0, $length) . "..."; + + if ($raw) { + return h($shortStr); + } + } else { + return h($str); + } + + return '' . h($shortStr) . ''; } } diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..f08ae66 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,203 @@ + $length) { + $shortStr = mb_substr($str, 0, $length) . "..."; + + if ($raw) { + return h($shortStr); + } + } else { + return h($str); + } + + return '' . h($shortStr) . ''; + } +} + +/*********************************************************************************** + * Array helpers * + ***********************************************************************************/ + +if (!function_exists('get_range')) { + /** + * returns array with options for select box + * + * @param $min + * @param $max + * @param int $step + * @return array + */ + function get_range($min, $max, $step = 1) + { + return \LaraSupport\Arr::range($min, $max, $step); + } +} + + +if (!function_exists('first_key')) { + /** + * returns the first key of the array + * + * @param array $array + * @return mixed + */ + function first_key(array $array = []) + { + return \LaraSupport\Arr::firstKey($array); + } +} + +if (!function_exists('last_key')) { + /** + * returns the last key of the array + * + * @param array $array + * @return mixed + */ + function get_last_key(array $array) + { + return \LaraSupport\Arr::lastKey($array); + } +} + +if (!function_exists('array_unset')) { + /** + * unsets array's items by value + * + * @param array $array - the original array + * @param array|string - the value or array of values to be unset + * @return array - the processed array + */ + function array_unset(array $array, $values = []) + { + + return \LaraSupport\Arr::unset($array, $values); + } +} + +if (!function_exists('array_i_unique')) { + /** + * case-insensitive array_unique + * + * @param array + * @return array + * @link http://stackoverflow.com/a/2276400/932473 + */ + function array_i_unique(array $array) + { + return \LaraSupport\Arr::iUnique($array); + } +} + +if (!function_exists('in_array_i')) { + /** + * case-insensitive in_array + * + * @param string $needle + * @param array $haystack + * @return bool + * + * @link http://us2.php.net/manual/en/function.in-array.php#89256 + * @link https://stackoverflow.com/a/2166524 + * @link https://stackoverflow.com/a/2166522 + */ + function in_array_i($needle, $haystack) + { + return \LaraSupport\Arr::inArrayI($needle, $haystack); + } +} + +if (!function_exists('is_numeric_array')) { + /** + * check if array's keys are all numeric + * + * @param array + * @return bool + * @link https://codereview.stackexchange.com/q/201/32948 + */ + function is_numeric_array($array) + { + return \LaraSupport\Arr::isNumeric($array); + } +}