From 061468a3575fe590525f9052149a8f8a806a7588 Mon Sep 17 00:00:00 2001 From: Anthony Rappa Date: Fri, 1 Oct 2021 13:33:16 -0400 Subject: [PATCH 1/2] Update to Laravel 8 --- composer.json | 11 +- readme.md | 1306 ++++++++++++++++++++++++----------- src/arrays.php | 906 ++++++++++++++++++++++++ src/classes.php | 65 ++ src/helpers.php | 1742 ++--------------------------------------------- src/strings.php | 670 ++++++++++++++++++ 6 files changed, 2586 insertions(+), 2114 deletions(-) create mode 100755 src/arrays.php create mode 100644 src/classes.php mode change 100755 => 100644 src/helpers.php create mode 100644 src/strings.php diff --git a/composer.json b/composer.json index b6ad4ca..3941f1d 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "rappasoft/laravel-helpers", "description": "Laravel Helpers for Non-Laravel Projects", "license": "MIT", - "keywords": ["laravel", "helpers"], + "keywords": ["laravel", "helpers", "string helpers", "array helpers"], "authors": [ { "name": "Anthony Rappa", @@ -10,12 +10,15 @@ } ], "require": { - "php": ">=5.4.0" + "php": "^7.3|^8.0" }, "autoload": { "files": [ - "src/helpers.php" + "src/arrays.php", + "src/classes.php", + "src/helpers.php", + "src/strings.php" ] }, "minimum-stability": "stable" -} \ No newline at end of file +} diff --git a/readme.md b/readme.md index edbb31e..4561eaa 100755 --- a/readme.md +++ b/readme.md @@ -1,213 +1,264 @@ -## Laravel 5 Helpers for Non-Laravel Projects +![Package Logo](https://banners.beyondco.de/Laravel%20Helpers.png?theme=dark&packageManager=composer+require&packageName=rappasoft%2Flaravel-helpers&pattern=architect&style=style_1&description=For+Non-Laravel+Projects&md=1&showWatermark=0&fontSize=100px&images=hand) -This project takes the useful [Laravel helper functions](http://laravel.com/docs/5.0/helpers) and allows you to use them in Non-Laravel projects. +## Laravel Helpers for Non-Laravel Projects -All dependencies have been extracted out to a single helpers file. No need to import half of Symphony and Laravel to make these work. +[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-helpers) +[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-helpers) -## License - -Since the Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT), this project is licensed under the same license. +This project takes the useful [Laravel helper functions](https://laravel.com/docs/8.x/helpers#introduction) and allows you to use them in Non-Laravel projects. -## Setup +## Installation -Run the following in your project root: - - composer require rappasoft/laravel-helpers +``` +composer require rappasoft/laravel-helpers +``` ## Documentation * [Arrays](#arrays) - * [append_config](#append_config) + * [array_accessible](#array_accessible) * [array_add](#array_add) - * [array_build](#array_build) + * [array_collapse](#array_collapse) + * [array_cross_join](#array_cross_join) * [array_divide](#array_divide) * [array_dot](#array_dot) * [array_except](#array_except) - * [array_fetch](#array_fetch) + * [array_exists](#array_exists) * [array_first](#array_first) * [array_last](#array_last) * [array_flatten](#array_flatten) * [array_forget](#array_forget) * [array_get](#array_get) * [array_has](#array_has) + * [array_has_any](#array_has_any) + * [array_is_assoc](#array_is_assoc) * [array_only](#array_only) * [array_pluck](#array_pluck) + * [array_prepend](#array_prepend) * [array_pull](#array_pull) + * [array_query](#array_query) + * [array_random](#array_random) * [array_set](#array_set) + * [array_shuffle](#array_shuffle) + * [array_sort_recursive](#array_sort_recursive) + * [array_to_css_classes](#array_to_css_classes) * [array_where](#array_where) + * [array_wrap](#array_wrap) + * [data_fill](#data_fill) + * [data_get](#data_get) + * [data_set](#data_set) * [head](#head) * [last](#last) * [Strings](#strings) - * [ascii](#ascii) - * [camel_case](#camel_case) - * [charsArray](#charsArray) - * [ends_with](#ends_with) - * [preg_replace_sub](#preg_replace_sub) - * [snake_case](#snake_case) - * [starts_with](#starts_with) + * [preg_replace_array](#preg_replace_array) + * [str_after](#str_after) + * [str_after_last](#str_after_last) + * [str_before](#str_before) + * [str_before_last](#str_before_last) + * [str_between](#str_between) * [str_contains](#str_contains) + * [str_contains_all](#str_contains_all) + * [str_ends_with](#str_ends_with) * [str_finish](#str_finish) * [str_is](#str_is) + * [str_is_uuid](#str_is_uuid) + * [str_kebab](#str_kebab) + * [str_length](#str_length) * [str_limit](#str_limit) + * [str_lower](#str_lower) + * [str_words](#str_words) + * [str_match](#str_match) + * [str_pad_both](#str_pad_both) + * [str_pad_left](#str_pad_left) + * [str_pad_right](#str_pad_right) * [str_random](#str_random) * [str_replace_array](#str_replace_array) - * [str_slug](#str_slug) - * [studly_case](#studly_case) + * [str_replace_first](#str_replace_first) + * [str_replace_last](#str_replace_last) + * [str_remove](#str_remove) + * [str_start](#str_start) + * [str_upper](#str_upper) + * [str_title](#str_title) + * [str_snake](#str_snake) + * [str_starts_with](#str_starts_with) + * [str_studly](#str_studly) * [Classes](#classes) * [class_basename](#class_basename) * [class_uses_recursive](#class_uses_recursive) * [trait_uses_recursive](#trait_uses_recursive) -* [Misc.](#misc) - * [data_get](#data_get) - * [data_set](#data_set) - * [data_fill](#data_fill) +* [Miscellaneous](#miscellaneous) + * [dd](#dd) * [e](#e) * [object_get](#object_get) + * [tap](#tap) * [value](#value) * [with](#with) - * [dd](#dd) ## Arrays -### append_config - +### array_accessible + ```php /** - * Assign high numeric IDs to a config item to force appending. - * - * @param array $array - * @return array -*/ -function append_config(array $array) -``` + * Determine whether the given value is array accessible. + * + * @param mixed $value + * + * @return bool + */ +function array_accessible($value): bool +`````` ### array_add ```php /** - * Add an element to an array using "dot" notation if it doesn't exist. - * - * @param array $array - * @param string $key - * @param mixed $value - * @return array -*/ -function array_add($array, $key, $value) -``` + * Add an element to an array using "dot" notation if it doesn't exist. + * + * @param array $array + * @param string $key + * @param mixed $value + * + * @return array + */ +function array_add(array $array, string $key, $value): array +```` -### array_build - +### array_collapse + ```php /** - * Build a new array using a callback. - * - * @param array $array - * @param \Closure $callback - * @return array -*/ -function array_build($array, Closure $callback) -``` + * Collapse an array of arrays into a single array. + * + * @param iterable $array + * + * @return array + */ +function array_collapse(iterable $array): array +```` + +### array_cross_join + + +```php +/** + * Cross join the given arrays, returning all possible permutations. + * + * @param iterable ...$arrays + * + * @return array + */ +function array_cross_join(...$arrays): array +```` ### array_divide ```php /** - * Divide an array into two arrays. One with keys and the other with values. - * - * @param array $array - * @return array -*/ -function array_divide($array) -``` + * Divide an array into two arrays. One with keys and the other with values. + * + * @param array $array + * + * @return array + */ +function array_divide(array $array): array +```` ### array_dot ```php /** - * Flatten a multi-dimensional associative array with dots. - * - * @param array $array - * @param string $prepend - * @return array -*/ -function array_dot($array, $prepend = '') -``` + * Flatten a multi-dimensional associative array with dots. + * + * @param iterable $array + * @param string $prepend + * + * @return array + */ +function array_dot(iterable $array, string $prepend = ''): array +```` ### array_except ```php /** - * Get all of the given array except for a specified array of items. - * - * @param array $array - * @param array|string $keys - * @return array -*/ -function array_except($array, $keys) -``` + * Get all of the given array except for a specified array of keys. + * + * @param array $array + * @param array|string $keys + * + * @return array + */ +function array_except(array $array, $keys): array +```` -### array_fetch - +### array_exists + ```php /** - * Fetch a flattened array of a nested array element. - * - * @param array $array - * @param string $key - * @return array -*/ -function array_fetch($array, $key) -``` + * Determine if the given key exists in the provided array. + * + * @param \ArrayAccess|array $array + * @param string|int $key + * + * @return bool + */ +function array_exists($array, $key): bool +```` ### array_first ```php /** - * Return the first element in an array passing a given truth test. - * - * @param array $array - * @param \Closure $callback - * @param mixed $default - * @return mixed -*/ -function array_first($array, $callback, $default = null) -``` + * Return the first element in an array passing a given truth test. + * + * @param iterable $array + * @param callable|null $callback + * @param mixed $default + * + * @return mixed + */ +function array_first(iterable $array, callable $callback = null, $default = null) +```` ### array_last ```php /** - * Return the last element in an array passing a given truth test. - * - * @param array $array - * @param \Closure $callback - * @param mixed $default - * @return mixed -*/ -function array_last($array, $callback, $default = null) -``` + * Return the last element in an array passing a given truth test. + * + * @param array $array + * @param callable|null $callback + * @param mixed $default + * + * @return mixed + */ +function array_last(array $array, callable $callback = null, $default = null) +```` ### array_flatten ```php /** - * Flatten a multi-dimensional array into a single level. - * - * @param array $array - * @return array -*/ -function array_flatten($array) + * Flatten a multi-dimensional array into a single level. + * + * @param iterable $array + * @param int $depth + * + * @return array + */ +function array_flatten(iterable $array, int $depth): array ``` ### array_forget @@ -215,13 +266,14 @@ function array_flatten($array) ```php /** - * Remove one or many array items from a given array using "dot" notation. - * - * @param array $array - * @param array|string $keys - * @return void -*/ -function array_forget(&$array, $keys) + * Remove one or many array items from a given array using "dot" notation. + * + * @param array $array + * @param array|string $keys + * + * @return void + */ +function array_forget(array &$array, $keys) ``` ### array_get @@ -229,13 +281,14 @@ function array_forget(&$array, $keys) ```php /** - * Get an item from an array using "dot" notation. - * - * @param array $array - * @param string $key - * @param mixed $default - * @return mixed -*/ + * Get an item from an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|int|null $key + * @param mixed $default + * + * @return mixed + */ function array_get($array, $key, $default = null) ``` @@ -244,13 +297,45 @@ function array_get($array, $key, $default = null) ```php /** - * Check if an item exists in an array using "dot" notation. - * - * @param array $array - * @param string $key - * @return bool -*/ -function array_has($array, $key) + * Check if an item or items exist in an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|array $keys + * + * @return bool + */ +function array_has($array, $keys): bool +``` + +### array_has_any + + +```php + /** + * Determine if any of the keys exist in an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|array $keys + * + * @return bool + */ +function array_has_any($array, $keys): bool +``` + +### array_is_assoc + + +```php +/** + * Determines if an array is associative. + * + * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. + * + * @param array $array + * + * @return bool + */ +function array_is_assoc(array $array): bool ``` ### array_only @@ -258,13 +343,14 @@ function array_has($array, $key) ```php /** - * Get a subset of the items from the given array. - * - * @param array $array - * @param array|string $keys - * @return array -*/ -function array_only($array, $keys) + * Get a subset of the items from the given array. + * + * @param array $array + * @param array|string $keys + * + * @return array + */ +function array_only(array $array, $keys): array ``` ### array_pluck @@ -272,14 +358,31 @@ function array_only($array, $keys) ```php /** - * Pluck an array of values from an array. - * - * @param array $array - * @param string $value - * @param string $key - * @return array -*/ -function array_pluck($array, $value, $key = null) + * Pluck an array of values from an array. + * + * @param iterable $array + * @param string|array|int|null $value + * @param string|array|null $key + * + * @return array + */ +function array_pluck(iterable $array, $value, $key = null): array +``` + +### array_prepend + + +```php +/** + * Push an item onto the beginning of an array. + * + * @param array $array + * @param mixed $value + * @param mixed $key + * + * @return array + */ +function array_prepend(array $array, $value, $key = null): array ``` ### array_pull @@ -287,14 +390,47 @@ function array_pluck($array, $value, $key = null) ```php /** - * Get a value from the array, and remove it. - * - * @param array $array - * @param string $key - * @param mixed $default - * @return mixed -*/ -function array_pull(&$array, $key, $default = null) + * Get a value from the array, and remove it. + * + * @param array $array + * @param string $key + * @param mixed $default + * + * @return mixed + */ +function array_pull(array &$array, string $key, $default = null) +``` + +### array_query + + +```php +/** + * Convert the array into a query string. + * + * @param array $array + * + * @return string + */ +function array_query(array $array): string +``` + +### array_random + + +```php +/** + * Get one or a specified number of random values from an array. + * + * @param array $array + * @param int|null $number + * @param bool|false $preserveKeys + * + * @return mixed + * + * @throws \InvalidArgumentException + */ +function array_random(array $array, int $number = null, bool $preserveKeys) ``` ### array_set @@ -302,16 +438,62 @@ function array_pull(&$array, $key, $default = null) ```php /** - * Set an array item to a given value using "dot" notation. - * - * If no key is given to the method, the entire array will be replaced. - * - * @param array $array - * @param string $key - * @param mixed $value - * @return array -*/ -function array_set(&$array, $key, $value) + * Set an array item to a given value using "dot" notation. + * + * If no key is given to the method, the entire array will be replaced. + * + * @param array $array + * @param string|null $key + * @param mixed $value + * + * @return array + */ +function array_set(array &$array, ?string $key, $value): array +``` + +### array_shuffle + + +```php + /** + * Shuffle the given array and return the result. + * + * @param array $array + * @param int|null $seed + * + * @return array + */ +function array_shuffle(array $array, int $seed = null): array +``` + +### array_sort_recursive + + +```php +/** + * Recursively sort an array by keys and values. + * + * @param array $array + * @param int $options + * @param bool $descending + * + * @return array + */ +function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending): array +``` + +### array_to_css_classes + + +```php +/** + * Conditionally compile classes from an array into a CSS class list. + * + * @param array $array + * + * @return string + */ +function array_to_css_classes(array $array): string ``` ### array_where @@ -319,13 +501,75 @@ function array_set(&$array, $key, $value) ```php /** - * Filter the array using the given Closure. - * - * @param array $array - * @param \Closure $callback - * @return array -*/ -function array_where($array, Closure $callback) + * Filter the array using the given callback. + * + * @param array $array + * @param callable $callback + * + * @return array + */ +function array_where(array $array, callable $callback): array +``` + +### array_wrap + + +```php +/** + * If the given value is not an array and not null, wrap it in one. + * + * @param mixed $value + * + * @return array + */ +function array_wrap($value): array +```` + +### data_fill + + +```php +/** + * Fill in data where it's missing. + * + * @param mixed $target + * @param string|array $key + * @param mixed $value + * @return mixed + */ +function data_fill(&$target, $key, $value) +``` + +### data_get + + +```php +/** + * Get an item from an array or object using "dot" notation. + * + * @param mixed $target + * @param string|array|int|null $key + * @param mixed $default + * @return mixed + */ +function data_get($target, $key, $default = null) +``` + +### data_set + + +```php +/** + * Set an item on an array or object using dot notation. + * + * @param mixed $target + * @param string|array $key + * @param mixed $value + * @param bool $overwrite + * + * @return mixed + */ +function data_set(&$target, $key, $value, bool $overwrite = true) ``` ### head @@ -333,12 +577,13 @@ function array_where($array, Closure $callback) ```php /** - * Get the first element of an array. Useful for method chaining. - * - * @param array $array - * @return mixed -*/ -function head($array) + * Get the first element of an array. Useful for method chaining. + * + * @param array $array + * + * @return mixed + */ +function head(array $array) ``` ### last @@ -346,128 +591,154 @@ function head($array) ```php /** - * Get the last element from an array. - * - * @param array $array - * @return mixed -*/ -function last($array) + * Get the last element from an array. + * + * @param array $array + * + * @return mixed + */ +function last(array $array) ``` + ## Strings -### ascii - +### preg_replace_array + ```php /** - * Transliterate a UTF-8 value to ASCII. - * - * @param string $value - * @return string + * Replace a given pattern with each value in the array in sequentially. + * + * @param string $pattern + * @param array $replacements + * @param string $subject + * + * @return string */ -function ascii($value) +function preg_replace_array(string $pattern, array $replacements, string $subject): string ``` -### camel_case - +### str_after + ```php /** - * Convert a value to camel case. - * - * @param string $value - * @return string -*/ -function camel_case($value) + * Return the remainder of a string after the first occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ +function str_after(string $subject, string $search): string ``` -### charsArray - +### str_after_last + ```php /** - * Returns the replacements for the ascii method. - * - * Note: Adapted from Stringy\Stringy. - * - * @see https://github.com/danielstjules/Stringy/blob/2.3.1/LICENSE.txt - * - * @return array + * Return the remainder of a string after the last occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string */ -function charsArray() +function str_after_last(string $subject, string $search): string ``` -### ends_with - +### str_before + ```php /** - * Determine if a given string ends with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool -*/ -function ends_with($haystack, $needles) + * Get the portion of a string before the first occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ +function str_before(string $subject, string $search): string ``` -### preg_replace_sub - +### str_before_last + ```php /** - * Replace a given pattern with each value in the array in sequentially. - * - * @param string $pattern - * @param array $replacements - * @param string $subject - * @return string -*/ -function preg_replace_sub($pattern, &$replacements, $subject) + * Get the portion of a string before the last occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ +function str_before_last(string $subject, string $search): string ``` -### snake_case - +### str_between + ```php /** - * Convert a string to snake case. - * - * @param string $value - * @param string $delimiter - * @return string -*/ -function snake_case($value, $delimiter = '_') + * Get the portion of a string between two given values. + * + * @param string $subject + * @param string $from + * @param string $to + * + * @return string + */ +function str_between(string $subject, string $from, string $to): string +``` + +### str_contains + + +```php + /** + * Determine if a given string contains a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ +function str_contains(string $haystack, $needles): bool ``` -### starts_with - +### str_contains_all + ```php /** - * Determine if a given string starts with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool -*/ -function starts_with($haystack, $needles) + * Determine if a given string contains all array values. + * + * @param string $haystack + * @param string[] $needles + * + * @return bool + */ +function str_contains_all(string $haystack, array $needles): bool ``` -### str_contains - +### str_ends_with + ```php /** - * Determine if a given string contains a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool -*/ -function str_contains($haystack, $needles) + * Determine if a given string ends with a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ +function str_ends_with(string $haystack, $needles): bool ``` ### str_finish @@ -475,13 +746,14 @@ function str_contains($haystack, $needles) ```php /** - * Cap a string with a single instance of a given value. - * - * @param string $value - * @param string $cap - * @return string -*/ -function str_finish($value, $cap) + * Cap a string with a single instance of a given value. + * + * @param string $value + * @param string $cap + * + * @return string + */ +function str_finish(string $value, string $cap): string ``` ### str_is @@ -489,13 +761,57 @@ function str_finish($value, $cap) ```php /** - * Determine if a given string matches a given pattern. - * - * @param string $pattern - * @param string $value - * @return bool -*/ -function str_is($pattern, $value) + * Determine if a given string matches a given pattern. + * + * @param string|array $pattern + * @param string $value + * + * @return bool + */ +function str_is($pattern, string $value): bool +``` + +### str_is_uuid + + +```php +/** + * Determine if a given string is a valid UUID. + * + * @param string $value + * + * @return bool + */ +function str_is_uuid(string $value): bool +``` + +### str_kebab + + +```php +/** + * Convert a string to kebab case. + * + * @param string $value + * + * @return string + */ +function str_kebab(string $value): string +``` + +### str_length + + +```php +/** + * Return the length of the given string. + * + * @param string $value + * @param string|null $encoding + * + * @return int + */ +function str_length(string $value, string $encoding = null): int ``` ### str_limit @@ -503,14 +819,108 @@ function str_is($pattern, $value) ```php /** - * Limit the number of characters in a string. - * - * @param string $value - * @param int $limit - * @param string $end - * @return string -*/ -function str_limit($value, $limit = 100, $end = '...') + * Limit the number of characters in a string. + * + * @param string $value + * @param int $limit + * @param string $end + * + * @return string + */ +function str_limit(string $value, int $limit = 100, string $end = '...'): string +``` + +### str_lower + + +```php +/** + * Convert the given string to lower-case. + * + * @param string $value + * + * @return string + */ +function str_lower(string $value): string +``` + +### str_words + + +```php +/** + * Limit the number of words in a string. + * + * @param string $value + * @param int $words + * @param string $end + * + * @return string + */ +function str_words(string $value, int $words = 100, string $end = '...'): string +``` + +### str_match + + +```php +/** + * Get the string matching the given pattern. + * + * @param string $pattern + * @param string $subject + * + * @return string + */ +function str_match(string $pattern, string $subject): string +``` + +### str_pad_both + + +```php +/** + * Pad both sides of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ +function str_pad_both(string $value, int $length, string $pad = ' '): string +``` + +### str_pad_left + + +```php +/** + * Pad the left side of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ +function str_pad_left(string $value, int $length, string $pad = ' '): string +``` + +### str_pad_right + + +```php +/** + * Pad the right side of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ +function str_pad_right(string $value, int $length, string $pad = ' '): string ``` ### str_random @@ -518,14 +928,13 @@ function str_limit($value, $limit = 100, $end = '...') ```php /** - * Generate a more truly "random" alpha-numeric string. - * - * @param int $length - * @return string - * - * @throws \RuntimeException -*/ -function str_random($length = 16) + * Generate a more truly "random" alpha-numeric string. + * + * @param int $length + * + * @return string + */ +function str_random(int $length = 16): string ``` ### str_replace_array @@ -533,41 +942,150 @@ function str_random($length = 16) ```php /** - * Replace a given value in the string sequentially with an array. - * - * @param string $search - * @param array $replace - * @param string $subject - * @return string -*/ -function str_replace_array($search, array $replace, $subject) + * Replace a given value in the string sequentially with an array. + * + * @param string $search + * @param array $replace + * @param string $subject + * + * @return string + */ +function str_replace_array(string $search, array $replace, string $subject): string +``` + +### str_replace_first + + +```php +/** + * Replace the first occurrence of a given value in the string. + * + * @param string $search + * @param string $replace + * @param string $subject + * + * @return string + */ +function str_replace_first(string $search, string $replace, string $subject): string ``` -### str_slug - +### str_replace_last + ```php /** - * Generate a URL friendly "slug" from a given string. - * - * @param string $title - * @param string $separator - * @return string -*/ -function str_slug(string $title, string $separator = '-') + * Replace the last occurrence of a given value in the string. + * + * @param string $search + * @param string $replace + * @param string $subject + * + * @return string + */ +function str_replace_last(string $search, string $replace, string $subject): string ``` -### studly_case - +### str_remove + ```php /** - * Convert a value to studly caps case. - * - * @param string $value - * @return string -*/ -function studly_case($value) + * Remove any occurrence of the given string in the subject. + * + * @param string|array $search + * @param string $subject + * @param bool $caseSensitive + * + * @return string + */ +function str_remove($search, string $subject, bool $caseSensitive = true): string +``` + +### str_start + + +```php +/** + * Begin a string with a single instance of a given value. + * + * @param string $value + * @param string $prefix + * + * @return string + */ +function str_start(string $value, string $prefix): string +``` + +### str_upper + + +```php +/** + * Convert the given string to upper-case. + * + * @param string $value + * + * @return string + */ +function str_upper(string $value): string +``` + +### str_title + + +```php +/** + * Convert the given string to title case. + * + * @param string $value + * + * @return string + */ +function str_title(string $value): string +``` + +### str_snake + + +```php +/** + * Convert a string to snake case. + * + * @param string $value + * @param string $delimiter + * + * @return string + */ +function str_snake(string $value, string $delimiter = '_'): string +``` + +### str_starts_with + + +```php +/** + * Determine if a given string starts with a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ +function str_starts_with(string $haystack, $needles): bool +``` + +### str_studly + + +```php + /** + * Convert a value to studly caps case. + * + * @param string $value + * + * @return string + */ +function str_studly(string $value): string ``` ## Classes @@ -578,12 +1096,12 @@ function studly_case($value) ```php /** - * Get the class "basename" of the given object / class. - * - * @param string|object $class - * @return string -*/ -function class_basename($class) + * Get the class "basename" of the given object / class. + * + * @param string|object $class + * @return string + */ +function class_basename($class): string ``` ### class_uses_recursive @@ -591,12 +1109,12 @@ function class_basename($class) ```php /** - * Returns all traits used by a class, it's subclasses and trait of their traits - * - * @param string $class - * @return array -*/ -function class_uses_recursive($class) + * Returns all traits used by a class, its parent classes and trait of their traits. + * + * @param object|string $class + * @return array + */ +function class_uses_recursive($class): array ``` ### trait_uses_recursive @@ -604,30 +1122,28 @@ function class_uses_recursive($class) ```php /** - * Returns all traits used by a trait and its traits - * - * @param string $trait - * @return array -*/ -function trait_uses_recursive($trait) + * Returns all traits used by a trait and its traits. + * + * @param string $trait + * @return array + */ +function trait_uses_recursive($trait): array ``` -## Misc. - +## Miscellaneous + -### data_get - +### dd + ```php /** - * Get an item from an array or object using "dot" notation. - * - * @param mixed $target - * @param string $key - * @param mixed $default - * @return mixed -*/ -function data_get($target, $key, $default = null) + * Dump the passed variables and end the script. + * + * @param mixed + * @return void + */ +function dd() ``` ### e @@ -635,27 +1151,44 @@ function data_get($target, $key, $default = null) ```php /** - * Escape HTML entities in a string. - * - * @param string $value - * @return string -*/ -function e($value) + * Escape HTML entities in a string. + * + * @param string $value + * + * @return string + */ +function e(string $value): string ``` ### object_get +```php + /** + * Get an item from an object using "dot" notation. + * + * @param object $object + * @param string $key + * @param mixed $default + * + * @return mixed + */ +function object_get(object $object, string $key, $default = null) +``` + +### tap + + ```php /** - * Get an item from an object using "dot" notation. - * - * @param object $object - * @param string $key - * @param mixed $default - * @return mixed -*/ -function object_get($object, $key, $default = null) + * Call the given Closure with the given value then return the value. + * + * @param mixed $value + * @param callable $callback + * + * @return mixed + */ +function tap($value, callable $callback) ``` ### value @@ -663,11 +1196,11 @@ function object_get($object, $key, $default = null) ```php /** - * Return the default value of the given value. - * - * @param mixed $value - * @return mixed -*/ + * Return the default value of the given value. + * + * @param mixed $value + * @return mixed + */ function value($value) ``` @@ -676,54 +1209,15 @@ function value($value) ```php /** - * Return the given object. Useful for chaining. - * - * @param mixed $object - * @return mixed -*/ + * Return the given object. Useful for chaining. + * + * @param mixed $object + * @return mixed + */ function with($object) ``` -### dd - -```php - /** - * Dump the passed variables and end the script. - * - * @param mixed $args - * @return void -*/ -function dd($arg...) -``` - -### data_set - - -```php - /** - * Set an item on an array or object using dot notation. - * - * @param mixed $target - * @param string|array $key - * @param mixed $value - * @param bool $overwrite - * @return mixed - */ - function data_set(&$target, $key, $value, $overwrite = true) -``` - -### data_fill - +## License -```php -/** - * Fill in data where it's missing. - * - * @param mixed $target - * @param string|array $key - * @param mixed $value - * @return mixed -*/ -function data_fill(&$target, $key, $value) -``` \ No newline at end of file +Since the Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT), this project is licensed under the same license. diff --git a/src/arrays.php b/src/arrays.php new file mode 100755 index 0000000..cab6735 --- /dev/null +++ b/src/arrays.php @@ -0,0 +1,906 @@ + $array) { + $append = []; + + foreach ($results as $product) { + foreach ($array as $item) { + $product[$index] = $item; + + $append[] = $product; + } + } + + $results = $append; + } + + return $results; + } +} + +if (! function_exists('array_divide')) { + /** + * Divide an array into two arrays. One with keys and the other with values. + * + * @param array $array + * + * @return array + */ + function array_divide(array $array): array + { + return [array_keys($array), array_values($array)]; + } +} + +if (! function_exists('array_dot')) { + /** + * Flatten a multi-dimensional associative array with dots. + * + * @param iterable $array + * @param string $prepend + * + * @return array + */ + function array_dot(iterable $array, string $prepend = ''): array + { + $results = []; + + foreach ($array as $key => $value) { + if (is_array($value) && !empty($value)) { + $results = array_merge($results, array_dot($value, $prepend.$key.'.')); + } else { + $results[$prepend.$key] = $value; + } + } + + return $results; + } +} + +if (! function_exists('array_except')) { + /** + * Get all of the given array except for a specified array of keys. + * + * @param array $array + * @param array|string $keys + * + * @return array + */ + function array_except(array $array, $keys): array + { + array_forget($array, $keys); + + return $array; + } +} + +if (! function_exists('array_exists')) { + /** + * Determine if the given key exists in the provided array. + * + * @param \ArrayAccess|array $array + * @param string|int $key + * + * @return bool + */ + function array_exists($array, $key): bool + { + if ($array instanceof ArrayAccess) { + return $array->offsetExists($key); + } + + return array_key_exists($key, $array); + } +} + +if (! function_exists('array_first')) { + /** + * Return the first element in an array passing a given truth test. + * + * @param iterable $array + * @param callable|null $callback + * @param mixed $default + * + * @return mixed + */ + function array_first(iterable $array, callable $callback = null, $default = null) + { + if (is_null($callback)) { + if (empty($array)) { + return value($default); + } + + foreach ($array as $item) { + return $item; + } + } + + foreach ($array as $key => $value) { + if ($callback($value, $key)) { + return $value; + } + } + + return value($default); + } +} + +if (! function_exists('array_last')) { + /** + * Return the last element in an array passing a given truth test. + * + * @param array $array + * @param callable|null $callback + * @param mixed $default + * + * @return mixed + */ + function array_last(array $array, callable $callback = null, $default = null) + { + if (is_null($callback)) { + return empty($array) ? value($default) : end($array); + } + + return array_first(array_reverse($array, true), $callback, $default); + } +} + +if (! function_exists('array_flatten')) { + /** + * Flatten a multi-dimensional array into a single level. + * + * @param iterable $array + * @param int $depth + * + * @return array + */ + function array_flatten(iterable $array, int $depth): array + { + $result = []; + + foreach ($array as $item) { + if (!is_array($item)) { + $result[] = $item; + } else { + $values = $depth === 1 + ? array_values($item) + : array_flatten($item, $depth - 1); + + foreach ($values as $value) { + $result[] = $value; + } + } + } + + return $result; + } +} + +if (! function_exists('array_forget')) { + /** + * Remove one or many array items from a given array using "dot" notation. + * + * @param array $array + * @param array|string $keys + * + * @return void + */ + function array_forget(array &$array, $keys) + { + $original = &$array; + + $keys = (array) $keys; + + if (count($keys) === 0) { + return; + } + + foreach ($keys as $key) { + // if the exact key exists in the top-level, remove it + if (array_exists($array, $key)) { + unset($array[$key]); + + continue; + } + + $parts = explode('.', $key); + + // clean up before each pass + $array = &$original; + + while (count($parts) > 1) { + $part = array_shift($parts); + + if (isset($array[$part]) && is_array($array[$part])) { + $array = &$array[$part]; + } else { + continue 2; + } + } + + unset($array[array_shift($parts)]); + } + } +} + +if (! function_exists('array_get')) { + /** + * Get an item from an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|int|null $key + * @param mixed $default + * + * @return mixed + */ + function array_get($array, $key, $default = null) + { + if (!array_accessible($array)) { + return value($default); + } + + if (is_null($key)) { + return $array; + } + + if (array_exists($array, $key)) { + return $array[$key]; + } + + if (strpos($key, '.') === false) { + return $array[$key] ?? value($default); + } + + foreach (explode('.', $key) as $segment) { + if (array_accessible($array) && array_exists($array, $segment)) { + $array = $array[$segment]; + } else { + return value($default); + } + } + + return $array; + } +} + +if (! function_exists('array_has')) { + /** + * Check if an item or items exist in an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|array $keys + * + * @return bool + */ + function array_has($array, $keys): bool + { + $keys = (array) $keys; + + if (!$array || $keys === []) { + return false; + } + + foreach ($keys as $key) { + $subKeyArray = $array; + + if (array_exists($array, $key)) { + continue; + } + + foreach (explode('.', $key) as $segment) { + if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) { + $subKeyArray = $subKeyArray[$segment]; + } else { + return false; + } + } + } + + return true; + } +} + +if (! function_exists('array_has_any')) { + /** + * Determine if any of the keys exist in an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|array $keys + * + * @return bool + */ + function array_has_any($array, $keys): bool + { + if (is_null($keys)) { + return false; + } + + $keys = (array) $keys; + + if (!$array) { + return false; + } + + if ($keys === []) { + return false; + } + + foreach ($keys as $key) { + if (array_has($array, $key)) { + return true; + } + } + + return false; + } +} + +if (! function_exists('array_is_assoc')) { + /** + * Determines if an array is associative. + * + * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. + * + * @param array $array + * + * @return bool + */ + function array_is_assoc(array $array): bool + { + $keys = array_keys($array); + + return array_keys($keys) !== $keys; + } +} + +if (! function_exists('array_only')) { + /** + * Get a subset of the items from the given array. + * + * @param array $array + * @param array|string $keys + * + * @return array + */ + function array_only(array $array, $keys): array + { + return array_intersect_key($array, array_flip((array) $keys)); + } +} + +if (! function_exists('array_pluck')) { + /** + * Pluck an array of values from an array. + * + * @param iterable $array + * @param string|array|int|null $value + * @param string|array|null $key + * + * @return array + */ + function array_pluck(iterable $array, $value, $key = null): array + { + $results = []; + + $value = is_string($value) ? explode('.', $value) : $value; + + $key = is_null($key) || is_array($key) ? $key : explode('.', $key); + + foreach ($array as $item) { + $itemValue = data_get($item, $value); + + // If the key is "null", we will just append the value to the array and keep + // looping. Otherwise we will key the array using the value of the key we + // received from the developer. Then we'll return the final array form. + if (is_null($key)) { + $results[] = $itemValue; + } else { + $itemKey = data_get($item, $key); + + if (is_object($itemKey) && method_exists($itemKey, '__toString')) { + $itemKey = (string) $itemKey; + } + + $results[$itemKey] = $itemValue; + } + } + + return $results; + } +} + +if (! function_exists('array_prepend')) { + /** + * Push an item onto the beginning of an array. + * + * @param array $array + * @param mixed $value + * @param mixed $key + * + * @return array + */ + function array_prepend(array $array, $value, $key = null): array + { + if (func_num_args() === 2) { + array_unshift($array, $value); + } else { + $array = [$key => $value] + $array; + } + + return $array; + } +} + +if (! function_exists('array_pull')) { + /** + * Get a value from the array, and remove it. + * + * @param array $array + * @param string $key + * @param mixed $default + * + * @return mixed + */ + function array_pull(array &$array, string $key, $default = null) + { + $value = array_get($array, $key, $default); + + array_forget($array, $key); + + return $value; + } +} + +if (! function_exists('array_query')) { + /** + * Convert the array into a query string. + * + * @param array $array + * + * @return string + */ + function array_query(array $array): string + { + return http_build_query($array, '', '&', PHP_QUERY_RFC3986); + } +} + +if (! function_exists('array_random')) { + /** + * Get one or a specified number of random values from an array. + * + * @param array $array + * @param int|null $number + * @param bool|false $preserveKeys + * + * @return mixed + * + * @throws \InvalidArgumentException + */ + function array_random(array $array, int $number = null, bool $preserveKeys) + { + $requested = is_null($number) ? 1 : $number; + + $count = count($array); + + if ($requested > $count) { + throw new InvalidArgumentException( + "You requested {$requested} items, but there are only {$count} items available." + ); + } + + if (is_null($number)) { + return $array[array_rand($array)]; + } + + if ((int) $number === 0) { + return []; + } + + $keys = array_rand($array, $number); + + $results = []; + + if ($preserveKeys) { + foreach ((array) $keys as $key) { + $results[$key] = $array[$key]; + } + } else { + foreach ((array) $keys as $key) { + $results[] = $array[$key]; + } + } + + return $results; + } +} + +if (! function_exists('array_set')) { + /** + * Set an array item to a given value using "dot" notation. + * + * If no key is given to the method, the entire array will be replaced. + * + * @param array $array + * @param string|null $key + * @param mixed $value + * + * @return array + */ + function array_set(array &$array, ?string $key, $value): array + { + if (is_null($key)) { + return $array = $value; + } + + $keys = explode('.', $key); + + foreach ($keys as $i => $key) { + if (count($keys) === 1) { + break; + } + + unset($keys[$i]); + + // If the key doesn't exist at this depth, we will just create an empty array + // to hold the next value, allowing us to create the arrays to hold final + // values at the correct depth. Then we'll keep digging into the array. + if (!isset($array[$key]) || !is_array($array[$key])) { + $array[$key] = []; + } + + $array = &$array[$key]; + } + + $array[array_shift($keys)] = $value; + + return $array; + } +} + +if (! function_exists('array_shuffle')) { + /** + * Shuffle the given array and return the result. + * + * @param array $array + * @param int|null $seed + * + * @return array + */ + function array_shuffle(array $array, int $seed = null): array + { + if (is_null($seed)) { + shuffle($array); + } else { + mt_srand($seed); + shuffle($array); + mt_srand(); + } + + return $array; + } +} + +if (! function_exists('array_sort_recursive')) { + /** + * Recursively sort an array by keys and values. + * + * @param array $array + * @param int $options + * @param bool $descending + * + * @return array + */ + function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending): array + { + foreach ($array as &$value) { + if (is_array($value)) { + $value = array_sort_recursive($value, $options, $descending); + } + } + + if (array_is_assoc($array)) { + $descending + ? krsort($array, $options) + : ksort($array, $options); + } else { + $descending + ? rsort($array, $options) + : sort($array, $options); + } + + return $array; + } +} + +if (! function_exists('array_to_css_classes')) { + /** + * Conditionally compile classes from an array into a CSS class list. + * + * @param array $array + * + * @return string + */ + function array_to_css_classes(array $array): string + { + $classList = array_wrap($array); + + $classes = []; + + foreach ($classList as $class => $constraint) { + if (is_numeric($class)) { + $classes[] = $constraint; + } elseif ($constraint) { + $classes[] = $class; + } + } + + return implode(' ', $classes); + } +} + +if (! function_exists('array_where')) { + /** + * Filter the array using the given callback. + * + * @param array $array + * @param callable $callback + * + * @return array + */ + function array_where(array $array, callable $callback): array + { + return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); + } +} + +if (! function_exists('array_wrap')) { + /** + * If the given value is not an array and not null, wrap it in one. + * + * @param mixed $value + * + * @return array + */ + function array_wrap($value): array + { + if (is_null($value)) { + return []; + } + + return is_array($value) ? $value : [$value]; + } +} + +if (! function_exists('data_fill')) { + /** + * Fill in data where it's missing. + * + * @param mixed $target + * @param string|array $key + * @param mixed $value + * @return mixed + */ + function data_fill(&$target, $key, $value) + { + return data_set($target, $key, $value, false); + } +} + +if (! function_exists('data_get')) { + /** + * Get an item from an array or object using "dot" notation. + * + * @param mixed $target + * @param string|array|int|null $key + * @param mixed $default + * @return mixed + */ + function data_get($target, $key, $default = null) + { + if (is_null($key)) { + return $target; + } + + $key = is_array($key) ? $key : explode('.', $key); + + foreach ($key as $i => $segment) { + unset($key[$i]); + + if (is_null($segment)) { + return $target; + } + + if ($segment === '*') { + if (! is_array($target)) { + return value($default); + } + + $result = []; + + foreach ($target as $item) { + $result[] = data_get($item, $key); + } + + return in_array('*', $key) ? array_collapse($result) : $result; + } + + if (array_accessible($target) && array_exists($target, $segment)) { + $target = $target[$segment]; + } elseif (is_object($target) && isset($target->{$segment})) { + $target = $target->{$segment}; + } else { + return value($default); + } + } + + return $target; + } +} + +if (! function_exists('data_set')) { + /** + * Set an item on an array or object using dot notation. + * + * @param mixed $target + * @param string|array $key + * @param mixed $value + * @param bool $overwrite + * + * @return mixed + */ + function data_set(&$target, $key, $value, bool $overwrite = true) + { + $segments = is_array($key) ? $key : explode('.', $key); + + if (($segment = array_shift($segments)) === '*') { + if (! array_accessible($target)) { + $target = []; + } + + if ($segments) { + foreach ($target as &$inner) { + data_set($inner, $segments, $value, $overwrite); + } + } elseif ($overwrite) { + foreach ($target as &$inner) { + $inner = $value; + } + } + } elseif (array_accessible($target)) { + if ($segments) { + if (! array_exists($target, $segment)) { + $target[$segment] = []; + } + + data_set($target[$segment], $segments, $value, $overwrite); + } elseif ($overwrite || ! array_exists($target, $segment)) { + $target[$segment] = $value; + } + } elseif (is_object($target)) { + if ($segments) { + if (! isset($target->{$segment})) { + $target->{$segment} = []; + } + + data_set($target->{$segment}, $segments, $value, $overwrite); + } elseif ($overwrite || ! isset($target->{$segment})) { + $target->{$segment} = $value; + } + } else { + $target = []; + + if ($segments) { + data_set($target[$segment], $segments, $value, $overwrite); + } elseif ($overwrite) { + $target[$segment] = $value; + } + } + + return $target; + } +} + +if (! function_exists('head')) { + /** + * Get the first element of an array. Useful for method chaining. + * + * @param array $array + * + * @return mixed + */ + function head(array $array) + { + return reset($array); + } +} + +if (! function_exists('last')) { + /** + * Get the last element from an array. + * + * @param array $array + * + * @return mixed + */ + function last(array $array) + { + return end($array); + } +} diff --git a/src/classes.php b/src/classes.php new file mode 100644 index 0000000..74fe8d4 --- /dev/null +++ b/src/classes.php @@ -0,0 +1,65 @@ + $class] as $class) { + $results += trait_uses_recursive($class); + } + + return array_unique($results); + } +} + + +if (! function_exists('trait_uses_recursive')) { + /** + * Returns all traits used by a trait and its traits. + * + * @param string $trait + * @return array + */ + function trait_uses_recursive($trait): array + { + $traits = class_uses($trait) ?: []; + + foreach ($traits as $trait) { + $traits += trait_uses_recursive($trait); + } + + return $traits; + } +} diff --git a/src/helpers.php b/src/helpers.php old mode 100755 new mode 100644 index 5e4d3ae..7de6f6a --- a/src/helpers.php +++ b/src/helpers.php @@ -1,1643 +1,67 @@ $value) { - if (is_numeric($key)) { - $start++; - - $array[$start] = array_pull($array, $key); - } - } - - return $array; - } -} - -if (!function_exists('array_add')) { - /** - * Add an element to an array using "dot" notation if it doesn't exist. - * - * @param array $array - * @param string $key - * @param mixed $value - * @return array - */ - function array_add($array, $key, $value) - { - if (is_null(get($array, $key))) { - set($array, $key, $value); - } - - return $array; - } -} - -if (!function_exists('array_build')) { - /** - * Build a new array using a callback. - * - * @param array $array - * @param \Closure $callback - * @return array - */ - function array_build($array, Closure $callback) - { - $results = array(); - - foreach ($array as $key => $value) { - list($innerKey, $innerValue) = call_user_func($callback, $key, $value); - - $results[$innerKey] = $innerValue; - } - - return $results; - } -} - -if (!function_exists('array_divide')) { - /** - * Divide an array into two arrays. One with keys and the other with values. - * - * @param array $array - * @return array - */ - function array_divide($array) - { - return array(array_keys($array), array_values($array)); - } -} - -if (!function_exists('array_dot')) { - /** - * Flatten a multi-dimensional associative array with dots. - * - * @param array $array - * @param string $prepend - * @return array - */ - function array_dot($array, $prepend = '') - { - $results = array(); - - foreach ($array as $key => $value) { - if (is_array($value)) { - $results = array_merge($results, dot($value, $prepend . $key . '.')); - } else { - $results[$prepend . $key] = $value; - } - } - - return $results; - } -} - -if (!function_exists('array_except')) { - /** - * Get all of the given array except for a specified array of items. - * - * @param array $array - * @param array|string $keys - * @return array - */ - function array_except($array, $keys) - { - return array_diff_key($array, array_flip((array)$keys)); - } -} - -if (!function_exists('array_fetch')) { - /** - * Fetch a flattened array of a nested array element. - * - * @param array $array - * @param string $key - * @return array - */ - function array_fetch($array, $key) - { - $results = array(); - - foreach (explode('.', $key) as $segment) { - foreach ($array as $value) { - if (array_key_exists($segment, $value = (array)$value)) { - $results[] = $value[$segment]; - } - } - - $array = array_values($results); - } - - return array_values($results); - } -} - -if (!function_exists('array_first')) { - /** - * Return the first element in an array passing a given truth test. - * - * @param array $array - * @param \Closure $callback - * @param mixed $default - * @return mixed - */ - function array_first($array, $callback, $default = null) - { - foreach ($array as $key => $value) { - if (call_user_func($callback, $key, $value)) { - return $value; - } - } - - return value($default); - } -} - -if (!function_exists('array_last')) { - /** - * Return the last element in an array passing a given truth test. - * - * @param array $array - * @param \Closure $callback - * @param mixed $default - * @return mixed - */ - function array_last($array, $callback, $default = null) - { - return first(array_reverse($array), $callback, $default); - } -} - -if (!function_exists('array_flatten')) { - /** - * Flatten a multi-dimensional array into a single level. - * - * @param array $array - * @return array - */ - function array_flatten($array) - { - $return = array(); - - array_walk_recursive($array, function ($x) use (&$return) { - $return[] = $x; - }); - - return $return; - } -} - -if (!function_exists('array_forget')) { - /** - * Remove one or many array items from a given array using "dot" notation. - * - * @param array $array - * @param array|string $keys - * @return void - */ - function array_forget(&$array, $keys) - { - $original =& $array; - - foreach ((array)$keys as $key) { - $parts = explode('.', $key); - - while (count($parts) > 1) { - $part = array_shift($parts); - - if (isset($array[$part]) && is_array($array[$part])) { - $array =& $array[$part]; - } - } - - unset($array[array_shift($parts)]); - - // clean up after each pass - $array =& $original; - } - } -} - -if (!function_exists('array_get')) { - /** - * Get an item from an array using "dot" notation. - * - * @param array $array - * @param string $key - * @param mixed $default - * @return mixed - */ - function array_get($array, $key, $default = null) - { - if (is_null($key)) { - return $array; - } - - if (isset($array[$key])) { - return $array[$key]; - } - - foreach (explode('.', $key) as $segment) { - if (!is_array($array) || !array_key_exists($segment, $array)) { - return value($default); - } - - $array = $array[$segment]; - } - - return $array; - } -} - -if (!function_exists('array_has')) { - /** - * Check if an item exists in an array using "dot" notation. - * - * @param array $array - * @param string $key - * @return bool - */ - function array_has($array, $key) - { - if (empty($array) || is_null($key)) { - return false; - } - - if (array_key_exists($key, $array)) { - return true; - } - - foreach (explode('.', $key) as $segment) { - if (!is_array($array) || !array_key_exists($segment, $array)) { - return false; - } - - $array = $array[$segment]; - } - - return true; - } -} - -if (!function_exists('array_only')) { - /** - * Get a subset of the items from the given array. - * - * @param array $array - * @param array|string $keys - * @return array - */ - function array_only($array, $keys) - { - return array_intersect_key($array, array_flip((array)$keys)); - } -} - -if (!function_exists('array_pluck')) { - /** - * Pluck an array of values from an array. - * - * @param array $array - * @param string $value - * @param string $key - * @return array - */ - function array_pluck($array, $value, $key = null) - { - $results = array(); - - foreach ($array as $item) { - $itemValue = data_get($item, $value); - - // If the key is "null", we will just append the value to the array and keep - // looping. Otherwise we will key the array using the value of the key we - // received from the developer. Then we'll return the final array form. - if (is_null($key)) { - $results[] = $itemValue; - } else { - $itemKey = data_get($item, $key); - - $results[$itemKey] = $itemValue; - } - } - - return $results; - } -} - -if (!function_exists('array_pull')) { - /** - * Get a value from the array, and remove it. - * - * @param array $array - * @param string $key - * @param mixed $default - * @return mixed - */ - function array_pull(&$array, $key, $default = null) - { - $value = get($array, $key, $default); - - forget($array, $key); - - return $value; - } -} - -if (!function_exists('array_set')) { - /** - * Set an array item to a given value using "dot" notation. - * - * If no key is given to the method, the entire array will be replaced. - * - * @param array $array - * @param string $key - * @param mixed $value - * @return array - */ - function array_set(&$array, $key, $value) - { - if (is_null($key)) { - return $array = $value; - } - - $keys = explode('.', $key); - - while (count($keys) > 1) { - $key = array_shift($keys); - - // If the key doesn't exist at this depth, we will just create an empty array - // to hold the next value, allowing us to create the arrays to hold final - // values at the correct depth. Then we'll keep digging into the array. - if (!isset($array[$key]) || !is_array($array[$key])) { - $array[$key] = array(); - } - - $array =& $array[$key]; - } - - $array[array_shift($keys)] = $value; - - return $array; - } -} - -if (!function_exists('array_where')) { - /** - * Filter the array using the given Closure. - * - * @param array $array - * @param \Closure $callback - * @return array - */ - function array_where($array, Closure $callback) - { - $filtered = array(); - - foreach ($array as $key => $value) { - if (call_user_func($callback, $key, $value)) { - $filtered[$key] = $value; - } - } - - return $filtered; - } -} - -if (!function_exists('camel_case')) { - /** - * Convert a value to camel case. - * - * @param string $value - * @return string - */ - function camel_case($value) - { - static $camelCache = []; - - if (isset($camelCache[$value])) { - return $camelCache[$value]; - } - - return $camelCache[$value] = lcfirst(studly($value)); - } -} - -if (!function_exists('class_basename')) { - /** - * Get the class "basename" of the given object / class. - * - * @param string|object $class - * @return string - */ - function class_basename($class) - { - $class = is_object($class) ? get_class($class) : $class; - - return basename(str_replace('\\', '/', $class)); - } -} - -if (!function_exists('class_uses_recursive')) { - /** - * Returns all traits used by a class, it's subclasses and trait of their traits - * - * @param string $class - * @return array - */ - function class_uses_recursive($class) - { - $results = []; - - foreach (array_merge([$class => $class], class_parents($class)) as $class) { - $results += trait_uses_recursive($class); - } - - return array_unique($results); - } -} - -if (!function_exists('data_get')) { - /** - * Get an item from an array or object using "dot" notation. - * - * @param mixed $target - * @param string $key - * @param mixed $default - * @return mixed - */ - function data_get($target, $key, $default = null) - { - if (is_null($key)) { - return $target; - } - - foreach (explode('.', $key) as $segment) { - if (is_array($target)) { - if (!array_key_exists($segment, $target)) { - return value($default); - } - - $target = $target[$segment]; - } elseif ($target instanceof ArrayAccess) { - if (!isset($target[$segment])) { - return value($default); - } - - $target = $target[$segment]; - } elseif (is_object($target)) { - if (!isset($target->{$segment})) { - return value($default); - } - - $target = $target->{$segment}; - } else { - return value($default); - } - } - - return $target; - } -} - -if (!function_exists('e')) { - /** - * Escape HTML entities in a string. - * - * @param string $value - * @return string - */ - function e($value) - { - return htmlentities($value, ENT_QUOTES, 'UTF-8', false); - } -} - -if (!function_exists('ends_with')) { - /** - * Determine if a given string ends with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function ends_with($haystack, $needles) - { - foreach ((array)$needles as $needle) { - if ((string)$needle === substr($haystack, -strlen($needle))) { - return true; - } - } - - return false; - } -} - -if (!function_exists('head')) { - /** - * Get the first element of an array. Useful for method chaining. - * - * @param array $array - * @return mixed - */ - function head($array) - { - return reset($array); - } -} - -if (!function_exists('last')) { - /** - * Get the last element from an array. - * - * @param array $array - * @return mixed - */ - function last($array) - { - return end($array); - } -} - -if (!function_exists('object_get')) { - /** - * Get an item from an object using "dot" notation. - * - * @param object $object - * @param string $key - * @param mixed $default - * @return mixed - */ - function object_get($object, $key, $default = null) - { - if (is_null($key) || trim($key) == '') { - return $object; - } - - foreach (explode('.', $key) as $segment) { - if (!is_object($object) || !isset($object->{$segment})) { - return value($default); - } - - $object = $object->{$segment}; - } - - return $object; - } -} - -if (!function_exists('preg_replace_sub')) { - /** - * Replace a given pattern with each value in the array in sequentially. - * - * @param string $pattern - * @param array $replacements - * @param string $subject - * @return string - */ - function preg_replace_sub($pattern, &$replacements, $subject) - { - return preg_replace_callback($pattern, function () use (&$replacements) { - return array_shift($replacements); - - }, $subject); - } -} - -if (!function_exists('snake_case')) { - /** - * Convert a string to snake case. - * - * @param string $value - * @param string $delimiter - * @return string - */ - function snake_case($value, $delimiter = '_') - { - static $snakeCache = []; - $key = $value . $delimiter; - - if (isset($snakeCache[$key])) { - return $snakeCache[$key]; - } - - if (!ctype_lower($value)) { - $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1' . $delimiter, $value)); - } - - return $snakeCache[$key] = $value; - } -} - -if (!function_exists('starts_with')) { - /** - * Determine if a given string starts with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function starts_with($haystack, $needles) - { - foreach ((array)$needles as $needle) { - if ($needle != '' && strpos($haystack, $needle) === 0) { - return true; - } - } - - return false; - } -} - -if (!function_exists('str_contains')) { - /** - * Determine if a given string contains a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function str_contains($haystack, $needles) - { - foreach ((array)$needles as $needle) { - if ($needle != '' && strpos($haystack, $needle) !== false) { - return true; - } - } - - return false; - } -} - -if (!function_exists('str_finish')) { - /** - * Cap a string with a single instance of a given value. - * - * @param string $value - * @param string $cap - * @return string - */ - function str_finish($value, $cap) - { - $quoted = preg_quote($cap, '/'); - - return preg_replace('/(?:' . $quoted . ')+$/', '', $value) . $cap; - } -} - -if (!function_exists('str_is')) { - /** - * Determine if a given string matches a given pattern. - * - * @param string $pattern - * @param string $value - * @return bool - */ - function str_is($pattern, $value) - { - if ($pattern == $value) { - return true; - } - - $pattern = preg_quote($pattern, '#'); - - // Asterisks are translated into zero-or-more regular expression wildcards - // to make it convenient to check if the strings starts with the given - // pattern such as "library/*", making any string check convenient. - $pattern = str_replace('\*', '.*', $pattern) . '\z'; - - return (bool)preg_match('#^' . $pattern . '#', $value); - } -} - -if (!function_exists('str_limit')) { - /** - * Limit the number of characters in a string. - * - * @param string $value - * @param int $limit - * @param string $end - * @return string - */ - function str_limit($value, $limit = 100, $end = '...') - { - if (mb_strlen($value) <= $limit) { - return $value; - } - - return rtrim(mb_substr($value, 0, $limit, 'UTF-8')) . $end; - } -} - -if (!function_exists('str_random')) { - /** - * Generate a more truly "random" alpha-numeric string. - * - * @param int $length - * @return string - * - * @throws \RuntimeException - */ - function str_random($length = 16) - { - if (!function_exists('openssl_random_pseudo_bytes')) { - throw new RuntimeException('OpenSSL extension is required.'); - } - - $bytes = openssl_random_pseudo_bytes($length * 2); - - if ($bytes === false) { - throw new RuntimeException('Unable to generate random string.'); - } - - return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length); - } -} - -if (!function_exists('str_replace_array')) { - /** - * Replace a given value in the string sequentially with an array. - * - * @param string $search - * @param array $replace - * @param string $subject - * @return string - */ - function str_replace_array($search, array $replace, $subject) - { - foreach ($replace as $value) { - $subject = preg_replace('/' . $search . '/', $value, $subject, 1); - } - - return $subject; - } -} - -if (!function_exists('str_slug')) { - /** - * Generate a URL friendly "slug" from a given string. - * - * @param string $title - * @param string $separator - * @return string - */ - function str_slug($title, $separator = '-') - { - $title = ascii($title); - - // Convert all dashes/underscores into separator - $flip = $separator == '-' ? '_' : '-'; - - $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title); - - // Remove all characters that are not the separator, letters, numbers, or whitespace. - $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', mb_strtolower($title)); - - // Replace all separator characters and whitespace by a single separator - $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title); - - return trim($title, $separator); - } -} - -if (!function_exists('ascii')) { - /** - * Transliterate a UTF-8 value to ASCII. - * - * @param string $value - * @return string - */ - function ascii($value) - { - foreach (charsArray() as $key => $val) { - $value = str_replace($val, $key, $value); - } - - return preg_replace('/[^\x20-\x7E]/u', '', $value); - } -} - -if (!function_exists('charsArray')) { - /** - * Returns the replacements for the ascii method. - * - * Note: Adapted from Stringy\Stringy. - * - * @see https://github.com/danielstjules/Stringy/blob/2.3.1/LICENSE.txt - * - * @return array - */ - function charsArray() - { - static $charsArray; - - if (isset($charsArray)) { - return $charsArray; - } - - return $charsArray = [ - '0' => ['°', '₀', '۰'], - '1' => ['¹', '₁', '۱'], - '2' => ['²', '₂', '۲'], - '3' => ['³', '₃', '۳'], - '4' => ['⁴', '₄', '۴', '٤'], - '5' => ['⁵', '₅', '۵', '٥'], - '6' => ['⁶', '₆', '۶', '٦'], - '7' => ['⁷', '₇', '۷'], - '8' => ['⁸', '₈', '۸'], - '9' => ['⁹', '₉', '۹'], - 'a' => [ - 'à', - 'á', - 'ả', - 'ã', - 'ạ', - 'ă', - 'ắ', - 'ằ', - 'ẳ', - 'ẵ', - 'ặ', - 'â', - 'ấ', - 'ầ', - 'ẩ', - 'ẫ', - 'ậ', - 'ā', - 'ą', - 'å', - 'α', - 'ά', - 'ἀ', - 'ἁ', - 'ἂ', - 'ἃ', - 'ἄ', - 'ἅ', - 'ἆ', - 'ἇ', - 'ᾀ', - 'ᾁ', - 'ᾂ', - 'ᾃ', - 'ᾄ', - 'ᾅ', - 'ᾆ', - 'ᾇ', - 'ὰ', - 'ά', - 'ᾰ', - 'ᾱ', - 'ᾲ', - 'ᾳ', - 'ᾴ', - 'ᾶ', - 'ᾷ', - 'а', - 'أ', - 'အ', - 'ာ', - 'ါ', - 'ǻ', - 'ǎ', - 'ª', - 'ა', - 'अ', - 'ا' - ], - 'b' => ['б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ'], - 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ'], - 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ'], - 'e' => [ - 'é', - 'è', - 'ẻ', - 'ẽ', - 'ẹ', - 'ê', - 'ế', - 'ề', - 'ể', - 'ễ', - 'ệ', - 'ë', - 'ē', - 'ę', - 'ě', - 'ĕ', - 'ė', - 'ε', - 'έ', - 'ἐ', - 'ἑ', - 'ἒ', - 'ἓ', - 'ἔ', - 'ἕ', - 'ὲ', - 'έ', - 'е', - 'ё', - 'э', - 'є', - 'ə', - 'ဧ', - 'ေ', - 'ဲ', - 'ე', - 'ए', - 'إ', - 'ئ' - ], - 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ'], - 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ'], - 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ'], - 'i' => [ - 'í', - 'ì', - 'ỉ', - 'ĩ', - 'ị', - 'î', - 'ï', - 'ī', - 'ĭ', - 'į', - 'ı', - 'ι', - 'ί', - 'ϊ', - 'ΐ', - 'ἰ', - 'ἱ', - 'ἲ', - 'ἳ', - 'ἴ', - 'ἵ', - 'ἶ', - 'ἷ', - 'ὶ', - 'ί', - 'ῐ', - 'ῑ', - 'ῒ', - 'ΐ', - 'ῖ', - 'ῗ', - 'і', - 'ї', - 'и', - 'ဣ', - 'ိ', - 'ီ', - 'ည်', - 'ǐ', - 'ი', - 'इ' - ], - 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج'], - 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک'], - 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ'], - 'm' => ['м', 'μ', 'م', 'မ', 'მ'], - 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ'], - 'o' => [ - 'ó', - 'ò', - 'ỏ', - 'õ', - 'ọ', - 'ô', - 'ố', - 'ồ', - 'ổ', - 'ỗ', - 'ộ', - 'ơ', - 'ớ', - 'ờ', - 'ở', - 'ỡ', - 'ợ', - 'ø', - 'ō', - 'ő', - 'ŏ', - 'ο', - 'ὀ', - 'ὁ', - 'ὂ', - 'ὃ', - 'ὄ', - 'ὅ', - 'ὸ', - 'ό', - 'о', - 'و', - 'θ', - 'ို', - 'ǒ', - 'ǿ', - 'º', - 'ო', - 'ओ' - ], - 'p' => ['п', 'π', 'ပ', 'პ', 'پ'], - 'q' => ['ყ'], - 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ'], - 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს'], - 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ'], - 'u' => [ - 'ú', - 'ù', - 'ủ', - 'ũ', - 'ụ', - 'ư', - 'ứ', - 'ừ', - 'ử', - 'ữ', - 'ự', - 'û', - 'ū', - 'ů', - 'ű', - 'ŭ', - 'ų', - 'µ', - 'у', - 'ဉ', - 'ု', - 'ူ', - 'ǔ', - 'ǖ', - 'ǘ', - 'ǚ', - 'ǜ', - 'უ', - 'उ' - ], - 'v' => ['в', 'ვ', 'ϐ'], - 'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ'], - 'x' => ['χ', 'ξ'], - 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ'], - 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ'], - 'aa' => ['ع', 'आ', 'آ'], - 'ae' => ['ä', 'æ', 'ǽ'], - 'ai' => ['ऐ'], - 'at' => ['@'], - 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'], - 'dj' => ['ђ', 'đ'], - 'dz' => ['џ', 'ძ'], - 'ei' => ['ऍ'], - 'gh' => ['غ', 'ღ'], - 'ii' => ['ई'], - 'ij' => ['ij'], - 'kh' => ['х', 'خ', 'ხ'], - 'lj' => ['љ'], - 'nj' => ['њ'], - 'oe' => ['ö', 'œ', 'ؤ'], - 'oi' => ['ऑ'], - 'oii' => ['ऒ'], - 'ps' => ['ψ'], - 'sh' => ['ш', 'შ', 'ش'], - 'shch' => ['щ'], - 'ss' => ['ß'], - 'sx' => ['ŝ'], - 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'], - 'ts' => ['ц', 'ც', 'წ'], - 'ue' => ['ü'], - 'uu' => ['ऊ'], - 'ya' => ['я'], - 'yu' => ['ю'], - 'zh' => ['ж', 'ჟ', 'ژ'], - '(c)' => ['©'], - 'A' => [ - 'Á', - 'À', - 'Ả', - 'Ã', - 'Ạ', - 'Ă', - 'Ắ', - 'Ằ', - 'Ẳ', - 'Ẵ', - 'Ặ', - 'Â', - 'Ấ', - 'Ầ', - 'Ẩ', - 'Ẫ', - 'Ậ', - 'Å', - 'Ā', - 'Ą', - 'Α', - 'Ά', - 'Ἀ', - 'Ἁ', - 'Ἂ', - 'Ἃ', - 'Ἄ', - 'Ἅ', - 'Ἆ', - 'Ἇ', - 'ᾈ', - 'ᾉ', - 'ᾊ', - 'ᾋ', - 'ᾌ', - 'ᾍ', - 'ᾎ', - 'ᾏ', - 'Ᾰ', - 'Ᾱ', - 'Ὰ', - 'Ά', - 'ᾼ', - 'А', - 'Ǻ', - 'Ǎ' - ], - 'B' => ['Б', 'Β', 'ब'], - 'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ'], - 'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'], - 'E' => [ - 'É', - 'È', - 'Ẻ', - 'Ẽ', - 'Ẹ', - 'Ê', - 'Ế', - 'Ề', - 'Ể', - 'Ễ', - 'Ệ', - 'Ë', - 'Ē', - 'Ę', - 'Ě', - 'Ĕ', - 'Ė', - 'Ε', - 'Έ', - 'Ἐ', - 'Ἑ', - 'Ἒ', - 'Ἓ', - 'Ἔ', - 'Ἕ', - 'Έ', - 'Ὲ', - 'Е', - 'Ё', - 'Э', - 'Є', - 'Ə' - ], - 'F' => ['Ф', 'Φ'], - 'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ'], - 'H' => ['Η', 'Ή', 'Ħ'], - 'I' => [ - 'Í', - 'Ì', - 'Ỉ', - 'Ĩ', - 'Ị', - 'Î', - 'Ï', - 'Ī', - 'Ĭ', - 'Į', - 'İ', - 'Ι', - 'Ί', - 'Ϊ', - 'Ἰ', - 'Ἱ', - 'Ἳ', - 'Ἴ', - 'Ἵ', - 'Ἶ', - 'Ἷ', - 'Ῐ', - 'Ῑ', - 'Ὶ', - 'Ί', - 'И', - 'І', - 'Ї', - 'Ǐ', - 'ϒ' - ], - 'K' => ['К', 'Κ'], - 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल'], - 'M' => ['М', 'Μ'], - 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν'], - 'O' => [ - 'Ó', - 'Ò', - 'Ỏ', - 'Õ', - 'Ọ', - 'Ô', - 'Ố', - 'Ồ', - 'Ổ', - 'Ỗ', - 'Ộ', - 'Ơ', - 'Ớ', - 'Ờ', - 'Ở', - 'Ỡ', - 'Ợ', - 'Ø', - 'Ō', - 'Ő', - 'Ŏ', - 'Ο', - 'Ό', - 'Ὀ', - 'Ὁ', - 'Ὂ', - 'Ὃ', - 'Ὄ', - 'Ὅ', - 'Ὸ', - 'Ό', - 'О', - 'Θ', - 'Ө', - 'Ǒ', - 'Ǿ' - ], - 'P' => ['П', 'Π'], - 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ'], - 'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ'], - 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ'], - 'U' => [ - 'Ú', - 'Ù', - 'Ủ', - 'Ũ', - 'Ụ', - 'Ư', - 'Ứ', - 'Ừ', - 'Ử', - 'Ữ', - 'Ự', - 'Û', - 'Ū', - 'Ů', - 'Ű', - 'Ŭ', - 'Ų', - 'У', - 'Ǔ', - 'Ǖ', - 'Ǘ', - 'Ǚ', - 'Ǜ' - ], - 'V' => ['В'], - 'W' => ['Ω', 'Ώ', 'Ŵ'], - 'X' => ['Χ', 'Ξ'], - 'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ'], - 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ'], - 'AE' => ['Ä', 'Æ', 'Ǽ'], - 'CH' => ['Ч'], - 'DJ' => ['Ђ'], - 'DZ' => ['Џ'], - 'GX' => ['Ĝ'], - 'HX' => ['Ĥ'], - 'IJ' => ['IJ'], - 'JX' => ['Ĵ'], - 'KH' => ['Х'], - 'LJ' => ['Љ'], - 'NJ' => ['Њ'], - 'OE' => ['Ö', 'Œ'], - 'PS' => ['Ψ'], - 'SH' => ['Ш'], - 'SHCH' => ['Щ'], - 'SS' => ['ẞ'], - 'TH' => ['Þ'], - 'TS' => ['Ц'], - 'UE' => ['Ü'], - 'YA' => ['Я'], - 'YU' => ['Ю'], - 'ZH' => ['Ж'], - ' ' => [ - "\xC2\xA0", - "\xE2\x80\x80", - "\xE2\x80\x81", - "\xE2\x80\x82", - "\xE2\x80\x83", - "\xE2\x80\x84", - "\xE2\x80\x85", - "\xE2\x80\x86", - "\xE2\x80\x87", - "\xE2\x80\x88", - "\xE2\x80\x89", - "\xE2\x80\x8A", - "\xE2\x80\xAF", - "\xE2\x81\x9F", - "\xE3\x80\x80" - ], - ]; - } -} - -if (!function_exists('studly_case')) { - /** - * Convert a value to studly caps case. - * - * @param string $value - * @return string - */ - function studly_case($value) - { - static $studlyCache = []; - $key = $value; - - if (isset($studlyCache[$key])) { - return $studlyCache[$key]; - } - - $value = ucwords(str_replace(array('-', '_'), ' ', $value)); - - return $studlyCache[$key] = str_replace(' ', '', $value); - } -} - -if (!function_exists('title_case')) { - /** - * Convert a value to title case. - * - * @param string $value - * @return string - */ - function title_case($value) - { - return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); - } -} - -if (!function_exists('trait_uses_recursive')) { +if (!function_exists('dd')) { /** - * Returns all traits used by a trait and its traits + * Dump the passed variables and end the script. * - * @param string $trait - * @return array + * @param mixed + * @return void */ - function trait_uses_recursive($trait) + function dd() { - $traits = class_uses($trait); - - foreach ($traits as $trait) { - $traits += trait_uses_recursive($trait); - } - - return $traits; - } -} + array_map(function ($x) { + var_dump($x); + }, func_get_args()); -if (!function_exists('value')) { - /** - * Return the default value of the given value. - * - * @param mixed $value - * @return mixed - */ - function value($value) - { - return $value instanceof Closure ? $value() : $value; + die(1); } } -if (!function_exists('with')) { +if (!function_exists('e')) { /** - * Return the given object. Useful for chaining. + * Escape HTML entities in a string. * - * @param mixed $object - * @return mixed - */ - function with($object) - { - return $object; - } -} - -/** - * Helper functions for the helper functions, that can still be used standalone - */ -if (!function_exists('studly')) { - /** - * Convert a value to studly caps case. + * @param string $value * - * @param string $value * @return string */ - function studly($value) + function e(string $value): string { - static $studlyCache = []; - $key = $value; - - if (isset($studlyCache[$key])) { - return $studlyCache[$key]; - } - - $value = ucwords(str_replace(array('-', '_'), ' ', $value)); - - return $studlyCache[$key] = str_replace(' ', '', $value); + return htmlentities($value, ENT_QUOTES, 'UTF-8', false); } } -if (!function_exists('get')) { +if (!function_exists('object_get')) { /** - * Get an item from an array using "dot" notation. + * Get an item from an object using "dot" notation. * - * @param array $array - * @param string $key + * @param object $object + * @param string $key * @param mixed $default + * * @return mixed */ - function get($array, $key, $default = null) + function object_get(object $object, string $key, $default = null) { - if (is_null($key)) { - return $array; - } - - if (isset($array[$key])) { - return $array[$key]; + if (trim($key) == '') { + return $object; } foreach (explode('.', $key) as $segment) { - if (!is_array($array) || !array_key_exists($segment, $array)) { + if (!is_object($object) || !isset($object->{$segment})) { return value($default); } - $array = $array[$segment]; - } - - return $array; - } -} - -if (!function_exists('set')) { - /** - * Set an array item to a given value using "dot" notation. - * - * If no key is given to the method, the entire array will be replaced. - * - * @param array $array - * @param string $key - * @param mixed $value - * @return array - */ - function set(&$array, $key, $value) - { - if (is_null($key)) { - return $array = $value; - } - - $keys = explode('.', $key); - - while (count($keys) > 1) { - $key = array_shift($keys); - - // If the key doesn't exist at this depth, we will just create an empty array - // to hold the next value, allowing us to create the arrays to hold final - // values at the correct depth. Then we'll keep digging into the array. - if (!isset($array[$key]) || !is_array($array[$key])) { - $array[$key] = array(); - } - - $array =& $array[$key]; - } - - $array[array_shift($keys)] = $value; - - return $array; - } -} - -if (!function_exists('dot')) { - /** - * Flatten a multi-dimensional associative array with dots. - * - * @param array $array - * @param string $prepend - * @return array - */ - function dot($array, $prepend = '') - { - $results = array(); - - foreach ($array as $key => $value) { - if (is_array($value)) { - $results = array_merge($results, dot($value, $prepend . $key . '.')); - } else { - $results[$prepend . $key] = $value; - } - } - - return $results; - } -} - -if (!function_exists('first')) { - /** - * Return the first element in an array passing a given truth test. - * - * @param array $array - * @param \Closure $callback - * @param mixed $default - * @return mixed - */ - function first($array, $callback, $default = null) - { - foreach ($array as $key => $value) { - if (call_user_func($callback, $key, $value)) { - return $value; - } - } - - return value($default); - } -} - -if (!function_exists('forget')) { - /** - * Remove one or many array items from a given array using "dot" notation. - * - * @param array $array - * @param array|string $keys - * @return void - */ - function forget(&$array, $keys) - { - $original =& $array; - - foreach ((array)$keys as $key) { - $parts = explode('.', $key); - - while (count($parts) > 1) { - $part = array_shift($parts); - - if (isset($array[$part]) && is_array($array[$part])) { - $array =& $array[$part]; - } - } - - unset($array[array_shift($parts)]); - - // clean up after each pass - $array =& $original; - } - } -} - -if (!function_exists('bcrypt')) { - /** - * Password hash the given value. - * - * @param string $value - * @param array $options - * @return string - * - * @throws \RuntimeException - */ - function bcrypt($value, $options = []) - { - $cost = isset($options['rounds']) ? $options['rounds'] : 10; - - $hashedValue = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]); - - if ($hashedValue === false) { - throw new RuntimeException('Bcrypt hashing not supported.'); + $object = $object->{$segment}; } - return $hashedValue; + return $object; } } @@ -1646,10 +70,11 @@ function bcrypt($value, $options = []) * Call the given Closure with the given value then return the value. * * @param mixed $value - * @param callable $callback + * @param callable $callback + * * @return mixed */ - function tap($value, $callback) + function tap($value, callable $callback) { $callback($value); @@ -1657,119 +82,28 @@ function tap($value, $callback) } } -if (!function_exists('dd')) { - /** - * Dump the passed variables and end the script. - * - * @param mixed - * @return void - */ - function dd() - { - array_map(function ($x) { - var_dump($x); - }, func_get_args()); - - die(1); - } - -} - -if (!function_exists('data_fill')) { +if (!function_exists('value')) { /** - * Fill in data where it's missing. + * Return the default value of the given value. * - * @param mixed $target - * @param string|array $key * @param mixed $value * @return mixed */ - function data_fill(&$target, $key, $value) + function value($value) { - return data_set($target, $key, $value, false); + return $value instanceof Closure ? $value() : $value; } } -if (! function_exists('data_set')) { +if (!function_exists('with')) { /** - * Set an item on an array or object using dot notation. + * Return the given object. Useful for chaining. * - * @param mixed $target - * @param string|array $key - * @param mixed $value - * @param bool $overwrite + * @param mixed $object * @return mixed */ - function data_set(&$target, $key, $value, $overwrite = true) - { - $segments = is_array($key) ? $key : explode('.', $key); - if (($segment = array_shift($segments)) === '*') { - if (! accessible($target)) { - $target = []; - } - if ($segments) { - foreach ($target as &$inner) { - data_set($inner, $segments, $value, $overwrite); - } - } elseif ($overwrite) { - foreach ($target as &$inner) { - $inner = $value; - } - } - } elseif (accessible($target)) { - if ($segments) { - if (! exists($target, $segment)) { - $target[$segment] = []; - } - data_set($target[$segment], $segments, $value, $overwrite); - } elseif ($overwrite || ! exists($target, $segment)) { - $target[$segment] = $value; - } - } elseif (is_object($target)) { - if ($segments) { - if (! isset($target->{$segment})) { - $target->{$segment} = []; - } - data_set($target->{$segment}, $segments, $value, $overwrite); - } elseif ($overwrite || ! isset($target->{$segment})) { - $target->{$segment} = $value; - } - } else { - $target = []; - if ($segments) { - data_set($target[$segment], $segments, $value, $overwrite); - } elseif ($overwrite) { - $target[$segment] = $value; - } - } - return $target; - } - - /** - * Determine whether the given value is array accessible. - * - * @param mixed $value - * @return bool - */ - function accessible($value) - { - return is_array($value) || $value instanceof ArrayAccess; - } - - - /** - * Determine if the given key exists in the provided array. - * - * @param \ArrayAccess|array $array - * @param string|int $key - * @return bool - */ - function exists($array, $key) + function with($object) { - if ($array instanceof ArrayAccess) { - return $array->offsetExists($key); - } - return array_key_exists($key, $array); + return $object; } } - diff --git a/src/strings.php b/src/strings.php new file mode 100644 index 0000000..c14e73a --- /dev/null +++ b/src/strings.php @@ -0,0 +1,670 @@ + $value) { + return array_shift($replacements); + } + }, $subject); + } +} + +if (! function_exists('str_after')) { + /** + * Return the remainder of a string after the first occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ + function str_after(string $subject, string $search): string + { + return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; + } +} + +if (! function_exists('str_after_last')) { + /** + * Return the remainder of a string after the last occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ + function str_after_last(string $subject, string $search): string + { + if ($search === '') { + return $subject; + } + + $position = strrpos($subject, (string) $search); + + if ($position === false) { + return $subject; + } + + return substr($subject, $position + strlen($search)); + } +} + +if (! function_exists('str_before')) { + /** + * Get the portion of a string before the first occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ + function str_before(string $subject, string $search): string + { + if ($search === '') { + return $subject; + } + + $result = strstr($subject, (string) $search, true); + + return $result === false ? $subject : $result; + } +} + +if (! function_exists('str_before_last')) { + /** + * Get the portion of a string before the last occurrence of a given value. + * + * @param string $subject + * @param string $search + * + * @return string + */ + function str_before_last(string $subject, string $search): string + { + if ($search === '') { + return $subject; + } + + $pos = mb_strrpos($subject, $search); + + if ($pos === false) { + return $subject; + } + + return str_substr($subject, 0, $pos); + } +} + +if (! function_exists('str_between')) { + /** + * Get the portion of a string between two given values. + * + * @param string $subject + * @param string $from + * @param string $to + * + * @return string + */ + function str_between(string $subject, string $from, string $to): string + { + if ($from === '' || $to === '') { + return $subject; + } + + return str_before_last(str_after($subject, $from), $to); + } +} + +if (! function_exists('str_contains')) { + /** + * Determine if a given string contains a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ + function str_contains(string $haystack, $needles): bool + { + foreach ((array) $needles as $needle) { + if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { + return true; + } + } + + return false; + } +} + +if (! function_exists('str_contains_all')) { + /** + * Determine if a given string contains all array values. + * + * @param string $haystack + * @param string[] $needles + * + * @return bool + */ + function str_contains_all(string $haystack, array $needles): bool + { + foreach ($needles as $needle) { + if (!str_contains($haystack, $needle)) { + return false; + } + } + + return true; + } +} + +if (! function_exists('str_ends_with')) { + /** + * Determine if a given string ends with a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ + function str_ends_with(string $haystack, $needles): bool + { + foreach ((array) $needles as $needle) { + if ( + $needle !== '' && $needle !== null + && substr($haystack, -strlen($needle)) === (string) $needle + ) { + return true; + } + } + + return false; + } +} + +if (! function_exists('str_finish')) { + /** + * Cap a string with a single instance of a given value. + * + * @param string $value + * @param string $cap + * + * @return string + */ + function str_finish(string $value, string $cap): string + { + $quoted = preg_quote($cap, '/'); + + return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap; + } +} + +if (! function_exists('str_is')) { + /** + * Determine if a given string matches a given pattern. + * + * @param string|array $pattern + * @param string $value + * + * @return bool + */ + function str_is($pattern, string $value): bool + { + $patterns = array_wrap($pattern); + + if (empty($patterns)) { + return false; + } + + foreach ($patterns as $pattern) { + // If the given value is an exact match we can of course return true right + // from the beginning. Otherwise, we will translate asterisks and do an + // actual pattern match against the two strings to see if they match. + if ($pattern == $value) { + return true; + } + + $pattern = preg_quote($pattern, '#'); + + // Asterisks are translated into zero-or-more regular expression wildcards + // to make it convenient to check if the strings starts with the given + // pattern such as "library/*", making any string check convenient. + $pattern = str_replace('\*', '.*', $pattern); + + if (preg_match('#^'.$pattern.'\z#u', $value) === 1) { + return true; + } + } + + return false; + } +} + +if (! function_exists('str_is_uuid')) { + /** + * Determine if a given string is a valid UUID. + * + * @param string $value + * + * @return bool + */ + function str_is_uuid(string $value): bool + { + if (!is_string($value)) { + return false; + } + + return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0; + } +} + +if (! function_exists('str_kebab')) { + /** + * Convert a string to kebab case. + * + * @param string $value + * + * @return string + */ + function str_kebab(string $value): string + { + return str_snake($value, '-'); + } +} + +if (! function_exists('str_length')) { + /** + * Return the length of the given string. + * + * @param string $value + * @param string|null $encoding + * + * @return int + */ + function str_length(string $value, string $encoding = null): int + { + if ($encoding) { + return mb_strlen($value, $encoding); + } + + return mb_strlen($value); + } +} + +if (! function_exists('str_limit')) { + /** + * Limit the number of characters in a string. + * + * @param string $value + * @param int $limit + * @param string $end + * + * @return string + */ + function str_limit(string $value, int $limit = 100, string $end = '...'): string + { + if (mb_strwidth($value, 'UTF-8') <= $limit) { + return $value; + } + + return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; + } +} + +if (! function_exists('str_lower')) { + /** + * Convert the given string to lower-case. + * + * @param string $value + * + * @return string + */ + function str_lower(string $value): string + { + return mb_strtolower($value, 'UTF-8'); + } +} + +if (! function_exists('str_words')) { + /** + * Limit the number of words in a string. + * + * @param string $value + * @param int $words + * @param string $end + * + * @return string + */ + function str_words(string $value, int $words = 100, string $end = '...'): string + { + preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches); + + if (!isset($matches[0]) || str_length($value) === str_length($matches[0])) { + return $value; + } + + return rtrim($matches[0]).$end; + } +} + +if (! function_exists('str_match')) { + /** + * Get the string matching the given pattern. + * + * @param string $pattern + * @param string $subject + * + * @return string + */ + function str_match(string $pattern, string $subject): string + { + preg_match($pattern, $subject, $matches); + + if (!$matches) { + return ''; + } + + return $matches[1] ?? $matches[0]; + } +} + +if (! function_exists('str_pad_both')) { + /** + * Pad both sides of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ + function str_pad_both(string $value, int $length, string $pad = ' '): string + { + return str_pad($value, $length, $pad, STR_PAD_BOTH); + } +} + +if (! function_exists('str_pad_left')) { + /** + * Pad the left side of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ + function str_pad_left(string $value, int $length, string $pad = ' '): string + { + return str_pad($value, $length, $pad, STR_PAD_LEFT); + } +} + +if (! function_exists('str_pad_right')) { + /** + * Pad the right side of a string with another. + * + * @param string $value + * @param int $length + * @param string $pad + * + * @return string + */ + function str_pad_right(string $value, int $length, string $pad = ' '): string + { + return str_pad($value, $length, $pad, STR_PAD_RIGHT); + } +} + +if (! function_exists('str_random')) { + /** + * Generate a more truly "random" alpha-numeric string. + * + * @param int $length + * + * @return string + */ + function str_random(int $length = 16): string + { + $string = ''; + + while (($len = strlen($string)) < $length) { + $size = $length - $len; + + $bytes = random_bytes($size); + + $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); + } + + return $string; + } +} + +if (! function_exists('str_replace_array')) { + /** + * Replace a given value in the string sequentially with an array. + * + * @param string $search + * @param array $replace + * @param string $subject + * + * @return string + */ + function str_replace_array(string $search, array $replace, string $subject): string + { + $segments = explode($search, $subject); + + $result = array_shift($segments); + + foreach ($segments as $segment) { + $result .= (array_shift($replace) ?? $search).$segment; + } + + return $result; + } +} + +if (! function_exists('str_replace_first')) { + /** + * Replace the first occurrence of a given value in the string. + * + * @param string $search + * @param string $replace + * @param string $subject + * + * @return string + */ + function str_replace_first(string $search, string $replace, string $subject): string + { + if ($search === '') { + return $subject; + } + + $position = strpos($subject, $search); + + if ($position !== false) { + return substr_replace($subject, $replace, $position, strlen($search)); + } + + return $subject; + } +} + +if (! function_exists('str_replace_last')) { + /** + * Replace the last occurrence of a given value in the string. + * + * @param string $search + * @param string $replace + * @param string $subject + * + * @return string + */ + function str_replace_last(string $search, string $replace, string $subject): string + { + if ($search === '') { + return $subject; + } + + $position = strrpos($subject, $search); + + if ($position !== false) { + return substr_replace($subject, $replace, $position, strlen($search)); + } + + return $subject; + } +} + +if (! function_exists('str_remove')) { + /** + * Remove any occurrence of the given string in the subject. + * + * @param string|array $search + * @param string $subject + * @param bool $caseSensitive + * + * @return string + */ + function str_remove($search, string $subject, bool $caseSensitive = true): string + { + $subject = $caseSensitive + ? str_replace($search, '', $subject) + : str_ireplace($search, '', $subject); + + return $subject; + } +} + +if (! function_exists('str_start')) { + /** + * Begin a string with a single instance of a given value. + * + * @param string $value + * @param string $prefix + * + * @return string + */ + function str_start(string $value, string $prefix): string + { + $quoted = preg_quote($prefix, '/'); + + return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value); + } +} + +if (! function_exists('str_upper')) { + /** + * Convert the given string to upper-case. + * + * @param string $value + * + * @return string + */ + function str_upper(string $value): string + { + return mb_strtoupper($value, 'UTF-8'); + } +} + +if (! function_exists('str_title')) { + /** + * Convert the given string to title case. + * + * @param string $value + * + * @return string + */ + function str_title(string $value): string + { + return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); + } +} + +if (! function_exists('str_snake')) { + /** + * Convert a string to snake case. + * + * @param string $value + * @param string $delimiter + * + * @return string + */ + function str_snake(string $value, string $delimiter = '_'): string + { + static $snakeCache = []; + $key = $value; + + if (isset($snakeCache[$key][$delimiter])) { + return $snakeCache[$key][$delimiter]; + } + + if (!ctype_lower($value)) { + $value = preg_replace('/\s+/u', '', ucwords($value)); + + $value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); + } + + return $snakeCache[$key][$delimiter] = $value; + } +} + +if (! function_exists('str_starts_with')) { + /** + * Determine if a given string starts with a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * + * @return bool + */ + function str_starts_with(string $haystack, $needles): bool + { + foreach ((array) $needles as $needle) { + if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { + return true; + } + } + + return false; + } +} + +if (! function_exists('str_studly')) { + /** + * Convert a value to studly caps case. + * + * @param string $value + * + * @return string + */ + function str_studly(string $value): string + { + static $studlyCache = []; + $key = $value; + + if (isset($studlyCache[$key])) { + return $studlyCache[$key]; + } + + $value = ucwords(str_replace(['-', '_'], ' ', $value)); + + return $studlyCache[$key] = str_replace(' ', '', $value); + } +} From 3fb3f34b72967c244fd60e9048f561268ee8ab97 Mon Sep 17 00:00:00 2001 From: Anthony Rappa Date: Fri, 1 Oct 2021 13:58:13 -0400 Subject: [PATCH 2/2] Add default --- src/arrays.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arrays.php b/src/arrays.php index cab6735..168c468 100755 --- a/src/arrays.php +++ b/src/arrays.php @@ -667,7 +667,7 @@ function array_shuffle(array $array, int $seed = null): array * * @return array */ - function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending): array + function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending = true): array { foreach ($array as &$value) { if (is_array($value)) {