Skip to content

Commit

Permalink
New functions: domain and all_routes + Documentation Enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Apr 9, 2019
1 parent 286ac91 commit 0fb0f9b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 13 deletions.
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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: <a href="https://google.com">google.com</a> is a search machine
linkify('https://google.com is a search engine');
// returns: <a href="https://google.com">google.com</a> is a search engine

linkify('https://google.com is a search engine', ['https'], ['target' => '_blank']);
// returns: <a target="_blank" href="https://google.com">google.com</a> is a search engine
```

#### `embedded_video_url($url)`
Expand Down Expand Up @@ -669,7 +682,7 @@ extract_inline_img("<img src='data:image/jpeg;base64,...>", '/var/www/htdocs/lar
* MIT, see [LICENSE](https://github.com/repat/laravel-helper/blob/master/LICENSE)
## Version
* Version 0.1.20
* Version 0.1.21
## Contact
#### repat
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["laravel", "helper", "files", "array", "database", "strings", "date"],
"homepage": "http://repat.de",
"license": "MIT",
"version" : "0.1.20",
"version" : "0.1.21",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down Expand Up @@ -33,6 +33,7 @@
"suggest": {
"dimsav/laravel-translatable": "Required to use translated_attributes()",
"league/commonmark" : "Required to use markdown2html()",
"spatie/laravel-image-optimizer" : "Required to use ImageOptimizer in extract_inline_img()"
"spatie/laravel-image-optimizer" : "Required to use ImageOptimizer in extract_inline_img()",
"layershifter/tld-extract" : "Required to parse domains with domain()"
}
}
40 changes: 40 additions & 0 deletions src/html_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,43 @@ function ul_li_unpack(array $array, $separator = ':')
echo '</ul>';
}
}

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 '<a href="' . $route . '" ' . $tmp . '>' . $description . '</a>';#
break;
case 'delete':
case 'post':
case 'put':
$link = '<form action="' . $route . '" method="' . $method . '">\n';
foreach ($inputs as $id => $value) {
$link .= '<input type="hidden" id="' . $id . '" name="' . $id . ' " value="' . $value . '">\n';
}
$link .= '<input type="submit" value="' . $description . '">\n';
$link .= '</form>\n';
return $link;
break;
default:
return null;
}
}
}
24 changes: 24 additions & 0 deletions src/networking_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/optional_packages_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 0fb0f9b

Please sign in to comment.