@@ -27,14 +27,26 @@ return [
27
27
28
28
/*
29
29
|--------------------------------------------------------------------------
30
- | Helper class
30
+ | Helper macros
31
31
|--------------------------------------------------------------------------
32
32
|
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.
34
37
|
35
38
*/
36
39
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
+ ],
38
50
39
51
];
40
52
```
@@ -53,55 +65,14 @@ hlp()->sanitizeUrl('mywebsite.com'); // using the helper function
53
65
54
66
## Available methods
55
67
56
- - [ ` paginationLimit ` ] ( #paginationlimit )
57
68
- [ ` anonymizeFilename ` ] ( #anonymizefilename )
58
- - [ ` sanitizeUrl ` ] ( #sanitizeurl )
59
- - [ ` instanceofTypes ` ] ( #instanceoftypes )
60
69
- [ ` classUsesTrait ` ] ( #classusestrait )
61
- - [ ` morphClassOf ` ] ( #morphclassof )
70
+ - [ ` instanceofTypes ` ] ( #instanceoftypes )
71
+ - [ ` isUrl ` ] ( #isurl )
62
72
- [ ` 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 )
105
76
106
77
### ` anonymizeFilename `
107
78
@@ -115,16 +86,24 @@ string $filename = 'my-custom-file.xml';
115
86
hlp()->anonymizeFilename($filename);
116
87
```
117
88
118
- ### ` sanitizeUrl `
89
+ ### ` classUsesTrait `
119
90
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 .
121
92
122
93
``` 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;
124
97
125
- hlp()->sanitizeUrl('innovation.h-farm.com'); // returns 'https://innovation.h-farm.com'
98
+ $model = User::firstOrFail();
126
99
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
128
107
```
129
108
130
109
### ` instanceofTypes `
@@ -148,24 +127,28 @@ hlp()->instanceofTypes($model, [
148
127
]); // returns true
149
128
```
150
129
151
- ### ` classUsesTrait `
130
+ ### ` isUrl `
152
131
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.
154
133
155
134
``` 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
159
136
160
- $model = User::firstOrFail();
137
+ hlp()->isUrl('not-an-url'); // returns false
138
+ ```
161
139
162
- hlp()->classUsesTrait(HasFactory::class, $model); // returns true
140
+ ### ` modelKeyName `
163
141
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.
165
143
166
- hlp()->classUsesTrait(Exception::class, $model); // returns false
144
+ ``` php
145
+ use App\Models\User;
167
146
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'
169
152
```
170
153
171
154
### ` morphClassOf `
@@ -191,68 +174,126 @@ hlp()->morphClassOf($model); // returns 'user'
191
174
hlp()->morphClassOf(User::class); // returns 'user'
192
175
```
193
176
194
- ### ` modelKeyName `
177
+ ### ` paginationLimit `
195
178
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.
197
184
198
185
``` php
199
- use App\Models\User ;
186
+ use App\Models\Article ;
200
187
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
+ );
202
193
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
+ );
204
199
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
+ );
206
217
```
207
218
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
+ ```
209
230
210
- If needed, you can easily extend the ` Helper ` class to add your own methods.
231
+ ## Adding custom helper methods
211
232
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.
214
234
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:
216
236
217
237
``` php
218
238
<?php
219
239
220
- namespace Support \Helpers;
240
+ namespace App \Helpers\Macros ;
221
241
222
- use Maize\Helpers\Helper as BaseHelper;
223
-
224
- class Helper extends BaseHelper
242
+ class Ping
225
243
{
226
- public static function awesomeHelper (): string
244
+ public function __invoke (): \Closure
227
245
{
228
- return "Just another awesome helper!";
246
+ return function (): string {
247
+ return 'pong';
248
+ };
229
249
}
230
250
}
231
251
```
232
252
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 ` :
234
254
235
255
``` php
236
256
return [
237
257
238
258
/*
239
259
|--------------------------------------------------------------------------
240
- | Helper class
260
+ | Helper macros
241
261
|--------------------------------------------------------------------------
242
262
|
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.
244
267
|
245
268
*/
246
269
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
+ ],
248
281
249
282
];
250
283
```
251
284
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:
253
294
254
295
``` php
255
- hlp()->awesomeHelper (); // returns "Just another awesome helper! ";
296
+ hlp()->ping (); // returns "pong ";
256
297
```
257
298
258
299
## Testing
0 commit comments