diff --git a/CHANGELOG.md b/CHANGELOG.md index cb365e5..4e49465 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All Notable changes to `support` will be documented in this file +## 1.3.1 - 2016-08-25 + +- CHANGE add if(!function_exists()) for some existing laravel functions and others to skip conflict. + ## 1.3.0 - 2016-08-24 - ADD PHP 7.1 support. diff --git a/src/array.php b/src/array.php index 7fdb4ac..b4ca1c1 100644 --- a/src/array.php +++ b/src/array.php @@ -160,50 +160,56 @@ function CleanUpArrayOfInt($array) return $result; } -/** - * Returns an array with two elements. - * - * Iterates over each value in the array passing them to the callback function. - * If the callback function returns true, the current value from array is returned in the first - * element of result array. If not, it is return in the second element of result array. - * - * Array keys are preserved. - * - * @param array $array - * @param callable $callback - * @return array - * @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php - */ -function array_split_filter(array $array, callable $callback) -{ - $passesFilter = array_filter($array, $callback); - $negatedCallback = function ($item) use ($callback) { - return !$callback($item); - }; - $doesNotPassFilter = array_filter($array, $negatedCallback); - return [$passesFilter, $doesNotPassFilter]; +if (!function_exists('array_split_filter')) { + + /** + * Returns an array with two elements. + * + * Iterates over each value in the array passing them to the callback function. + * If the callback function returns true, the current value from array is returned in the first + * element of result array. If not, it is return in the second element of result array. + * + * Array keys are preserved. + * + * @param array $array + * @param callable $callback + * @return array + * @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php + */ + function array_split_filter(array $array, callable $callback) + { + $passesFilter = array_filter($array, $callback); + $negatedCallback = function ($item) use ($callback) { + return !$callback($item); + }; + $doesNotPassFilter = array_filter($array, $negatedCallback); + return [$passesFilter, $doesNotPassFilter]; + } } -/** - * Checks whether specific value exists in array of object. - * For exampe, following code - * $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id'); - * will produce 2 - * @author wapmorgan - * @since 2015.05.19 - * @param array $haystack Source array - * @param mixed $needle Needed value - * @param string $column Column to perform search - * @param bool $strict Should search be strict or not. - * @return bool True if value exists in array, False otherwise. - * @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php - */ -function in_array_column($haystack, $needle, $column, $strict = false) -{ - foreach ($haystack as $k => $elem) { - if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) { - return true; +if (!function_exists('in_array_column')) { + + /** + * Checks whether specific value exists in array of object. + * For exampe, following code + * $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id'); + * will produce 2 + * @author wapmorgan + * @since 2015.05.19 + * @param array $haystack Source array + * @param mixed $needle Needed value + * @param string $column Column to perform search + * @param bool $strict Should search be strict or not. + * @return bool True if value exists in array, False otherwise. + * @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php + */ + function in_array_column($haystack, $needle, $column, $strict = false) + { + foreach ($haystack as $k => $elem) { + if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) { + return true; + } } + return false; } - return false; } diff --git a/src/helpers.php b/src/helpers.php index 58be996..9dcbb7a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,19 +1,22 @@ map(function (int $decimal): string { - return str_pad(dechex($decimal), 2, STR_PAD_LEFT); - }) - ->implode(''); +if (!function_exists('rgb2hex')) { + + /** + * @param int $red + * @param int $green + * @param int $blue + * @return string + * @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php + */ + function rgb2hex(int $red, int $green, int $blue): string + { + return '#' . collect([$red, $green, $blue]) + ->map(function (int $decimal): string { + return str_pad(dechex($decimal), 2, STR_PAD_LEFT); + }) + ->implode(''); + } } /** @@ -37,18 +40,21 @@ function format_euro(float $val = 0) : string return format_money($val, 2, '€ '); } -/** - * Given a number, return the number + 'th' or 'rd' etc - * @param $cdnl - * @return string - */ -function ordinal($cdnl) -{ - $test_c = abs($cdnl) % 10; - $ext = ((abs($cdnl) % 100 < 21 && abs($cdnl) % 100 > 4) ? 'th' - : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) - ? 'th' : 'st' : 'nd' : 'rd' : 'th')); - return $cdnl . $ext; +if (!function_exists('ordinal')) { + + /** + * Given a number, return the number + 'th' or 'rd' etc + * @param $cdnl + * @return string + */ + function ordinal($cdnl) + { + $test_c = abs($cdnl) % 10; + $ext = ((abs($cdnl) % 100 < 21 && abs($cdnl) % 100 > 4) ? 'th' + : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) + ? 'th' : 'st' : 'nd' : 'rd' : 'th')); + return $cdnl . $ext; + } } if (!function_exists('value')) { @@ -185,7 +191,7 @@ function getMaximumFileUploadSize(bool $humanFormat = false) * @param string $chiave the key to encrypt. if empty generate a random key on the fly. * @return string */ -function EncryptString(string $string, string $chiave = '') +function encryptString(string $string, string $chiave = '') { if ($chiave == '') { $chiave = str_random(64); diff --git a/src/reflection.php b/src/reflection.php index 594a1c0..57af284 100644 --- a/src/reflection.php +++ b/src/reflection.php @@ -1,37 +1,43 @@ getShortName(); + /** + * @param $object + * @return string + */ + function short_class_name($object): string + { + $objectProperties = new \ReflectionClass($object); + + return $objectProperties->getShortName(); + } } -/** - * Get all class constants that starts with $startsWithFilter - * or if empty get all class constants. - * @param $object - * @param string $startsWithFilter - * @return array - * @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php - */ -function class_constants($object, string $startsWithFilter = ''): array -{ - $objectProperties = new \ReflectionClass($object); +if (!function_exists('class_constants')) { + + /** + * Get all class constants that starts with $startsWithFilter + * or if empty get all class constants. + * @param $object + * @param string $startsWithFilter + * @return array + * @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php + */ + function class_constants($object, string $startsWithFilter = ''): array + { + $objectProperties = new \ReflectionClass($object); - $constants = $objectProperties->getConstants(); + $constants = $objectProperties->getConstants(); - if ($startsWithFilter == '') { - return $constants; - } + if ($startsWithFilter == '') { + return $constants; + } - return array_filter($constants, function ($key) use ($startsWithFilter) { - return starts_with(strtolower($key), strtolower($startsWithFilter)); - }, ARRAY_FILTER_USE_KEY); + return array_filter($constants, function ($key) use ($startsWithFilter) { + return starts_with(strtolower($key), strtolower($startsWithFilter)); + }, ARRAY_FILTER_USE_KEY); + } } if (!function_exists('class_uses_recursive')) { diff --git a/src/sanitize.php b/src/sanitize.php index d799065..2bacecc 100644 --- a/src/sanitize.php +++ b/src/sanitize.php @@ -1,28 +1,34 @@