Skip to content

Commit

Permalink
## 1.3.1 - 2016-08-25
Browse files Browse the repository at this point in the history
- CHANGE add if(!function_exists()) for some existing laravel functions and others to skip conflict.
  • Loading branch information
lopadova committed Aug 25, 2016
1 parent d6b7100 commit 841ded1
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 184 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
90 changes: 48 additions & 42 deletions src/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
60 changes: 33 additions & 27 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<?php

/**
* @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('');
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('');
}
}

/**
Expand All @@ -37,18 +40,21 @@ function format_euro(float $val = 0) : string
return format_money($val, 2, '&euro; ');
}

/**
* 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')) {
Expand Down Expand Up @@ -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);
Expand Down
58 changes: 32 additions & 26 deletions src/reflection.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
<?php

/**
* @param $object
* @return string
*/
function short_class_name($object): string
{
$objectProperties = new \ReflectionClass($object);
if (!function_exists('short_class_name')) {

return $objectProperties->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')) {
Expand Down
46 changes: 26 additions & 20 deletions src/sanitize.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
<?php

/**
* Strip new line breaks from a string
* @param $str
* @return string|array
*/
function strip_nl($str)
{
return str_replace("\n", "", str_replace("\r", "", $str));
if (!function_exists('strip_nl')) {

/**
* Strip new line breaks from a string
* @param $str
* @return string|array
*/
function strip_nl($str)
{
return str_replace("\n", "", str_replace("\r", "", $str));
}
}

/**
* Javascript escape
* @param string $str
* @return string
* @source https://github.com/rtconner/laravel-plusplus/blob/laravel-5/src/plus-functions.php
*/
function jse(string $str) : string
{
if (isNullOrEmpty($str)) {
return '';
if (!function_exists('jse')) {

/**
* Javascript escape
* @param string $str
* @return string
* @source https://github.com/rtconner/laravel-plusplus/blob/laravel-5/src/plus-functions.php
*/
function jse(string $str) : string
{
if (isNullOrEmpty($str)) {
return '';
}
$str = str_replace("\n", "", str_replace("\r", "", $str));
return addslashes($str);
}
$str = str_replace("\n", "", str_replace("\r", "", $str));
return addslashes($str);
}

if (!function_exists('e')) {
Expand Down
Loading

0 comments on commit 841ded1

Please sign in to comment.