diff --git a/README.md b/README.md index 8191d1d..3f37e97 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,8 @@ print_r(table_headers($user)); // returns: Array( 1 => id, 2 => name, ...) ``` -#### `print_db_session($table)` -`print_r()` the session of current user, `$table` is optional and defaults to `sessions`. +#### `print_db_session($table = 'sessions')` +`print_r()` the session of current user. ```php print_db_session(); @@ -124,7 +124,7 @@ get_free_slug('foobar', 'slug', User::class, 1, 'id'); ``` ### Date -#### `days_in_month($month, $year)` +#### `days_in_month($month = null, $year = null)` Returns amount of days in given month or year. Defaults to current month and year. ```php @@ -270,8 +270,8 @@ toggle(true); // returns: false ``` -#### `generate_password($size)` -Returns a random password, `$size` is optional and defaults to `15`. Syntactic sugar for `str_random()`. +#### `generate_password($size = 15)` +Returns a random password. Syntactic sugar for `str_random()`. ```php generate_password(); @@ -600,16 +600,29 @@ Uses [dimsav/laravel-translatable](https://github.com/dimsav/laravel-translatabl use App\Models\Product; translated_attributes(Product::class); -// returns ['title', 'description']; +// returns: ['title', 'description']; +``` + +#### `domain($url)` +Uses [layershifter/tld-extract](https://github.com/layershifter/tld-extract) to return the domain only from a URL, removing protocol, subdomain and path. + +* `$ composer require layershifter/tld-extract` + +```php +domain('https://repat.de/about?foo=bar'); +// returns: repat.de ``` ### HTML -#### `linkify($string, $protocols, $attributes)` -Returns the string with all URLs for given protocols made into links. Optionally, attributes for the [a tag](https://www.w3.org/TR/html4/struct/links.html) can be passed. `$protocols` defaults to an array of `'http', 'https', 'mail'`. +#### `linkify($string, $protocols = ['http', 'https', 'mail'], $attributes)` +Returns the string with all URLs for given protocols made into links. Optionally, attributes for the [a tag](https://www.w3.org/TR/html4/struct/links.html) can be passed. ```php -linkify('https://google.com is a search machine'); -// returns: google.com is a search machine +linkify('https://google.com is a search engine'); +// returns: google.com is a search engine + +linkify('https://google.com is a search engine', ['https'], ['target' => '_blank']); +// returns: google.com is a search engine ``` #### `embedded_video_url($url)` @@ -669,7 +682,7 @@ extract_inline_img("'; } } + +if (!function_exists('link')) { + /** + * Links/Form with specific HTTP Verb (WIP, undocumented) + * + * @param string $route + * @param string $description + * @param string $method + * @param array $inputs + * @return string|null + */ + function link(string $route, string $description, string $method, array $inputs = [], array $attributes = []) : ?string + { + $method = strtolower($method); + + if (!in_array($method, HTTP_VERBS)) { + return null; + } + + switch ($method) { + case 'get': + $tmp = trim(implode(' ', $attributes)); + return '' . $description . '';# + break; + case 'delete': + case 'post': + case 'put': + $link = '
\n'; + foreach ($inputs as $id => $value) { + $link .= '\n'; + } + $link .= '\n'; + $link .= '
\n'; + return $link; + break; + default: + return null; + } + } +} diff --git a/src/networking_helpers.php b/src/networking_helpers.php index 4de629d..735fe32 100644 --- a/src/networking_helpers.php +++ b/src/networking_helpers.php @@ -154,3 +154,27 @@ function current_route_name() : ?string return \Route::getCurrentRoute()->getName(); } } + +if (!function_exists('all_routes')) { + /** + * Array of all Routes and their properties + * + * @return array + */ + function all_routes() : array + { + $allRoutes = []; + $routes = \Route::getRoutes(); + + foreach ($routes as $route) { + $allRoutes[] = [ + 'name' => $route->getName(), + 'methods' => $route->methods(), // array + 'uri' => $route->uri(), + 'action' => $route->getActionName(), + ]; + } + + return $allRoutes; + } +} diff --git a/src/optional_packages_helpers.php b/src/optional_packages_helpers.php index e9a7cf5..5beddbf 100644 --- a/src/optional_packages_helpers.php +++ b/src/optional_packages_helpers.php @@ -29,3 +29,24 @@ function translated_attributes(string $fqcn) : array return $reflectionObj->getValue(app()->make($fqcn)); } } + +if (!function_exists('domain')) { + /** + * Gets domain without subdomain etc + * + * @param string $url + * @return string|null + */ + function domain(string $url) : ?string + { + $extract = new \LayerShifter\TLDExtract\Extract(); + $result = $extract->parse($url); + + // If domain parsing worked + if (!empty($result->getRegistrableDomain()) && $result->isValidDomain()) { + return $result->getRegistrableDomain(); + } + + return null; + } +}