Skip to content

Commit 4159ba0

Browse files
author
riccardodallavia
committed
wip
1 parent 347b53a commit 4159ba0

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

Diff for: README.md

+53-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Laravel Helpers
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/maize-tech/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-helpers)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/maize-tech/laravel-helpers/run-tests?label=tests)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/maize-tech/laravel-helpers/Check%20&%20fix%20styling?label=code%20style)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-helpers/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-helpers/php-cs-fixer.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/maize-tech/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-helpers)
77

88
This repository contains some useful helpers for most applications using Laravel.
@@ -37,16 +37,9 @@ return [
3737
|
3838
*/
3939

40-
'macros' => [
41-
'anonymizeFilename' => \Maize\Helpers\Macros\AnonymizeFilename::class,
42-
'classUsesTrait' => \Maize\Helpers\Macros\ClassUsesTrait::class,
43-
'instanceofTypes' => \Maize\Helpers\Macros\InstanceofTypes::class,
44-
'isUrl' => \Maize\Helpers\Macros\IsUrl::class,
45-
'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class,
46-
'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class,
47-
'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class,
48-
'sanitizeUrl' => \Maize\Helpers\Macros\SanitizeUrl::class,
49-
],
40+
'macros' => Helper::defaultMacros()->merge([
41+
// 'methodName' => App\Example\ExampleClass::class,
42+
])->toArray(),
5043

5144
];
5245
```
@@ -72,6 +65,9 @@ hlp()->sanitizeUrl('mywebsite.com'); // using the helper function
7265
- [`modelKeyName`](#modelkeyname)
7366
- [`morphClassOf`](#morphclassof)
7467
- [`paginationLimit`](#paginationlimit)
68+
- [`pipe`](#pipe)
69+
- [`sanitizeArrayOfStrings`](#sanitizearrayofstrings)
70+
- [`sanitizeString`](#sanitizestring)
7571
- [`sanitizeUrl`](#sanitizeurl)
7672

7773
### `anonymizeFilename`
@@ -216,6 +212,49 @@ Article::paginate(
216212
);
217213
```
218214

215+
### `pipe`
216+
217+
The `pipe` function allows you to integrate Laravel pipelines with ease.
218+
The first parameter is the object to be sent through the pipeline, while the second is the list of pipes.
219+
220+
```php
221+
hlp()->pipe('test', [
222+
\Maize\Helpers\Tests\Support\Actions\Uppercase::class,
223+
]); // returns 'TEST'
224+
225+
hlp()->pipe('test', [
226+
\Maize\Helpers\Tests\Support\Actions\Uppercase::class,
227+
\Maize\Helpers\Tests\Support\Actions\Reverse::class,
228+
]); // returns 'TSET'
229+
230+
hlp()->pipe('', []) // returns an empty string
231+
```
232+
233+
### `sanitizeArrayOfStrings`
234+
235+
The `sanitizeArrayOfStrings` function sanitizes an array of strings by removing all HTML tags and whitespace from both ends.
236+
When passing an associative array, it also filters all keys with an empty value.
237+
238+
```php
239+
hlp()->sanitizeArrayOfStrings([' test ', ' test ']); // returns '['test', 'test']'
240+
241+
hlp()->sanitizeArrayOfStrings(['a' => ' test </h1> ', 'b' => ' test </h1> ']) // returns ['a' => 'test', 'b' => 'test']
242+
243+
hlp()->sanitizeArrayOfStrings(['a' => '']); // returns an empty array
244+
```
245+
246+
### `sanitizeString`
247+
248+
The `sanitizeString` function sanitizes a string by removing all HTML tags and whitespace from both ends.
249+
250+
```php
251+
hlp()->sanitizeString('<h1> test </h1>'); // returns 'test'
252+
253+
hlp()->sanitizeString(' <h1> test '); // returns 'test'
254+
255+
hlp()->sanitizeString('<br />') // returns an empty string
256+
```
257+
219258
### `sanitizeUrl`
220259

221260
The `sanitizeUrl` function prepends the specified url with the `https` protocol if none is set.
@@ -269,17 +308,9 @@ return [
269308
|
270309
*/
271310

272-
'macros' => [
273-
'anonymizeFilename' => \Maize\Helpers\Macros\AnonymizeFilename::class,
274-
'classUsesTrait' => \Maize\Helpers\Macros\ClassUsesTrait::class,
275-
'instanceofTypes' => \Maize\Helpers\Macros\InstanceofTypes::class,
276-
'isUrl' => \Maize\Helpers\Macros\IsUrl::class,
277-
'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class,
278-
'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class,
279-
'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class,
280-
'sanitizeUrl' => \Maize\Helpers\Macros\SanitizeUrl::class,
311+
'macros' => Helper::defaultMacros()->merge([
281312
'ping' => \App\Helpers\Macros\Ping::class,
282-
],
313+
])->toArray(),
283314

284315
];
285316
```

Diff for: src/Helper.php

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
use Illuminate\Support\Traits\Macroable;
77
use Maize\Helpers\Support\Config;
88

9+
/**
10+
* @method string anonymizeFilename(string $filename)
11+
* @method bool classUsesTrait(string $trait, mixed $class)
12+
* @method bool instanceofTypes(mixed $value, array|string|object $types)
13+
* @method bool isUrl(mixed $url)
14+
* @method string modelKeyName(\Illuminate\Database\Eloquent\Model|string $model)
15+
* @method string morphClassOf(\Illuminate\Database\Eloquent\Model|string $model)
16+
* @method int paginationLimit(int $default = 16, int $max = 48)
17+
* @method mixed pipe(mixed $passable, mixed $pipes)
18+
* @method array|null sanitizeArrayOfStrings(array|null $value, array|string|null $only = null)
19+
* @method string|null sanitizeString(string|null $value)
20+
* @method string|null sanitizeUrl(string|null $url)
21+
*/
922
class Helper
1023
{
1124
use Macroable;

Diff for: src/Macros/Pipe.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ class Pipe implements HelperMacro
99
{
1010
public function __invoke(): \Closure
1111
{
12-
return function (
13-
mixed $passable,
14-
mixed $pipes
15-
): mixed {
12+
return function (mixed $passable, mixed $pipes): mixed {
1613
return app(Pipeline::class)
1714
->send($passable)
1815
->through($pipes)

Diff for: src/Macros/SanitizeString.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __invoke(): \Closure
1515
}
1616

1717
return Str::of($value)
18-
->pipe('strip_tags')
18+
->stripTags()
1919
->trim()
2020
->toString();
2121
};

0 commit comments

Comments
 (0)