From 8ef941b43c60702810800d8ccef9d033deb73d99 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Mon, 3 Oct 2022 09:28:26 +0300 Subject: [PATCH 1/8] remove spaces --- README.md | 174 +++++++++++++++++++++++++++--------------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 1a75019..cafb56a 100644 --- a/README.md +++ b/README.md @@ -22,151 +22,151 @@ composer require stepanenko3/laravel-helpers Included functions: ```php - apply_condition(string|int|float $amount, string|null $conditionValue = null): float +apply_condition(string|int|float $amount, string|null $conditionValue = null): float - camel_to_snake_case(string $input): string +camel_to_snake_case(string $input): string - carbon(string|null $parseString = '', string $tz = null): Carbon +carbon(string|null $parseString = '', string $tz = null): Carbon - class_basename(mixed $class): ?string +class_basename(mixed $class): ?string - class_by_key(string $key, string $service = 'Models'): ?string +class_by_key(string $key, string $service = 'Models'): ?string - cleanConditionValue(string|array $value): string|array +cleanConditionValue(string|array $value): string|array - closetags(string $html): string +closetags(string $html): string - condition_value(string|int|float $amount, string|null $conditionValue = null): string|int|float +condition_value(string|int|float $amount, string|null $conditionValue = null): string|int|float - contrast_color(string $hexColor): string +contrast_color(string $hexColor): string - distance( - string|float|int $latitudeFrom, - string|float|int $longitudeFrom, - string|float|int $latitudeTo, - string|float|int $longitudeTo, - int $earthRadius = 6371000 - ): float|int +distance( + string|float|int $latitudeFrom, + string|float|int $longitudeFrom, + string|float|int $latitudeTo, + string|float|int $longitudeTo, + int $earthRadius = 6371000 +): float|int - domain(): string +domain(): string - dots_to_camel_case( - string $string, - bool $capitalizeFirstCharacter = false - ): string +dots_to_camel_case( + string $string, + bool $capitalizeFirstCharacter = false +): string - formatMemory( - float $size, - int $level = 0, - int $precision = 2, - int $base = 1024, - bool $asArray = false - ): string|array +formatMemory( + float $size, + int $level = 0, + int $precision = 2, + int $base = 1024, + bool $asArray = false +): string|array - format_weight(float|int|string $weight): string +format_weight(float|int|string $weight): string - from_timestamp(string|int $timestamp): Carbon +from_timestamp(string|int $timestamp): Carbon - get_ascii_key(string $needle, array $haystack = []): ?int +get_ascii_key(string $needle, array $haystack = []): ?int - getAspectRatio(int $width, int $height): string +getAspectRatio(int $width, int $height): string - get_by_ascii(string $needle, array $haystack = []): mixed +get_by_ascii(string $needle, array $haystack = []): mixed - get_initials(string $name): array|string|null +get_initials(string $name): array|string|null - get_ip(): string +get_ip(): string - get_order_id(string|int $id): string +get_order_id(string|int $id): string - get_query_raw(EloquentBuilder|QueryBuilder $builder): string +get_query_raw(EloquentBuilder|QueryBuilder $builder): string - hashid_decode(string|int $id): array +hashid_decode(string|int $id): array - hashid_encode(string|int $id): string +hashid_encode(string|int $id): string - in_array_wildcard(mixed $needle, array $haystack): bool +in_array_wildcard(mixed $needle, array $haystack): bool - ipv4_in_range(mixed $ip, array|string $range): bool +ipv4_in_range(mixed $ip, array|string $range): bool - ipv4_match_mask(string $ip, string $network): bool +ipv4_match_mask(string $ip, string $network): bool - is_day( - string|int|null $timestamp = null, - float $lat = 50.458124677588046, - float $lng = 30.51755711378018 - ): bool +is_day( + string|int|null $timestamp = null, + float $lat = 50.458124677588046, + float $lng = 30.51755711378018 +): bool - is_digit(mixed $value): bool +is_digit(mixed $value): bool - is_night( - string|int|null $timestamp = null, - float $lat = 50.458124677588046, - float $lng = 30.51755711378018 - ): bool +is_night( + string|int|null $timestamp = null, + float $lat = 50.458124677588046, + float $lng = 30.51755711378018 +): bool - is_query_joined(EloquentBuilder|QueryBuilder $query, string $table): bool +is_query_joined(EloquentBuilder|QueryBuilder $query, string $table): bool - key_by_class(mixed $class): ?string +key_by_class(mixed $class): ?string - mb_lcfirst(string $string): string +mb_lcfirst(string $string): string - mb_pathinfo(string $path, string $opt = ''): array|string +mb_pathinfo(string $path, string $opt = ''): array|string - mb_ucfirst(string $string): string +mb_ucfirst(string $string): string - model_by_key(string $key): ?string +model_by_key(string $key): ?string - normalizePrice(string|int|float $price): float +normalizePrice(string|int|float $price): float - number(string|int|float $value, null|int $decimals = 0): string +number(string|int|float $value, null|int $decimals = 0): string - num_pad(string|int $value, int $length = 8): string +num_pad(string|int $value, int $length = 8): string - plural_text(string $text, string $endings): string +plural_text(string $text, string $endings): string - // Example usage: str_plural_ru('черновик', 2, '|а|ов'); # Return: черновика - plural_word(string $word, int $count, string $endings): string +// Example usage: str_plural_ru('черновик', 2, '|а|ов'); # Return: черновика +plural_word(string $word, int $count, string $endings): string - remove_query_param(string $url, string $param): string +remove_query_param(string $url, string $param): string - renderBlade(string $string, array|null $data = null): false|string +renderBlade(string $string, array|null $data = null): false|string - rglob(string $pattern): array|false +rglob(string $pattern): array|false - seo_replace(string $str, array $attributes = []): string +seo_replace(string $str, array $attributes = []): string - storage_url(string $path, string|null $disk = null): string +storage_url(string $path, string|null $disk = null): string - str_contains(string $haystack, array $needles): bool +str_contains(string $haystack, array $needles): bool - toggle_url( - string $key, - string|null $value = null, - string|null $url = null - ): string +toggle_url( + string $key, + string|null $value = null, + string|null $url = null +): string - translit_to_ua(string $text): string +translit_to_ua(string $text): string - truncate_html(string $text, int $length = 100, array $options = []): string +truncate_html(string $text, int $length = 100, array $options = []): string - url_data(string $string, string $action = 'encrypt'): bool|string +url_data(string $string, string $action = 'encrypt'): bool|string - user(): \App\Models\User +user(): \App\Models\User - utf8ize(array|string $d): string +utf8ize(array|string $d): string - uuid(int $version = 6): string +uuid(int $version = 6): string - validateDate(string $date, string $format = 'Y-m-d'): bool +validateDate(string $date, string $format = 'Y-m-d'): bool - valueIsPercentage(string $value): bool +valueIsPercentage(string $value): bool - valueIsToBeAdded(string $value): bool +valueIsToBeAdded(string $value): bool - valueIsToBeSubtracted(string $value): bool +valueIsToBeSubtracted(string $value): bool - youtube_id(string $url): string +youtube_id(string $url): string ``` ## Credits From 23f33d7d3521884b8a4c3e3afa649517e4e89db5 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Mon, 3 Oct 2022 23:46:09 +0300 Subject: [PATCH 2/8] WIP --- src/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.php b/src/helpers.php index 847997f..0c1a23e 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -100,7 +100,7 @@ function contrast_color(string $hexColor): string } if (!function_exists('carbon')) { - function carbon(string|null $parseString = '', string $tz = null): Carbon + function carbon(DateTimeInterface|string|null $parseString = '', string $tz = null): Carbon { return new Carbon($parseString, $tz); } From 22c130809adfbba1eb11f2ecd1989f9bf4a5cf35 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Wed, 5 Oct 2022 22:04:12 +0300 Subject: [PATCH 3/8] add natural_language_join helper; cs fix --- composer.json | 3 +- pint.json | 31 ++++++++ src/helpers.php | 207 ++++++++++++++++++++++++++++-------------------- 3 files changed, 154 insertions(+), 87 deletions(-) create mode 100644 pint.json diff --git a/composer.json b/composer.json index fde0ebe..57c47f8 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,9 @@ "php": ">=8.0", "ext-json": "*", "ext-openssl": "*", - "laravel/framework": "^9.0", "hashids/hashids": "^4.1", + "laravel/framework": "^9.0", + "laravel/pint": "^1.2", "ramsey/uuid": "^4.0" }, "autoload": { diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..b7f4611 --- /dev/null +++ b/pint.json @@ -0,0 +1,31 @@ +{ + "preset": "laravel", + "rules": { + "concat_space": { + "spacing": "one" + }, + "group_import": true, + "single_import_per_statement": false, + "no_space_around_double_colon": true, + "types_spaces": { + "space": "single" + }, + "operator_linebreak": { + "position": "beginning" + }, + "single_quote": { + "strings_containing_single_quote_chars": true + }, + "blank_line_before_statement": { + "statements": [ + "break", + "continue", + "declare", + "return", + "throw", + "try" + ] + }, + "not_operator_with_successor_space": false + } +} diff --git a/src/helpers.php b/src/helpers.php index 0c1a23e..2c0cccd 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,15 +3,13 @@ use App\Models\User; use Carbon\Carbon; use Hashids\Hashids; -use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\Storage; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Support\Facades\{Blade, Storage}; use Illuminate\Support\Str; use Illuminate\View\Factory; use Ramsey\Uuid\Uuid; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; - if (!function_exists('getAspectRatio')) { function getAspectRatio(int $width, int $height): string { @@ -27,7 +25,7 @@ function getAspectRatio(int $width, int $height): string } if (!function_exists('rglob')) { - function rglob(string $pattern): array|false + function rglob(string $pattern): array | false { $files = glob($pattern); @@ -49,7 +47,7 @@ function formatMemory( int $precision = 2, int $base = 1024, bool $asArray = false, - ): string|array { + ): string | array { $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; $times = floor(log($size, $base)); @@ -70,7 +68,7 @@ function formatMemory( if (!function_exists('get_initials')) { function get_initials( string $name, - ): array|string|null { + ): array | string | null { $ru = 'АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя'; $ua = 'АаБбВвГ㥴ДдЕеЄєЖжЗзИиIіЇїЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЬьЮюЯя'; @@ -100,7 +98,7 @@ function contrast_color(string $hexColor): string } if (!function_exists('carbon')) { - function carbon(DateTimeInterface|string|null $parseString = '', string $tz = null): Carbon + function carbon(DateTimeInterface | string | null $parseString = '', string $tz = null): Carbon { return new Carbon($parseString, $tz); } @@ -120,7 +118,7 @@ function translit_to_ua(string $text): string 'S' => 'С', 'T' => 'Т', 'Y' => 'Ю', 'F' => 'Ф', 'H' => 'Х', 'C' => 'Ц', 'Ch' => 'Ч', 'Sh' => 'Ш', 'Sh' => 'Щ', 'I' => 'Ы', 'E' => 'Е', 'U' => 'У', 'Ya' => 'Я', '\'' => 'ь', '\'' => 'Ь', '\'\'' => 'ъ', '\'\'' => 'Ъ', 'j' => 'ї', 'i' => 'и', 'g' => 'ґ', - 'ye' => 'є', 'J' => 'Ї', 'I' => 'І', 'G' => 'Ґ', 'YE' => 'Є' + 'ye' => 'є', 'J' => 'Ї', 'I' => 'І', 'G' => 'Ґ', 'YE' => 'Є', ]; return str_ireplace( @@ -138,7 +136,7 @@ function translit_to_ua(string $text): string if (!function_exists('is_digit')) { function is_digit(mixed $value): bool { - return !is_bool($value) && ctype_digit((string)$value); + return !is_bool($value) && ctype_digit((string) $value); } } @@ -152,7 +150,7 @@ function validateDate(string $date, string $format = 'Y-m-d'): bool } if (!function_exists('format_weight')) { - function format_weight(float|int|string $weight): string + function format_weight(float | int | string $weight): string { $string = ''; @@ -184,39 +182,42 @@ function mb_ucfirst(string $string): string } if (!function_exists('hashid_encode')) { - function hashid_encode(string|int $id): string + function hashid_encode(string | int $id): string { $hashids = new Hashids(); + return $hashids->encode($id); } } if (!function_exists('hashid_decode')) { - function hashid_decode(string|int $id): array + function hashid_decode(string | int $id): array { $hashids = new Hashids(); + return $hashids->decode($id); } } if (!function_exists('num_pad')) { - function num_pad(string|int $value, int $length = 8): string + function num_pad(string | int $value, int $length = 8): string { return str_pad($value, $length, '0', STR_PAD_LEFT); } } if (!function_exists('get_order_id')) { - function get_order_id(string|int $id): string + function get_order_id(string | int $id): string { return num_pad($id); } } if (!function_exists('get_query_raw')) { - function get_query_raw(EloquentBuilder|QueryBuilder $builder): string + function get_query_raw(EloquentBuilder | QueryBuilder $builder): string { - $queryRaw = str_replace(array('?'), array('\'%s\''), $builder->toSql()); + $queryRaw = str_replace(['?'], ['\'%s\''], $builder->toSql()); + return vsprintf($queryRaw, $builder->getBindings()); } } @@ -270,7 +271,7 @@ function seo_replace(string $str, array $attributes = []): string if (!function_exists('is_query_joined')) { function is_query_joined( - EloquentBuilder|QueryBuilder $query, + EloquentBuilder | QueryBuilder $query, string $table, ): bool { $joins = $query->getQuery()->joins; @@ -282,14 +283,15 @@ function is_query_joined( return true; } } + return false; } } if (!function_exists('normalizePrice')) { - function normalizePrice(string|int|float $price): float + function normalizePrice(string | int | float $price): float { - return (is_string($price)) ? (float)$price : $price; + return (is_string($price)) ? (float) $price : $price; } } @@ -297,18 +299,24 @@ function normalizePrice(string|int|float $price): float function mb_pathinfo( string $path, string $opt = '', - ): array|string { + ): array | string { $separator = ' qq '; - $path = preg_replace('/[^ ]/u', $separator . "\$0" . $separator, $path); - if ($opt == '') $pathinfo = pathinfo($path); - else $pathinfo = pathinfo($path, $opt); + $path = preg_replace('/[^ ]/u', $separator . '$0' . $separator, $path); + if ($opt == '') { + $pathinfo = pathinfo($path); + } else { + $pathinfo = pathinfo($path, $opt); + } if (is_array($pathinfo)) { $pathinfo2 = $pathinfo; foreach ($pathinfo2 as $key => $val) { $pathinfo[$key] = str_replace($separator, '', $val); } - } else if (is_string($pathinfo)) $pathinfo = str_replace($separator, '', $pathinfo); + } elseif (is_string($pathinfo)) { + $pathinfo = str_replace($separator, '', $pathinfo); + } + return $pathinfo; } } @@ -316,17 +324,22 @@ function mb_pathinfo( if (!function_exists('toggle_url')) { function toggle_url( string $key, - string|null $value = null, - string|null $url = null, + string | null $value = null, + string | null $url = null, ): string { $url = $url ?? request()->url(); $params = request()->all(); if (isset($params[$key])) { - if ($value) $params[$key] = $value; - else unset($params[$key]); - } else $params[$key] = $value; + if ($value) { + $params[$key] = $value; + } else { + unset($params[$key]); + } + } else { + $params[$key] = $value; + } $query = http_build_query($params); @@ -356,26 +369,27 @@ function remove_query_param( $path = $parsed['path'] ?? ''; $query = $query ? '?' . $query : ''; $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : ''; + return "$scheme$user$pass$host$port$path$query$fragment"; } } if (!function_exists('distance')) { function distance( - string|float|int $latitudeFrom, - string|float|int $longitudeFrom, - string|float|int $latitudeTo, - string|float|int $longitudeTo, + string | float | int $latitudeFrom, + string | float | int $longitudeFrom, + string | float | int $latitudeTo, + string | float | int $longitudeTo, int $earthRadius = 6371000, - ): float|int { + ): float | int { $latFrom = deg2rad($latitudeFrom); $lonFrom = deg2rad($longitudeFrom); $latTo = deg2rad($latitudeTo); $lonTo = deg2rad($longitudeTo); $lonDelta = $lonTo - $lonFrom; - $a = pow(cos($latTo) * sin($lonDelta), 2) + - pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); + $a = pow(cos($latTo) * sin($lonDelta), 2) + + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); @@ -387,8 +401,8 @@ function distance( if (!function_exists('renderBlade')) { function renderBlade( string $string, - array|null $data = null, - ): false|string { + array | null $data = null, + ): false | string { if (!$data) { $data = []; } @@ -450,7 +464,7 @@ function truncate_html( if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) { if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) { array_unshift($openTags, $tag[2]); - } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { + } elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { $pos = array_search($closeTag[1], $openTags); if ($pos !== false) { array_splice($openTags, $pos, 1); @@ -475,6 +489,7 @@ function truncate_html( } $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength); + break; } else { $truncate .= $tag[3]; @@ -523,23 +538,24 @@ function truncate_html( if (!function_exists('number')) { function number( - string|int|float $value, - null|int $decimals = 0, + string | int | float $value, + null | int $decimals = 0, ): string { return number_format($value, $decimals, '.', ' '); } } if (!function_exists('utf8ize')) { - function utf8ize(array|string $d): string + function utf8ize(array | string $d): string { if (is_array($d)) { foreach ($d as $k => $v) { $d[$k] = utf8ize($v); } - } else if (is_string($d)) { + } elseif (is_string($d)) { return utf8_encode($d); } + return $d; } } @@ -549,8 +565,9 @@ function get_ascii_key( string $needle, array $haystack = [], ): ?int { - if (!$needle || !$haystack) + if (!$needle || !$haystack) { return null; + } $asciiCodeSum = 0; $array = str_split($needle); @@ -568,8 +585,9 @@ function get_by_ascii( array $haystack = [], ): mixed { $asciiKey = get_ascii_key($needle, $haystack); - if (!$asciiKey && $asciiKey !== 0) + if (!$asciiKey && $asciiKey !== 0) { return null; + } return $haystack[$asciiKey]; } @@ -584,7 +602,7 @@ function domain(): string if (!function_exists('is_day')) { function is_day( - string|int|null $timestamp = null, + string | int | null $timestamp = null, float $lat = 50.458124677588046, float $lng = 30.51755711378018, ): bool { @@ -601,7 +619,7 @@ function is_day( if (!function_exists('is_night')) { function is_night( - string|int|null $timestamp = null, + string | int | null $timestamp = null, float $lat = 50.458124677588046, float $lng = 30.51755711378018, ): bool { @@ -612,8 +630,9 @@ function is_night( if (!function_exists('class_basename')) { function class_basename(mixed $class): ?string { - if (!$class) + if (!$class) { return null; + } if (!is_string($class)) { $class = get_class($class); @@ -630,8 +649,9 @@ function key_by_class(mixed $class): ?string { $class = class_basename($class); - if (!$class) + if (!$class) { return null; + } return camel_to_snake_case($class); } @@ -678,26 +698,26 @@ function plural_word( if (!function_exists('closetags')) { function closetags(string $html): string { - #put all opened tags into an array - preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result); + //put all opened tags into an array + preg_match_all('#<([a-z]+)( .*)?(?!/)>#iU', $html, $result); $openedtags = $result[1]; - #put all closed tags into an array - preg_match_all("##iU", $html, $result); + //put all closed tags into an array + preg_match_all('##iU', $html, $result); $closedtags = $result[1]; $len_opened = count($openedtags); - # all tags are closed + // all tags are closed if (count($closedtags) == $len_opened) { return $html; } $openedtags = array_reverse($openedtags); - # close tags + // close tags for ($i = 0; $i < $len_opened; $i++) { if (!in_array($openedtags[$i], $closedtags)) { - $html .= ""; + $html .= ''; } else { unset($closedtags[array_search($openedtags[$i], $closedtags)]); } @@ -735,6 +755,7 @@ function camel_to_snake_case( foreach ($ret as &$match) { $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); } + return implode('_', $ret); } } @@ -774,13 +795,13 @@ function youtube_id(string $url): string { if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $matches)) { $id = $matches[1]; - } else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $matches)) { + } elseif (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $matches)) { $id = $matches[1]; - } else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $matches)) { + } elseif (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $matches)) { $id = $matches[1]; - } else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $matches)) { + } elseif (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $matches)) { $id = $matches[1]; - } else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $matches)) { + } elseif (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $matches)) { $id = $matches[1]; } else { $id = null; @@ -795,15 +816,16 @@ function str_contains( string $haystack, array $needles, ): bool { - foreach ((array)$needles as $needle) { - if ($needle != '' && str_contains($haystack, $needle)) return true; + foreach ((array) $needles as $needle) { + if ($needle != '' && str_contains($haystack, $needle)) { + return true; + } } return false; } } - if (!function_exists('get_ip')) { function get_ip(): string { @@ -823,7 +845,6 @@ function get_ip(): string } } - if (!function_exists('ipv4_match_mask')) { function ipv4_match_mask( string $ip, @@ -838,7 +859,7 @@ function ipv4_match_mask( $network_long = ip2long($ipv4_arr[0]); $x = ip2long($ipv4_arr[1]); - $mask = long2ip($x) == $ipv4_arr[1] ? $x : 0xffffffff << (32 - $ipv4_arr[1]); + $mask = long2ip($x) == $ipv4_arr[1] ? $x : 0xFFFFFFFF << (32 - $ipv4_arr[1]); $ipv4_long = ip2long($ip); return ($ipv4_long & $mask) == ($network_long & $mask); @@ -848,7 +869,7 @@ function ipv4_match_mask( if (!function_exists('ipv4_in_range')) { function ipv4_in_range( mixed $ip, - array|string $range, + array | string $range, ): bool { if (is_array($range)) { foreach ($range as $iprange) { @@ -877,32 +898,30 @@ function ipv4_in_range( /** * Conditional helpers - * */ - if (!function_exists('valueIsPercentage')) { function valueIsPercentage(string $value): bool { - return (preg_match('/%/', $value) == 1); + return preg_match('/%/', $value) == 1; } } if (!function_exists('valueIsToBeSubtracted')) { function valueIsToBeSubtracted(string $value): bool { - return (preg_match('/\-/', $value) == 1); + return preg_match('/\-/', $value) == 1; } } if (!function_exists('valueIsToBeAdded')) { function valueIsToBeAdded(string $value): bool { - return (preg_match('/\+/', $value) == 1); + return preg_match('/\+/', $value) == 1; } } if (!function_exists('cleanConditionValue')) { - function cleanConditionValue(string|array $value): string|array + function cleanConditionValue(string | array $value): string | array { return str_replace(['%', '-', '+'], '', $value); } @@ -910,11 +929,12 @@ function cleanConditionValue(string|array $value): string|array if (!function_exists('condition_value')) { function condition_value( - string|int|float $amount, - string|null $conditionValue = null, - ): string|int|float { - if (!$conditionValue) + string | int | float $amount, + string | null $conditionValue = null, + ): string | int | float { + if (!$conditionValue) { return $amount; + } if (valueIsPercentage($conditionValue)) { $value = normalizePrice(cleanConditionValue($conditionValue)); @@ -928,17 +948,18 @@ function condition_value( if (!function_exists('apply_condition')) { function apply_condition( - string|int|float $amount, - string|null $conditionValue = null, + string | int | float $amount, + string | null $conditionValue = null, ): float { - if (!$conditionValue) + if (!$conditionValue) { return $amount < 0 ? 0.00 : $amount; + } $parsedRawValue = condition_value($amount, $conditionValue); $result = valueIsToBeSubtracted($conditionValue) - ? (float)($amount - $parsedRawValue) - : (float)($amount + $parsedRawValue); + ? (float) ($amount - $parsedRawValue) + : (float) ($amount + $parsedRawValue); return $result < 0 ? 0.00 : $result; } @@ -946,7 +967,7 @@ function apply_condition( if (!function_exists('from_timestamp')) { function from_timestamp( - string|int $timestamp, + string | int $timestamp, ): Carbon { return Carbon::createFromTimestamp($timestamp); } @@ -955,7 +976,7 @@ function from_timestamp( if (!function_exists('storage_url')) { function storage_url( string $path, - string|null $disk = null, + string | null $disk = null, ): string { return Storage::disk($disk)->url($path); } @@ -965,7 +986,7 @@ function storage_url( function url_data( string $string, string $action = 'encrypt', - ): bool|string { + ): bool | string { $encrypt_method = 'AES-256-CBC'; $secret_key = config('app.key'); $secret_iv = md5(config('app.key')); @@ -988,3 +1009,17 @@ function uuid( return (string) Uuid::$method(); } } + +if (!function_exists('natural_language_join')) { + function natural_language_join(array $list, $conjunction = 'and'): string + { + $oxford_separator = count($list) == 2 ? ' ' : ', '; + $last = array_pop($list); + + if ($list) { + return implode(', ', $list) . $oxford_separator . $conjunction . ' ' . $last; + } + + return $last; + } +} From c71853d15806de011a5acdf25bcfc5d0c2972bba Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Wed, 5 Oct 2022 22:09:33 +0300 Subject: [PATCH 4/8] remove oxford separator --- src/helpers.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 2c0cccd..cbbc329 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1013,11 +1013,10 @@ function uuid( if (!function_exists('natural_language_join')) { function natural_language_join(array $list, $conjunction = 'and'): string { - $oxford_separator = count($list) == 2 ? ' ' : ', '; $last = array_pop($list); if ($list) { - return implode(', ', $list) . $oxford_separator . $conjunction . ' ' . $last; + return implode(', ', $list) . ' ' . $conjunction . ' ' . $last; } return $last; From 2071d52add98bfb3b5e1c0500d0c43b6081984f4 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Wed, 12 Oct 2022 16:40:50 +0300 Subject: [PATCH 5/8] fix --- README.md | 2 +- src/helpers.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cafb56a..3d7fd02 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ truncate_html(string $text, int $length = 100, array $options = []): string url_data(string $string, string $action = 'encrypt'): bool|string -user(): \App\Models\User +user(): \App\Models\User|null utf8ize(array|string $d): string diff --git a/src/helpers.php b/src/helpers.php index cbbc329..6b94671 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -77,7 +77,7 @@ function get_initials( } if (!function_exists('user')) { - function user(): User + function user(): User | null { return request()->is('api/*') ? auth()->guard('api')->user() From 20d583724e37875da216c8b4a2dd3d6ac3acc383 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Wed, 12 Oct 2022 16:43:37 +0300 Subject: [PATCH 6/8] remove user() return type --- src/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.php b/src/helpers.php index 6b94671..3c2b685 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -77,7 +77,7 @@ function get_initials( } if (!function_exists('user')) { - function user(): User | null + function user() { return request()->is('api/*') ? auth()->guard('api')->user() From 91640a207ac156d1a6c40a690dc91aa1d290e59e Mon Sep 17 00:00:00 2001 From: Artem Stepanenko <31134245+stepanenko3@users.noreply.github.com> Date: Wed, 12 Oct 2022 16:44:27 +0300 Subject: [PATCH 7/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d7fd02..c19b640 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ truncate_html(string $text, int $length = 100, array $options = []): string url_data(string $string, string $action = 'encrypt'): bool|string -user(): \App\Models\User|null +user() utf8ize(array|string $d): string From d351609eed0e3d6308e036179f1769b3bbc32070 Mon Sep 17 00:00:00 2001 From: Artem Stepanenko Date: Wed, 19 Oct 2022 17:44:10 +0300 Subject: [PATCH 8/8] fix --- src/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.php b/src/helpers.php index 3c2b685..27ac46d 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -546,7 +546,7 @@ function number( } if (!function_exists('utf8ize')) { - function utf8ize(array | string $d): string + function utf8ize(array | string $d): string | array { if (is_array($d)) { foreach ($d as $k => $v) {