@@ -27,14 +27,26 @@ return [
2727
2828 /*
2929 |--------------------------------------------------------------------------
30- | Helper class
30+ | Helper macros
3131 |--------------------------------------------------------------------------
3232 |
33- | Here you may specify the fully qualified class name of the helper class.
33+ | Here you may specify the full list of helper macros which will automatically
34+ | be registered on boot.
35+ | The key defines the method name, whereas the value should be the
36+ | fully qualified name of the invokable class.
3437 |
3538 */
3639
37- 'helper' => Maize\Helpers\Helper::class,
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+ ],
3850
3951];
4052```
@@ -53,55 +65,14 @@ hlp()->sanitizeUrl('mywebsite.com'); // using the helper function
5365
5466## Available methods
5567
56- - [ ` paginationLimit ` ] ( #paginationlimit )
5768- [ ` anonymizeFilename ` ] ( #anonymizefilename )
58- - [ ` sanitizeUrl ` ] ( #sanitizeurl )
59- - [ ` instanceofTypes ` ] ( #instanceoftypes )
6069- [ ` classUsesTrait ` ] ( #classusestrait )
61- - [ ` morphClassOf ` ] ( #morphclassof )
70+ - [ ` instanceofTypes ` ] ( #instanceoftypes )
71+ - [ ` isUrl ` ] ( #isurl )
6272- [ ` modelKeyName ` ] ( #modelkeyname )
63-
64- ### ` paginationLimit `
65-
66- The ` paginationLimit ` function returns the amount of items per page.
67-
68- It is useful when working with queries who need a pagination, and allows to define a default pagination limit and the max amount of items per page.
69-
70- It will also check whether the request's query string contains a ` limit ` parameter: if true, the given limit overrides the default limit.
71-
72- ``` php
73- use App\Models\Article;
74-
75- // use the default pagination limit (16 items)
76- // GET /api/articles
77- Article::paginate(
78- hlp()->paginationLimit() // returns 16 items
79- );
80-
81- // use the pagination limit given by the request query string
82- // GET /api/articles?limit=20
83- Article::paginate(
84- hlp()->paginationLimit() // returns 20 items
85- );
86-
87- // provide a custom default pagination limit
88- // GET /api/articles
89- Article::paginate(
90- hlp()->paginationLimit(30) // returns 30 items
91- );
92-
93- // when defined, the request query string limit overrides the default limit
94- // GET /api/articles?limit=20
95- Article::paginate(
96- hlp()->paginationLimit(30) // returns 20 items
97- );
98-
99- // provide a max limit of items for each page
100- // GET /api/articles?limit=200
101- Article::paginate(
102- hlp()->paginationLimit(16, 50) // returns 50 items
103- );
104- ```
73+ - [ ` morphClassOf ` ] ( #morphclassof )
74+ - [ ` paginationLimit ` ] ( #paginationlimit )
75+ - [ ` sanitizeUrl ` ] ( #sanitizeurl )
10576
10677### ` anonymizeFilename `
10778
@@ -115,16 +86,24 @@ string $filename = 'my-custom-file.xml';
11586hlp()->anonymizeFilename($filename);
11687```
11788
118- ### ` sanitizeUrl `
89+ ### ` classUsesTrait `
11990
120- The ` sanitizeUrl ` function prepends the specified url with the ` https ` protocol if none is set .
91+ The ` classUsesTrait ` function returns whether a class object or name uses the given trait or not .
12192
12293``` php
123- hlp()->sanitizeUrl('http://innovation.h-farm.com'); // returns 'http://innovation.h-farm.com'
94+ use App\Models\User;
95+ use Exception;
96+ use Illuminate\Database\Eloquent\Factories\HasFactory;
12497
125- hlp()->sanitizeUrl('innovation.h-farm.com'); // returns 'https://innovation.h-farm.com'
98+ $model = User::firstOrFail();
12699
127- hlp()->sanitizeUrl('') // returns an empty string
100+ hlp()->classUsesTrait(HasFactory::class, $model); // returns true
101+
102+ hlp()->classUsesTrait(HasFactory::class, User::class); // returns true
103+
104+ hlp()->classUsesTrait(Exception::class, $model); // returns false
105+
106+ hlp()->classUsesTrait(Exception::class, User::class); // returns false
128107```
129108
130109### ` instanceofTypes `
@@ -148,24 +127,28 @@ hlp()->instanceofTypes($model, [
148127]); // returns true
149128```
150129
151- ### ` classUsesTrait `
130+ ### ` isUrl `
152131
153- The ` classUsesTrait ` function returns whether a class object or name uses the given trait or not.
132+ The ` isUrl ` function returns whether a string is a valid URL or not.
154133
155134``` php
156- use App\Models\User;
157- use Exception;
158- use Illuminate\Database\Eloquent\Factories\HasFactory;
135+ hlp()->isUrl('https://my-application.test'); // returns true
159136
160- $model = User::firstOrFail();
137+ hlp()->isUrl('not-an-url'); // returns false
138+ ```
161139
162- hlp()->classUsesTrait(HasFactory::class, $model); // returns true
140+ ### ` modelKeyName `
163141
164- hlp()->classUsesTrait(HasFactory::class, User::class); // returns true
142+ The ` modelKeyName ` function returns the key name of a given model object or class name.
165143
166- hlp()->classUsesTrait(Exception::class, $model); // returns false
144+ ``` php
145+ use App\Models\User;
167146
168- hlp()->classUsesTrait(Exception::class, User::class); // returns false
147+ $model = User::firstOrFail();
148+
149+ hlp()->modelKeyName($model); // returns 'id'
150+
151+ hlp()->modelKeyName(User::class); // returns 'id'
169152```
170153
171154### ` morphClassOf `
@@ -191,68 +174,126 @@ hlp()->morphClassOf($model); // returns 'user'
191174hlp()->morphClassOf(User::class); // returns 'user'
192175```
193176
194- ### ` modelKeyName `
177+ ### ` paginationLimit `
195178
196- The ` modelKeyName ` function returns the key name of a given model object or class name.
179+ The ` paginationLimit ` function returns the amount of items per page.
180+
181+ It is useful when working with queries who need a pagination, and allows to define a default pagination limit and the max amount of items per page.
182+
183+ It will also check whether the request's query string contains a ` limit ` parameter: if true, the given limit overrides the default limit.
197184
198185``` php
199- use App\Models\User ;
186+ use App\Models\Article ;
200187
201- $model = User::firstOrFail();
188+ // use the default pagination limit (16 items)
189+ // GET /api/articles
190+ Article::paginate(
191+ hlp()->paginationLimit() // returns 16 items
192+ );
202193
203- hlp()->modelKeyName($model); // returns 'id'
194+ // use the pagination limit given by the request query string
195+ // GET /api/articles?limit=20
196+ Article::paginate(
197+ hlp()->paginationLimit() // returns 20 items
198+ );
204199
205- hlp()->modelKeyName(User::class); // returns 'id'
200+ // provide a custom default pagination limit
201+ // GET /api/articles
202+ Article::paginate(
203+ hlp()->paginationLimit(30) // returns 30 items
204+ );
205+
206+ // when defined, the request query string limit overrides the default limit
207+ // GET /api/articles?limit=20
208+ Article::paginate(
209+ hlp()->paginationLimit(30) // returns 20 items
210+ );
211+
212+ // provide a max limit of items for each page
213+ // GET /api/articles?limit=200
214+ Article::paginate(
215+ hlp()->paginationLimit(16, 50) // returns 50 items
216+ );
206217```
207218
208- ## Extending the Helper class
219+ ### ` sanitizeUrl `
220+
221+ The ` sanitizeUrl ` function prepends the specified url with the ` https ` protocol if none is set.
222+
223+ ``` php
224+ hlp()->sanitizeUrl('http://innovation.h-farm.com'); // returns 'http://innovation.h-farm.com'
225+
226+ hlp()->sanitizeUrl('innovation.h-farm.com'); // returns 'https://innovation.h-farm.com'
227+
228+ hlp()->sanitizeUrl('') // returns an empty string
229+ ```
209230
210- If needed, you can easily extend the ` Helper ` class to add your own methods.
231+ ## Adding custom helper methods
211232
212- All you have to do is create a new class which overrides the ` Helper ` base class and adds all the methods you need.
213- After that, you can override the ` helper ` attribute from ` config/helpers.php ` to match the newly created class.
233+ If needed, you can easily add your own helper methods.
214234
215- Here's an example of the output class:
235+ All you have to do is define your custom helper method using an invokable class:
216236
217237``` php
218238<?php
219239
220- namespace Support \Helpers;
240+ namespace App \Helpers\Macros ;
221241
222- use Maize\Helpers\Helper as BaseHelper;
223-
224- class Helper extends BaseHelper
242+ class Ping
225243{
226- public static function awesomeHelper (): string
244+ public function __invoke (): \Closure
227245 {
228- return "Just another awesome helper!";
246+ return function (): string {
247+ return 'pong';
248+ };
229249 }
230250}
231251```
232252
233- Whereas the ` helpers.php ` config file should look like this :
253+ After that, you can add your method to the ` macros ` attribute from ` config/helpers.php ` :
234254
235255``` php
236256return [
237257
238258 /*
239259 |--------------------------------------------------------------------------
240- | Helper class
260+ | Helper macros
241261 |--------------------------------------------------------------------------
242262 |
243- | Here you may specify the fully qualified class name of the helper class.
263+ | Here you may specify the full list of helper macros which will automatically
264+ | be registered on boot.
265+ | The key defines the method name, whereas the value should be the
266+ | fully qualified name of the invokable class.
244267 |
245268 */
246269
247- 'helper' => Support\Helpers\Helper::class,
270+ 'macros' => [
271+ 'anonymizeFilename' => \Maize\Helpers\Macros\AnonymizeFilename::class,
272+ 'classUsesTrait' => \Maize\Helpers\Macros\ClassUsesTrait::class,
273+ 'instanceofTypes' => \Maize\Helpers\Macros\InstanceofTypes::class,
274+ 'isUrl' => \Maize\Helpers\Macros\IsUrl::class,
275+ 'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class,
276+ 'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class,
277+ 'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class,
278+ 'sanitizeUrl' => \Maize\Helpers\Macros\SanitizeUrl::class,
279+ 'ping' => \App\Helpers\Macros\Ping::class,
280+ ],
248281
249282];
250283```
251284
252- You can then call all your custom-made helper methods using the ` hlp() ` function:
285+ Alternatively, if you need to define custom helpers at runtime, you can call the ` macro ` static method of the ` Helper ` class:
286+
287+ ``` php
288+ use Maize\Helpers\Helper;
289+
290+ Helper::macro('ping', fn (): string => 'pong');
291+ ```
292+
293+ You can now call your custom helper method with the ` hlp() ` helper method:
253294
254295``` php
255- hlp()->awesomeHelper (); // returns "Just another awesome helper! ";
296+ hlp()->ping (); // returns "pong ";
256297```
257298
258299## Testing
0 commit comments