andrey-helldar / helpers
Helpers that simplify the development process
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.3
- illuminate/contracts: ^5.5
- illuminate/support: ^5.5
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2022-02-01 13:09:56 UTC
README
Functions that simplify the development process.
Attention
This package is abandoned and no longer maintained. The author suggests using the andrey-helldar/support package instead.
Installation
To get the latest version of Helpers, simply require the project using Composer:
$ composer require andrey-helldar/helpers
Instead, you may of course manually update your require block and run composer update
if you so choose:
{ "require": { "andrey-helldar/helpers": "^1.0" } }
If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php
:
Helldar\Helpers\LaravelServiceProvider::class,
You can also publish the config file to change implementations (ie. interface to specific class):
php artisan vendor:publish --provider="Helldar\Helpers\ServiceProvider"
Now you can use helpers or access directly to interfaces.
Enjoy!
Documentation
Menu
Arrays
array_item_value_max_length()
Returns the number of characters of the longest element in the array:
echo array_item_value_max_length(array $array = []) : int echo (new \Helldar\Helpers\Support\Arr(array $array = [])) ->arrayItemValueMaxLength() : int
array_add_unique()
Push one a unique element onto the end of array:
array_add_unique(array &$array, $value) (new \Helldar\Helpers\Support\Arr()) ->addUnique(&$array, $value = 'value') (new \Helldar\Helpers\Support\Arr()) ->addUnique(&$array, $value = ['value1', 'value2', ...])
array_rename_keys()
Renaming array keys. As the first parameter, a callback function is passed, which determines the actions for processing the value. The output of the function must be a string with a name.
return array_rename_keys($callback, array $array = []) : array return (new \Helldar\Helpers\Support\Arr(array $array = [])) ->arrayRenameKeys($callback) : array
array_size_max_value()
Get the size of the longest text element of the array:
return array_size_max_value(array $array = []) : int return (new \Helldar\Helpers\Support\Arr((array $array = [])) ->arraySizeOfMaxValue() : int
array_sort_by_keys_array()
Sort an associative array in the order specified by an array of keys.
array_sort_by_keys_array($array, $sorter) : void (new \Helldar\Helpers\Support\Arr()) ->sortByKeysArray($array, $sorter) : void
Example:
$arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123]; array_sort_by_keys_array($arr, ['q', 'w', 'e']); print_r($arr); /* Array ( [q] => 1 [w] => 123 [r] => 2 [s] => 5 )
Digits
factorial()
Calculating the factorial of a number:
echo factorial(int $n = 0) : int echo (new \Helldar\Helpers\Support\Digits()) ->factorial(int $n = 0) : int
short_number()
Converts a number into a short version:
echo short_number($n = 0, $precision = 1) : int|string echo (new \Helldar\Helpers\Support\Digits($n = 0)) ->shortNumber($precision = 1) : int|string
Example:
short_number(576); // returns: 576 short_number(1000); // returns: 1K short_number(1440); // returns: 1.4K short_number(3000000); // returns: 3M short_number(3000000000); // returns: 3B short_number(3000000000000); // returns: 3T+
Dumper
dd_sql()
Dump the passed variables and end the script.
return dd_sql($query, bool $is_short = false, bool $is_return = false) : array|string|void return (new \Helldar\Helpers\Support\Dumper($query)) ->ddSql($is_short, $is_return) : array|string|void
Files
url_file_exists()
Checks whether a file or directory exists on URL:
return url_file_exists($path) : bool return (new \Helldar\Helpers\Support\Files($path)) ->urlFileExists() : bool
Http
mix_url()
Convert the relative path of a versioned Mix files to absolute.
return mix_url($path) : string return (new \Helldar\Helpers\Support\Http($path)) ->mixUrl() : string
base_url()
Get the domain name from the URL.
return base_url($url) : string return (new \Helldar\Helpers\Support\Http($url)) ->baseUrl() : string
build_url()
Reverse function for parse_url() (http://php.net/manual/en/function.parse-url.php).
The code is taken from gist.github.com/Ellrion
$parts1 = [ 'scheme' => 'http', 'host' => 'mysite.dev', ]; $parts2 = [ 'scheme' => 'https', 'host' => 'mysite.dev', 'port' => 1234, 'user' => 'foo', 'pass' => 'bar', 'path' => '/category/subcategory', 'query' => 'page=1', 'fragment' => 'section=5', ]; return build_url($parts1) : string return (new \Helldar\Helpers\Support\Http($parts2)) ->buildUrl(); // returned 1: http://mysite.dev // returned 2: https://foo:bar@mysite.dev:1234/category/subcategory?page=1#section=5
subdomain_name()
Retrieving the current subdomain name.
return subdomain_name(); return (new \Helldar\Helpers\Support\Http()) ->getSubdomain(); // from domain `test.mysite.local` will return `test` (string). // from domain `mysite.local` will return `null` (null).
Jsonable
Convert the object to its JSON representation.
The code is taken from gist.github.com/Ellrion
use Helldar\Traits\Jsonable; class MyClass { use Jsonable; public function __construct($data) { $this->setData($data); } } $obj = new MyClass($data); echo $obj->toJson();
Images
image_or_default()
Check the existence of the file and return the default value if it is missing:
echo image_or_default(string $filename, $default = null) : string echo (new \Helldar\Helpers\Support\Images($filename)) ->imageOrDefault($default = null) : string
Notifications
Notification helpers were placed in a separate package andrey-helldar/notify-exceptions.
Strings
str_choice()
The str_choice function translates the given language line with inflection:
echo str_choice(int $num, array $choice = [], string $additional = '') : string echo (new \Helldar\Helpers\Support\Str($num)) ->choice(array $choice = [], string $additional = '') : string
Example:
echo str_choice(1, ['пользователь', 'пользователя', 'пользователей']); // returned "пользователь" echo str_choice(3, ['пользователь', 'пользователя', 'пользователей']); // returned "пользователя" echo str_choice(20, ['пользователь', 'пользователя', 'пользователей']); // returned "пользователей" echo str_choice(20, ['пользователь', 'пользователя', 'пользователей'], 'здесь'); // returned "пользователей здесь"
e()
Escape HTML special characters in a string:
echo e($value) : string echo (new \Helldar\Helpers\Support\Str($value)) ->e() : string
de()
Convert special HTML entities back to characters:
echo de($value) : string echo (new \Helldar\Helpers\Support\Str($value)) ->de() : string
str_replace_spaces()
Replacing multiple spaces with a single space.
echo str_replace_spaces($value) : string echo (new \Helldar\Helpers\Support\Str($value)) ->replaceSpaces() : string
Systems
is_windows()
Determine whether the current environment is Windows based:
return is_windows() : bool return (new \Helldar\Helpers\Support\System()) ->isWindows() : bool
is_linux()
Determine whether the current environment is Linux based:
return is_linux() : bool return (new \Helldar\Helpers\Support\System()) ->isLinux() : bool
Copyright and License
Helpers was written by Andrey Helldar for the Laravel framework 5.5 or later, and is released under the MIT License. See the LICENSE file for details.
Translation
Translations of text and comment by Google Translate. Help with translation +1 in karma :)