-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
methods.php
398 lines (347 loc) · 11.7 KB
/
methods.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
use Kirby\Cms\App;
use Kirby\Cms\Field;
use Kirby\Cms\File;
use Kirby\Cms\Files;
use Kirby\Cms\Html;
use Kirby\Cms\Structure;
use Kirby\Cms\Page;
use Kirby\Cms\Url;
use Kirby\Data\Json;
use Kirby\Data\Yaml;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\V;
use Kirby\Toolkit\Xml;
/**
* Field method setup
*/
return function (App $app) {
return [
// states
/**
* Converts the field value into a proper boolean and inverts it
*
* @param Field $field
* @return boolean
*/
'isFalse' => function (Field $field): bool {
return $field->toBool() === false;
},
/**
* Converts the field value into a proper boolean
*
* @param Field $field
* @return boolean
*/
'isTrue' => function (Field $field): bool {
return $field->toBool() === true;
},
/**
* Validates the field content with the given validator and parameters
*
* @param string $validator
* @param mixed[] ...$arguments A list of optional validator arguments
* @return boolean
*/
'isValid' => function (Field $field, string $validator, ...$arguments): bool {
return V::$validator($field->value, ...$arguments);
},
// converters
/**
* Parses the field value with the given method
*
* @param Field $field
* @param string $method [',', 'yaml', 'json']
* @return array
*/
'toData' => function (Field $field, string $method = ',') {
switch ($method) {
case 'yaml':
return Yaml::decode($field->value);
case 'json':
return Json::decode($field->value);
default:
return $field->split($method);
}
},
/**
* Converts the field value into a proper boolean
*
* @param Field $field
* @param bool $default Default value if the field is empty
* @return bool
*/
'toBool' => function (Field $field, $default = false): bool {
$value = $field->isEmpty() ? $default : $field->value;
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
},
/**
* Converts the field value to a timestamp or a formatted date
*
* @param Field $field
* @param string $format PHP date formatting string
* @return string|int
*/
'toDate' => function (Field $field, string $format = null) use ($app) {
if (empty($field->value) === true) {
return null;
}
if ($format === null) {
return $field->toTimestamp();
}
return $app->option('date.handler', 'date')($format, $field->toTimestamp());
},
/**
* Returns a file object from a filename in the field
*
* @param Field $field
* @return File|null
*/
'toFile' => function (Field $field) {
return $field->toFiles()->first();
},
/**
* Returns a file collection from a yaml list of filenames in the field
*
* @return Files
*/
'toFiles' => function (Field $field, string $separator = 'yaml') {
$parent = $field->parent();
$files = new Files([]);
foreach ($field->toData($separator) as $id) {
if ($file = $parent->kirby()->file($id, $parent)) {
$files->add($file);
}
}
return $files;
},
/**
* Converts the field value into a proper float
*
* @param Field $field
* @param float $default Default value if the field is empty
* @return float
*/
'toFloat' => function (Field $field, float $default = 0) {
$value = $field->isEmpty() ? $default : $field->value;
return floatval($value);
},
/**
* Converts the field value into a proper integer
*
* @param Field $field
* @param int $default Default value if the field is empty
* @return int
*/
'toInt' => function (Field $field, int $default = 0) {
$value = $field->isEmpty() ? $default : $field->value;
return intval($value);
},
/**
* Wraps a link tag around the field value. The field value is used as the link text
*
* @param Field $field
* @param mixed $attr1 Can be an optional Url. If no Url is set, the Url of the Page, File or Site will be used. Can also be an array of link attributes
* @param mixed $attr2 If `$attr1` is used to set the Url, you can use `$attr2` to pass an array of additional attributes.
* @return string
*/
'toLink' => function (Field $field, $attr1 = null, $attr2 = null) {
if (is_string($attr1) === true) {
$href = $attr1;
$attr = $attr2;
} else {
$href = $field->parent()->url();
$attr = $attr1;
}
if ($field->parent()->isActive()) {
$attr['aria-current'] = 'page';
}
return Html::a($href, $field->value, $attr ?? []);
},
/**
* Returns a page object from a page id in the field
*
* @param Field $field
* @return Page|null
*/
'toPage' => function (Field $field) use ($app) {
return $field->toPages()->first();
},
/**
* Returns a pages collection from a yaml list of page ids in the field
*
* @param string $separator Can be any other separator to split the field value by
* @return Pages
*/
'toPages' => function (Field $field, string $separator = 'yaml') use ($app) {
return $app->site()->find(false, false, ...$field->toData($separator));
},
/**
* Converts a yaml field to a Structure object
*/
'toStructure' => function (Field $field) {
return new Structure(Yaml::decode($field->value), $field->parent());
},
/**
* Converts the field value to a Unix timestamp
*/
'toTimestamp' => function (Field $field) {
return strtotime($field->value);
},
/**
* Turns the field value into an absolute Url
*/
'toUrl' => function (Field $field) {
return Url::to($field->value);
},
/**
* Converts a user email address to a user object
*
* @return User|null
*/
'toUser' => function (Field $field) use ($app) {
return $field->toUsers()->first();
},
/**
* Returns a users collection from a yaml list of user email addresses in the field
*
* @return Users
*/
'toUsers' => function (Field $field, string $separator = 'yaml') use ($app) {
return $app->users()->find(false, false, ...$field->toData($separator));
},
// inspectors
/**
* Returns the length of the field content
*/
'length' => function (Field $field) {
return Str::length($field->value);
},
/**
* Returns the number of words in the text
*/
'words' => function (Field $field) {
return str_word_count(strip_tags($field->value));
},
// manipulators
/**
* Escapes the field value to be safely used in HTML
* templates without the risk of XSS attacks
*
* @param Field $field
* @param string $context html, attr, js or css
*/
'escape' => function (Field $field, string $context = 'html') {
$field->value = esc($field->value, $context);
return $field;
},
/**
* Creates an excerpt of the field value without html
* or any other formatting.
*/
'excerpt' => function (Field $field, int $chars = 0, bool $strip = true, string $rep = '…') {
$field->value = Str::excerpt($field->kirbytext()->value(), $chars, $strip, $rep);
return $field;
},
/**
* Converts the field content to valid HTML
*/
'html' => function (Field $field) {
$field->value = htmlentities($field->value, ENT_COMPAT, 'utf-8');
return $field;
},
/**
* Converts the field content from Markdown/Kirbytext to valid HTML
*/
'kirbytext' => function (Field $field) use ($app) {
$field->value = $app->kirbytext($field->value, [
'parent' => $field->parent(),
'field' => $field
]);
return $field;
},
/**
* Parses all KirbyTags without also parsing Markdown
*/
'kirbytags' => function (Field $field) use ($app) {
$field->value = $app->kirbytags($field->value, [
'parent' => $field->parent(),
'field' => $field
]);
return $field;
},
/**
* Converts the field content to lowercase
*/
'lower' => function (Field $field) {
$field->value = Str::lower($field->value);
return $field;
},
/**
* Converts markdown to valid HTML
*/
'markdown' => function (Field $field) use ($app) {
$field->value = $app->markdown($field->value);
return $field;
},
/**
* Converts the field content to valid XML
*/
'xml' => function (Field $field) {
$field->value = Xml::encode($field->value);
return $field;
},
/**
* Cuts the string after the given length and adds "…" if it is longer
*
* @param int $length The number of characters in the string
* @param string $appendix An optional replacement for the missing rest
* @return Field
*/
'short' => function (Field $field, int $length, string $appendix = '…') {
$field->value = Str::short($field->value, $length, $appendix);
return $field;
},
/**
* Converts the field content to a slug
*/
'slug' => function (Field $field) {
$field->value = Str::slug($field->value);
return $field;
},
/**
* Applies SmartyPants to the field
*/
'smartypants' => function (Field $field) use ($app) {
$field->value = $app->smartypants($field->value);
return $field;
},
/**
* Splits the field content into an array
*/
'split' => function (Field $field, $separator = ',') {
return Str::split((string)$field->value, $separator);
},
/**
* Converts the field content to uppercase
*/
'upper' => function (Field $field) {
$field->value = Str::upper($field->value);
return $field;
},
/**
* Avoids typographical widows in strings by replacing the last space with
*/
'widont' => function (Field $field) {
$field->value = Str::widont($field->value);
return $field;
},
// aliases
/**
* Parses yaml in the field content and returns an array
*/
'yaml' => function (Field $field): array {
return $field->toData('yaml');
},
];
};