Skip to content

Commit

Permalink
New function:insert_bindings+seconds for cache_get_or_add+CodingStandard
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Nov 4, 2019
1 parent 571b30f commit 6f63627
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -116,6 +116,14 @@ $posts = cache_get_or_add('posts', function() {
});
```

#### `insert_bindings($query)`
Inserts values into `?` from the `->toSql()` string.

```php
insert_bindings(DB::table('users')->where('id', 1));
// returns: SELECT * FROM `users` WHERE `id` = '1'
```

### 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.
Expand Down Expand Up @@ -228,7 +236,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.2.1
* Version 0.2.2
## Contact
#### repat
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -4,7 +4,7 @@
"keywords": ["laravel", "helper", "files", "array", "database", "strings", "date"],
"homepage": "https://repat.de",
"license": "MIT",
"version" : "0.2.1",
"version" : "0.2.2",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down
30 changes: 22 additions & 8 deletions src/database_helpers.php
@@ -1,6 +1,6 @@
<?php

if (!function_exists('mysql_headers')) {
if (! function_exists('mysql_headers')) {
/**
* Returns the headers of a MySQL/MariaDB table
* or empty array in case of error
Expand Down Expand Up @@ -28,7 +28,7 @@ function mysql_headers(string $table, bool $assoc = false) : array
}
}

if (!function_exists('print_db_session')) {
if (! function_exists('print_db_session')) {
/**
* Prints the session from the DB
*
Expand All @@ -44,13 +44,13 @@ function print_db_session(string $table = 'sessions') : void
->where('user_id', auth()->id())
->first()
->payload
)
)
);
)
)
);
}
}

if (!function_exists('get_free_slug')) {
if (! function_exists('get_free_slug')) {
/**
* Looks up a free slug for the `$fqcn` eloquent model,
* Suggesting `$toSlug` for field `$field`
Expand Down Expand Up @@ -83,7 +83,7 @@ function get_free_slug(string $toSlug, string $field, string $fqcn, ?int $id = n
}

// search for a free slug
while (!empty($query->first())) {
while (! empty($query->first())) {
$slug = str_slug($toSlug) . $counter;
$counter++;
$query = resolve($fqcn)::where($field, $slug);
Expand All @@ -96,7 +96,7 @@ function get_free_slug(string $toSlug, string $field, string $fqcn, ?int $id = n
}
}

if (!function_exists('table_headers')) {
if (! function_exists('table_headers')) {
/**
* Gets table headers the Laravel way via the Schema builder
*
Expand All @@ -108,3 +108,17 @@ function table_headers(\Illuminate\Database\Eloquent\Model $model) : array
return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
}
}

if (! function_exists('insert_bindings')) {
/**
* Uses toSql() and getBindings() to get full SQL Query
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
function insert_bindings(\Illuminate\Database\Query\Builder $query) : string
{
$querySql = str_replace(['?'], ['\'%s\''], $query->toSql());
return vsprintf($querySql, $query->getBindings());
}
}
4 changes: 2 additions & 2 deletions src/html_helpers.php
@@ -1,6 +1,6 @@
<?php

if (!function_exists('extract_inline_img')) {
if (! function_exists('extract_inline_img')) {
/**
* Extracts base64 encoded inline images from HTML text, save it to the harddrive
* and replace them in `$text` with a public URL
Expand All @@ -25,7 +25,7 @@ function extract_inline_img(string $text, string $storagePath, string $srcPath,
$filename = uniqid() . '.' . $extension;
$filePath = str_finish($storagePath, '/') . $filename;

if (!file_exists($storagePath)) {
if (! file_exists($storagePath)) {
mkdir($storagePath, $mode = 0777, $recursive = true);
}

Expand Down
16 changes: 8 additions & 8 deletions src/networking_helpers.php
@@ -1,6 +1,6 @@
<?php

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

Expand All @@ -19,9 +19,9 @@ function named_routes(string $filepath, ?string $verb = null) : ?array
$verb = strtolower($verb);
$regex = '/name\(\'([0-9a-z\.\_]*)\'\)';

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

Expand All @@ -33,7 +33,7 @@ function named_routes(string $filepath, ?string $verb = null) : ?array
}
}

if (!function_exists('routes_path')) {
if (! function_exists('routes_path')) {
/**
* Get the path to the routes folder, similar to `app_path()` etc
*
Expand All @@ -46,7 +46,7 @@ function routes_path(string $path = '') : string
}
}

if (!function_exists('current_route_name')) {
if (! function_exists('current_route_name')) {
/**
* If the current Route has a name, otherwise return `null`
*
Expand All @@ -58,7 +58,7 @@ function current_route_name() : ?string
}
}

if (!function_exists('all_routes')) {
if (! function_exists('all_routes')) {
/**
* Array of all Routes and their properties
*
Expand All @@ -82,7 +82,7 @@ function all_routes() : array
}
}

if (!function_exists('route_exists')) {
if (! function_exists('route_exists')) {
/**
* Wrapper around all_routes()
*
Expand Down
11 changes: 6 additions & 5 deletions src/object_helpers.php
@@ -1,6 +1,6 @@
<?php

if (!function_exists('morph_map')) {
if (! function_exists('morph_map')) {
/**
* Returns Laravels MorphMap set in `AppServiceProvider`
*
Expand All @@ -12,7 +12,7 @@ function morph_map() : array
}
}

if (!function_exists('morph_map_key')) {
if (! function_exists('morph_map_key')) {
/**
* Returns short version of $fqcn class
* (Reverse search in morphMap)
Expand All @@ -28,18 +28,19 @@ function morph_map_key(string $fqcn) : ?string
}
}

if (!function_exists('cache_get_or_add')) {
if (! function_exists('cache_get_or_add')) {
/**
* Transforms an object into an array
*
* @param string $key
* @param callable $function
* @param int|null $seconds
* @return mixed
*/
function cache_get_or_add(string $key, callable $function)
function cache_get_or_add(string $key, callable $function, ?int $seconds = null)
{
$result = \Cache::get($key, $function);
\Cache::add($key, $result);
\Cache::add($key, $result, $seconds);
return $result;
}
}
3 changes: 2 additions & 1 deletion src/optional_packages_helpers.php
@@ -1,5 +1,6 @@
<?php
if (!function_exists('translated_attributes')) {

if (! function_exists('translated_attributes')) {
/**
* Uses Reflection to get the translated attributes of
* `astrotomic/laravel-translatable` Models
Expand Down

0 comments on commit 6f63627

Please sign in to comment.