Skip to content

Commit 347b53a

Browse files
author
enricodelazzari
committed
ADD new helpers
1 parent 46cf1e8 commit 347b53a

File tree

11 files changed

+183
-19
lines changed

11 files changed

+183
-19
lines changed

config/helpers.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Maize\Helpers\Helper;
4+
35
return [
46

57
/*
@@ -14,15 +16,8 @@
1416
|
1517
*/
1618

17-
'macros' => [
18-
'anonymizeFilename' => \Maize\Helpers\Macros\AnonymizeFilename::class,
19-
'classUsesTrait' => \Maize\Helpers\Macros\ClassUsesTrait::class,
20-
'instanceofTypes' => \Maize\Helpers\Macros\InstanceofTypes::class,
21-
'isUrl' => \Maize\Helpers\Macros\IsUrl::class,
22-
'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class,
23-
'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class,
24-
'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class,
25-
'sanitizeUrl' => \Maize\Helpers\Macros\SanitizeUrl::class,
26-
],
19+
'macros' => Helper::defaultMacros()->merge([
20+
// 'methodName' => App\Example\ExampleClass::class,
21+
])->toArray(),
2722

2823
];

src/Helper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\Helpers;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Traits\Macroable;
67
use Maize\Helpers\Support\Config;
78

@@ -15,4 +16,21 @@ public static function register(): void
1516
->reject(fn ($class, $macro) => Helper::hasMacro($macro))
1617
->each(fn ($class, $macro) => Helper::macro($macro, app($class)()));
1718
}
19+
20+
public static function defaultMacros(): Collection
21+
{
22+
return collect([
23+
'anonymizeFilename' => \Maize\Helpers\Macros\AnonymizeFilename::class,
24+
'classUsesTrait' => \Maize\Helpers\Macros\ClassUsesTrait::class,
25+
'instanceofTypes' => \Maize\Helpers\Macros\InstanceofTypes::class,
26+
'isUrl' => \Maize\Helpers\Macros\IsUrl::class,
27+
'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class,
28+
'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class,
29+
'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class,
30+
'pipe' => \Maize\Helpers\Macros\Pipe::class,
31+
'sanitizeArrayOfStrings' => \Maize\Helpers\Macros\SanitizeArrayOfStrings::class,
32+
'sanitizeString' => \Maize\Helpers\Macros\SanitizeString::class,
33+
'sanitizeUrl' => \Maize\Helpers\Macros\SanitizeUrl::class,
34+
]);
35+
}
1836
}

src/Macros/Pipe.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Maize\Helpers\Macros;
4+
5+
use Illuminate\Pipeline\Pipeline;
6+
use Maize\Helpers\HelperMacro;
7+
8+
class Pipe implements HelperMacro
9+
{
10+
public function __invoke(): \Closure
11+
{
12+
return function (
13+
mixed $passable,
14+
mixed $pipes
15+
): mixed {
16+
return app(Pipeline::class)
17+
->send($passable)
18+
->through($pipes)
19+
->thenReturn();
20+
};
21+
}
22+
}

src/Macros/SanitizeArrayOfStrings.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Maize\Helpers\Macros;
4+
5+
use Maize\Helpers\HelperMacro;
6+
7+
class SanitizeArrayOfStrings implements HelperMacro
8+
{
9+
public function __invoke(): \Closure
10+
{
11+
return function (array|null $value, array|string|null $only = null): array|null {
12+
if (is_null($value)) {
13+
return null;
14+
}
15+
16+
return collect($value)
17+
->only($only)
18+
->map(
19+
fn ($item) => app(SanitizeString::class)()($item)
20+
)
21+
->filter()
22+
->toArray();
23+
};
24+
}
25+
}

src/Macros/SanitizeString.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Maize\Helpers\Macros;
4+
5+
use Illuminate\Support\Str;
6+
use Maize\Helpers\HelperMacro;
7+
8+
class SanitizeString implements HelperMacro
9+
{
10+
public function __invoke(): \Closure
11+
{
12+
return function (?string $value): ?string {
13+
if (is_null($value)) {
14+
return null;
15+
}
16+
17+
return Str::of($value)
18+
->pipe('strip_tags')
19+
->trim()
20+
->toString();
21+
};
22+
}
23+
}

tests/HelpersTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Eloquent\Factories\HasFactory;
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Foundation\Auth\User;
6-
use Maize\Helpers\Tests\Models\Article;
6+
use Maize\Helpers\Tests\Support\Models\Article;
77

88
use function PHPUnit\Framework\assertNotEquals;
99

@@ -102,6 +102,66 @@
102102

103103
]);
104104

