123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- namespace Dcat\Admin\Support;
- use Dcat\Admin\Grid;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Artisan;
- use Illuminate\Support\Str;
- class Helper
- {
- /**
- * Update extension config.
- *
- * @param array $config
- *
- * @return bool
- */
- public static function updateExtensionConfig(array $config)
- {
- $files = app('files');
- $result = (bool) $files->put(config_path('admin-extensions.php'), self::exportArrayPhp($config));
- if ($result && is_file(base_path('bootstrap/cache/config.php'))) {
- Artisan::call('config:cache');
- }
- config(['admin-extensions' => $config]);
- return $result;
- }
- /**
- * Converts the given value to an array.
- *
- * @param $value
- * @param bool $filter
- *
- * @return array
- */
- public static function array($value, bool $filter = true): array
- {
- if (! $value) {
- return [];
- }
- if ($value instanceof \Closure) {
- $value = $value();
- }
- if (is_array($value)) {
- } elseif ($value instanceof Arrayable) {
- $value = $value->toArray();
- } elseif (is_string($value)) {
- $value = explode(',', $value);
- } else {
- $value = (array) $value;
- }
- return $filter ? array_filter($value, function ($v) {
- return $v !== '' && $v !== null;
- }) : $value;
- }
- /**
- * Converts the given value to string.
- *
- * @param mixed $value
- * @param array $params
- * @param object $newThis
- *
- * @return string
- */
- public static function render($value, $params = [], $newThis = null): string
- {
- if (is_string($value)) {
- return $value;
- }
- if ($value instanceof Grid) {
- return (string) $value->render();
- }
- if ($value instanceof \Closure) {
- $newThis && $value = $value->bindTo($newThis);
- $value = $value(...(array) $params);
- }
- if ($value instanceof Renderable) {
- return (string) $value->render();
- }
- if ($value instanceof Htmlable) {
- return (string) $value->toHtml();
- }
- return (string) $value;
- }
- /**
- * Build an HTML attribute string from an array.
- *
- * @param array $attributes
- *
- * @return string
- */
- public static function buildHtmlAttributes($attributes)
- {
- $html = '';
- foreach ((array) $attributes as $key => &$value) {
- if (is_numeric($key)) {
- $key = $value;
- }
- $element = '';
- if ($value !== null) {
- $element = $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8').'"';
- }
- $html .= $element;
- }
- return $html;
- }
- /**
- * Get url with the added query string parameters.
- *
- * @param string $url
- * @param array $query
- *
- * @return string
- */
- public static function urlWithQuery(?string $url, array $query = [])
- {
- if (! $url || ! $query) {
- return $url;
- }
- $array = explode('?', $url);
- $url = $array[0];
- $originalQuery = $array[1] ?? '';
- parse_str($originalQuery, $originalQuery);
- return $url.'?'.http_build_query(array_merge($originalQuery, $query));
- }
- /**
- * @param string $url
- * @param string|array|Arrayable $keys
- *
- * @return string
- */
- public static function urlWithoutQuery($url, $keys)
- {
- if (! Str::contains($url, '?') || ! $keys) {
- return $url;
- }
- if ($keys instanceof Arrayable) {
- $keys = $keys->toArray();
- }
- $keys = (array) $keys;
- $urlInfo = parse_url($url);
- parse_str($urlInfo['query'], $query);
- Arr::forget($query, $keys);
- $baseUrl = explode('?', $url)[0];
- return $query
- ? $baseUrl.'?'.http_build_query($query)
- : $baseUrl;
- }
- /**
- * Get full url without query strings.
- *
- * @param Arrayable|array|string $keys
- *
- * @return string
- */
- public static function fullUrlWithoutQuery($keys)
- {
- return static::urlWithoutQuery(request()->fullUrl(), $keys);
- }
- /**
- * If a request match the specific path.
- *
- * @example
- * Helper::matchRequestPath('auth/user')
- * Helper::matchRequestPath('auth/user*')
- * Helper::matchRequestPath('auth/user/* /edit')
- * Helper::matchRequestPath('GET,POST:auth/user')
- *
- * @param string $path
- * @param null|string $current
- *
- * @return bool
- */
- public static function matchRequestPath($path, ?string $current = null)
- {
- $request = request();
- $current = $current ?: $request->decodedPath();
- if (Str::contains($path, ':')) {
- [$methods, $path] = explode(':', $path);
- $methods = array_map('strtoupper', explode(',', $methods));
- if (! empty($methods) && ! in_array($request->method(), $methods)) {
- return false;
- }
- }
- if (! Str::contains($path, '*')) {
- return $path === $current;
- }
- $path = str_replace(['*', '/'], ['([0-9a-z-_,])*', "\/"], $path);
- return preg_match("/$path/i", $current);
- }
- /**
- * Build nested array.
- *
- * @param array $nodes
- * @param int $parentId
- * @param string|null $primaryKeyName
- * @param string|null $parentKeyName
- * @param string|null $childrenKeyName
- *
- * @return array
- */
- public static function buildNestedArray(
- $nodes = [],
- $parentId = 0,
- ?string $primaryKeyName = null,
- ?string $parentKeyName = null,
- ?string $childrenKeyName = null
- ) {
- $branch = [];
- $primaryKeyName = $primaryKeyName ?: 'id';
- $parentKeyName = $parentKeyName ?: 'parent_id';
- $childrenKeyName = $childrenKeyName ?: 'children';
- $parentId = is_numeric($parentId) ? (int) $parentId : $parentId;
- foreach ($nodes as $node) {
- $pk = Arr::get($node, $parentKeyName);
- $pk = is_numeric($pk) ? (int) $pk : $pk;
- if ($pk === $parentId) {
- $children = static::buildNestedArray(
- $nodes,
- Arr::get($node, $primaryKeyName),
- $primaryKeyName,
- $parentKeyName,
- $childrenKeyName
- );
- if ($children) {
- $node[$childrenKeyName] = $children;
- }
- $branch[] = $node;
- }
- }
- return $branch;
- }
- /**
- * Generate a URL friendly "slug" from a given string.
- *
- * @param string $name
- * @param string $symbol
- *
- * @return mixed
- */
- public static function slug(string $name, string $symbol = '-')
- {
- $text = preg_replace_callback('/([A-Z])/', function (&$text) use ($symbol) {
- return $symbol.strtolower($text[1]);
- }, $name);
- return str_replace('_', $symbol, ltrim($text, $symbol));
- }
- /**
- * @param array $array
- * @param int $level
- *
- * @return string
- */
- public static function exportArray(array &$array, $level = 1)
- {
- $start = '[';
- $end = ']';
- $txt = "$start\n";
- foreach ($array as $k => &$v) {
- if (is_array($v)) {
- $pre = is_string($k) ? "'$k' => " : "$k => ";
- $txt .= str_repeat(' ', $level * 4).$pre.static::exportArray($v, $level + 1).",\n";
- continue;
- }
- $t = $v;
- if ($v === true) {
- $t = 'true';
- } elseif ($v === false) {
- $t = 'false';
- } elseif ($v === null) {
- $t = 'null';
- } elseif (is_string($v)) {
- $v = str_replace("'", "\\'", $v);
- $t = "'$v'";
- }
- $pre = is_string($k) ? "'$k' => " : "$k => ";
- $txt .= str_repeat(' ', $level * 4)."{$pre}{$t},\n";
- }
- return $txt.str_repeat(' ', ($level - 1) * 4).$end;
- }
- /**
- * @param array $array
- *
- * @return string
- */
- public static function exportArrayPhp(array $array)
- {
- return "<?php \nreturn ".static::exportArray($array).";\n";
- }
- /**
- * Delete from array by value.
- *
- * @param array $array
- * @param mixed $value
- */
- public static function deleteByValue(&$array, $value)
- {
- $value = (array) $value;
- foreach ($array as $index => $item) {
- if (in_array($item, $value)) {
- unset($array[$index]);
- }
- }
- }
- }
|