| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651 |
- <?php
- /**
- * 辅助函数.
- *
- * @author nj
- * @date 2021-05-14 11:05:31
- */
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Support\Collection;
- 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)
- {
- return Arr::add($array, $key, $value);
- }
- }
- if (! function_exists('array_build')) {
- /**
- * Build a new array using a callback.
- *
- * @param array $array
- * @param callable $callback
- * @return array
- */
- function array_build($array, callable $callback)
- {
- return Arr::build($array, $callback);
- }
- }
- if (! function_exists('array_collapse')) {
- /**
- * Collapse an array of arrays into a single array.
- *
- * @param \ArrayAccess|array $array
- * @return array
- */
- function array_collapse($array)
- {
- return Arr::collapse($array);
- }
- }
- 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 Arr::divide($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 = '')
- {
- return Arr::dot($array, $prepend);
- }
- }
- 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 Arr::except($array, $keys);
- }
- }
- if (! function_exists('array_fetch')) {
- /**
- * Fetch a flattened array of a nested array element.
- *
- * @param array $array
- * @param string $key
- * @return array
- *
- * @deprecated since version 5.1. Use array_pluck instead.
- */
- function array_fetch($array, $key)
- {
- return Arr::fetch($array, $key);
- }
- }
- if (! function_exists('array_first')) {
- /**
- * Return the first element in an array passing a given truth test.
- *
- * @param array $array
- * @param callable $callback
- * @param mixed $default
- * @return mixed
- */
- function array_first($array, callable $callback, $default = null)
- {
- return Arr::first($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 Arr::flatten($array);
- }
- }
- 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)
- {
- return Arr::forget($array, $keys);
- }
- }
- 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)
- {
- return Arr::get($array, $key, $default);
- }
- }
- 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)
- {
- return Arr::has($array, $key);
- }
- }
- if (! function_exists('array_last')) {
- /**
- * Return the last element in an array passing a given truth test.
- *
- * @param array $array
- * @param callable $callback
- * @param mixed $default
- * @return mixed
- */
- function array_last($array, $callback, $default = null)
- {
- return Arr::last($array, $callback, $default);
- }
- }
- 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 Arr::only($array, $keys);
- }
- }
- if (! function_exists('array_pluck')) {
- /**
- * Pluck an array of values from an array.
- *
- * @param array $array
- * @param string|array $value
- * @param string|array|null $key
- * @return array
- */
- function array_pluck($array, $value, $key = null)
- {
- return Arr::pluck($array, $value, $key);
- }
- }
- 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, $value, $key = null)
- {
- return Arr::prepend($array, $value, $key);
- }
- }
- 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)
- {
- return Arr::pull($array, $key, $default);
- }
- }
- 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)
- {
- return Arr::set($array, $key, $value);
- }
- }
- if (! function_exists('array_sort')) {
- /**
- * Sort the array using the given callback.
- *
- * @param array $array
- * @param callable $callback
- * @return array
- */
- function array_sort($array, callable $callback)
- {
- return Arr::sort($array, $callback);
- }
- }
- if (! function_exists('array_sort_recursive')) {
- /**
- * Recursively sort an array by keys and values.
- *
- * @param array $array
- * @return array
- */
- function array_sort_recursive($array)
- {
- return Arr::sortRecursive($array);
- }
- }
- if (! function_exists('array_where')) {
- /**
- * Filter the array using the given callback.
- *
- * @param array $array
- * @param callable $callback
- * @return array
- */
- function array_where($array, callable $callback)
- {
- return Arr::where($array, $callback);
- }
- }
- if (! function_exists('camel_case')) {
- /**
- * Convert a value to camel case.
- *
- * @param string $value
- * @return string
- */
- function camel_case($value)
- {
- return Str::camel($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, its 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('collect')) {
- /**
- * Create a collection from the given value.
- *
- * @param mixed $value
- * @return \Illuminate\Support\Collection
- */
- function collect($value = null)
- {
- return new Collection($value);
- }
- }
- if (! function_exists('data_get')) {
- /**
- * Get an item from an array or object using "dot" notation.
- *
- * @param mixed $target
- * @param string|array $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 $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('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)
- {
- return Str::endsWith($haystack, $needles);
- }
- }
- 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 ($match) use (&$replacements) {
- foreach ($replacements as $key => $value) {
- 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 = '_')
- {
- return Str::snake($value, $delimiter);
- }
- }
- 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)
- {
- return Str::startsWith($haystack, $needles);
- }
- }
- 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)
- {
- return Str::contains($haystack, $needles);
- }
- }
- 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)
- {
- return Str::finish($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)
- {
- return Str::is($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 = '...')
- {
- return Str::limit($value, $limit, $end);
- }
- }
- if (! function_exists('str_plural')) {
- /**
- * Get the plural form of an English word.
- *
- * @param string $value
- * @param int $count
- * @return string
- */
- function str_plural($value, $count = 2)
- {
- return Str::plural($value, $count);
- }
- }
- 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)
- {
- return Str::random($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_singular')) {
- /**
- * Get the singular form of an English word.
- *
- * @param string $value
- * @return string
- */
- function str_singular($value)
- {
- return Str::singular($value);
- }
- }
- 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 = '-')
- {
- return Str::slug($title, $separator);
- }
- }
- if (! function_exists('studly_case')) {
- /**
- * Convert a value to studly caps case.
- *
- * @param string $value
- * @return string
- */
- function studly_case($value)
- {
- return Str::studly($value);
- }
- }
- if (! function_exists('title_case')) {
- /**
- * Convert a value to title case.
- *
- * @param string $value
- * @return string
- */
- function title_case($value)
- {
- return Str::title($value);
- }
- }
- if (! function_exists('make')) {
- /**
- * @param $abstract
- * @param array $parameters
- * @return mixed
- * @throws @\Illuminate\Contracts\Container\BindingResolutionException
- */
- function make($abstract, array $parameters = [])
- {
- return app()->make($abstract, $parameters = []);
- }
- }
|