105+
it('pipe', function ($passable, $pipes, $result) {
106+
expect(hlp()->pipe($passable, $pipes))->toBe($result);
107+
})->with([
108+
['passable' => null, 'pipes' => [], 'result' => null],
109+
['passable' => [], 'pipes' => [], 'result' => []],
110+
['passable' => '', 'pipes' => [], 'result' => ''],
111+
['passable' => 'test', 'pipes' => [], 'result' => 'test'],
112+
['passable' => 'test', 'pipes' => [
113+
\Maize\Helpers\Tests\Support\Actions\Uppercase::class,
114+
\Maize\Helpers\Tests\Support\Actions\Reverse::class,
115+
], 'result' => 'TSET'],
116+
['passable' => 'test', 'pipes' => [
117+
\Maize\Helpers\Tests\Support\Actions\Reverse::class,
118+
\Maize\Helpers\Tests\Support\Actions\Uppercase::class,
119+
], 'result' => 'TSET'],
120+
['passable' => 'test', 'pipes' => [
121+
\Maize\Helpers\Tests\Support\Actions\Uppercase::class,
122+
], 'result' => 'TEST'],
123+
['passable' => 'test', 'pipes' => [
124+
\Maize\Helpers\Tests\Support\Actions\Reverse::class,
125+
], 'result' => 'tset'],
126+
]);
127+
128+
it('sanitizeString', function (?string $string, $result) {
129+
expect(hlp()->sanitizeString($string))->toBe($result);
130+
})->with([
131+
['string' => null, 'result' => null],
132+
['string' => '', 'result' => ''],
133+
['string' => ' test ', 'result' => 'test'],
134+
['string' => '<h1> test </h1>', 'result' => 'test'],
135+
['string' => ' <h1> test </h1> ', 'result' => 'test'],
136+
['string' => ' test </h1> ', 'result' => 'test'],
137+
['string' => ' <h1> test ', 'result' => 'test'],
138+
['string' => 'test<br>', 'result' => 'test'],
139+
['string' => 'test<br />', 'result' => 'test'],
140+
['string' => '<h1></h1>', 'result' => ''],
141+
['string' => '<br />', 'result' => ''],
142+
]);
143+
144+
it('sanitizeArrayOfStrings', function (?array $array, $result) {
145+
expect(hlp()->sanitizeArrayOfStrings($array))->toBe($result);
146+
})->with([
147+
['array' => null, 'result' => null],
148+
['array' => [], 'result' => []],
149+
['array' => [null], 'result' => []],
150+
['array' => ['a' => ''], 'result' => []],
151+
['array' => ['a' => null], 'result' => []],
152+
['array' => [' test ', ' test '], 'result' => ['test', 'test']],
153+
['array' => ['a' => ' test ', 'b' => ' test '],'result' => ['a' => 'test', 'b' => 'test']],
154+
['array' => ['a' => '<h1> test </h1>', 'b' => '<h1> test </h1>'],'result' => ['a' => 'test', 'b' => 'test']],
155+
['array' => ['a' => ' <h1> test </h1> ', 'b' => ' <h1> test </h1> '],'result' => ['a' => 'test', 'b' => 'test']],
156+
['array' => ['a' => ' test </h1> ', 'b' => ' test </h1> '],'result' => ['a' => 'test', 'b' => 'test']],
157+
['array' => [' test </h1> ', ' test </h1> '], 'result' => ['test', 'test']],
158+
['array' => [' <h1> test ', ' <h1> test '], 'result' => ['test', 'test']],
159+
['array' => ['test<br>', 'test<br>'], 'result' => ['test', 'test']],
160+
['array' => ['test<br />', 'test<br />'], 'result' => ['test', 'test']],
161+
['array' => ['<h1></h1>', '<h1></h1>'], 'result' => []],
162+
['array' => ['<br />', '<br />'], 'result' => []],
163+
]);
164+
105165
it('sanitizeUrl', function (?string $url, $result) {
106166
expect(hlp()->sanitizeUrl($url))->toBe($result);
107167
})->with([

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
use Maize\Helpers\Tests\TestCase;
3+
use Maize\Helpers\Tests\Support\TestCase;
44

55
uses(TestCase::class)->in(__DIR__);

tests/Support/Actions/Reverse.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Maize\Helpers\Tests\Support\Actions;
4+
5+
class Reverse
6+
{
7+
public function __invoke(string $string, \Closure $next): string
8+
{
9+
return $next(
10+
strrev($string)
11+
);
12+
}
13+
}

tests/Support/Actions/Uppercase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Maize\Helpers\Tests\Support\Actions;
4+
5+
class Uppercase
6+
{
7+
public function handle(string $string, \Closure $next): string
8+
{
9+
return $next(
10+
strtoupper($string)
11+
);
12+
}
13+
}

tests/Models/Article.php renamed to tests/Support/Models/Article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Maize\Helpers\Tests\Models;
3+
namespace Maize\Helpers\Tests\Support\Models;
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;

tests/TestCase.php renamed to tests/Support/TestCase.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Maize\Helpers\Tests;
3+
namespace Maize\Helpers\Tests\Support;
44

55
use Maize\Helpers\HelperServiceProvider;
66
use Orchestra\Testbench\TestCase as Orchestra;
@@ -18,9 +18,4 @@ protected function getPackageProviders($app)
1818
HelperServiceProvider::class,
1919
];
2020
}
21-
22-
public function getEnvironmentSetUp($app)
23-
{
24-
//
25-
}
2621
}

0 commit comments

Comments
 (0)