Skip to content

Commit

Permalink
New functions: named_routes,str_remove,route_path
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Apr 5, 2019
1 parent dce223f commit e00027c
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 4 deletions.
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,45 @@ gettype(auto_cast('true'));
// returns: boolean
```

### Networking
#### `route_path($path)`
Get the path to the Laravel routes folder, similar to `app_path()`, see [Helpers Documentation](https://laravel.com/docs/5.8/helpers). It will append `$path` but it's not mandatory.

```php
route_path();
// returns: /var/www/htdocs/laravel/routes

route_path('web.php');
// returns: /var/www/htdocs/laravel/routes/web.php
```

#### `named_routes($path, $verb)`
Returns array of all named routes in a routes file or `null` on error. It's possible to pass an HTTP verb/method defined in `HTTP_VERBS_LARAVEL` (see below).

```php
named_routes('/var/www/htdocs/laravel/routes/web.php');
// returns: [
// 'laravel.get'
// 'laravel.post'
// ]

named_routes('/var/www/htdocs/laravel/routes/web.php', 'get');
// returns: [
// 'laravel.get'
// ]
```

#### `scrub_url($url)`
Removes the protocol, www and trailing slashes from a URL.

```php
scrub_url('https://www.repat.de/');
// returns: 'repat.de'

scrub_url('https://blog.fefe.de/?ts=a262bcdf');
// returns: 'blog.fefe.de/?ts=a262bcdf'
```

### String
#### `str_icontains($haystack, $needle)`
Similar to [Str::contains()](https://laravel.com/docs/5.7/helpers#method-str-contains) but case _insensitive_.
Expand Down Expand Up @@ -321,6 +360,35 @@ str_slug('blogfefe.de');
// returns: blogfefede // same as subdomain on fefe.de
```

#### `str_remove($string, $remove)`
Removes given string(s), numbers or array of strings. Syntactic sugar for `str_replace($remove, '', $string)`.

```php
str_remove('foobar', 'bar');
// returns: foo
str_remove('foobar42', ['foo', 'bar']);
// returns: 42
str_remove('foobar42', 42);
// returns: foobar
```

### Constants
* `DAYS_PER_YEAR`: 365
* `PARETO_HIGH`: 80
* `PARETO_LOW`: 20
* `HTTP_1_0_VERBS`: [get, head, post]
* `HTTP_1_1_VERBS`: [get, head, post, connect, delete, options, put, trace]
* `HTTP_VERBS`: [get, head, post, connect, delete, options, put, trace, patch]
* `HTTP_VERBS_LARAVEL`: [get, head, post, delete, options, put, patch]
* `REGEX_WORD_BOUNDARY`: \\b
* `REGEX_IMG_BASE64_SRC`: Regular Expression used to find a base64 encoded image in HTML text
* `REGEX_IMG_BASE64_REPLACE`: Regular Expression used to replace a base64 encoded image in HTML text
* `REGEX_FIRST_RESULT_KEY`: 1
* `MACOS`: macos
* `WINDOWS`: windows
* `LINUX`: linux
* `BSD`: bsd

## Undocumented
### database
* `get_free_slug()`
Expand All @@ -340,7 +408,6 @@ str_slug('blogfefe.de');
### networking
* `http_status_code()`
* `domain_slug()`
* `scrub_url()`
* `parse_signed_request()`

### object
Expand All @@ -363,7 +430,7 @@ str_slug('blogfefe.de');
* MIT, see [LICENSE](https://github.com/repat/laravel-helper/blob/master/LICENSE)

## Version
* Version 0.1.17
* Version 0.1.18

## Contact
#### repat
Expand Down
2 changes: 1 addition & 1 deletion 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.17",
"version" : "0.1.18",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down
36 changes: 36 additions & 0 deletions src/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@
*/
define('PARETO_LOW', 20);

/*
|--------------------------------------------------------------------------
| Networking
|--------------------------------------------------------------------------
*/

/**
* HTTP 1.0 Verbs (rfc1945)
* @var array
*/
define('HTTP_1_0_VERBS', ['get', 'head', 'post']);

/**
* HTTP 1.1 Verbs (rfc2616)
* @var array
*/
define('HTTP_1_1_VERBS', array_merge(HTTP_1_0_VERBS, ['connect', 'delete', 'options', 'put', 'trace']));

/**
* All HTTP Verbs including PATCH (rfc5789)
* @var array
*/
define('HTTP_VERBS', array_merge(HTTP_1_1_VERBS, ['patch']));

/**
* All HTTP Verbs Laravel understand in a routes
* @var array
*/
define('HTTP_VERBS_LARAVEL', array_merge(HTTP_1_0_VERBS, ['all', 'delete', 'options', 'put']));

/*
|--------------------------------------------------------------------------
| Regex
Expand All @@ -54,6 +84,12 @@
*/
define('REGEX_IMG_BASE64_REPLACE', '/src=(\"data:image\/[a-zA-Z]*;base64,[^\"]*)\"/');

/**
* Key for the first result of `preg_match_all`
* @var int
*/
define('REGEX_FIRST_RESULT_KEY', 1);

/*
|--------------------------------------------------------------------------
| Operating Systems
Expand Down
48 changes: 47 additions & 1 deletion src/networking_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function domain_slug(string $domain) : ?string

if (!function_exists('scrub_url')) {
/**
* Removes protocols, subdomains and slashes from URL
* Removes protocols, www and trailing slashes from URL
*
* @param string $url
* @return string
Expand Down Expand Up @@ -96,3 +96,49 @@ function parse_signed_request(string $signedRequest, string $secret, string $alg
return $data;
}
}

if (!function_exists('named_routes')) {
/**
* Returns array of all named routes in a routes file or null on error
*
* @param string $filepath
* @param string|null $verb
* @return null|array
*/
function named_routes(string $filepath, ?string $verb = null) : ?array
{
if (!file_exists($filepath)) {
return null;
}

$content = file_get_contents($filepath);

$verb = strtolower($verb);
$regex = '/name\(\'([0-9a-z\.\_]*)\'\)';

if (!empty($verb) && !in_array($verb, HTTP_VERBS_LARAVEL)) {
return null;
} elseif (!empty($verb)) {
$regex .= '.*\-\>' . $verb . '\(/';
}

// filter with regex
$results = [];
$found = preg_match_all(str_finish($regex, '/'), $content, $results);

return array_key_exists(REGEX_FIRST_RESULT_KEY, $results) ? $results[REGEX_FIRST_RESULT_KEY] : [];
}
}

if (!function_exists('routes_path')) {
/**
* Get the path to the routes folder, similar to `app_path()` etc
*
* @param string $path
* @return string
*/
function routes_path(string $path = '') : string
{
return base_path() . '/routes' . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}
}
18 changes: 18 additions & 0 deletions src/string_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,21 @@ function sluggify_domain(string $domain)
return str_replace('.', '_', strtolower($domain));
}
}

if (!function_exists('str_remove')) {
/**
* Removes given string(s), syntactic sugar for str_replace
* Returns null on error
*
* @param string $string
* @param int|float|string|array $remove
* @return null|string
*/
function str_remove(string $string, $remove) : ?string
{
if (!(is_array($remove) || is_string($remove) || is_numeric($remove))) {
return null;
}
return str_replace($remove, '', $string);
}
}

0 comments on commit e00027c

Please sign in to comment.