@@ -121,27 +121,27 @@ public function callback(Closure $callback): mixed
121
121
* templates without the risk of XSS attacks
122
122
*
123
123
* @param string $context Location of output (`html`, `attr`, `js`, `css`, `url` or `xml`)
124
- * @return $this
125
124
*/
126
125
public function escape (string $ context = 'html ' ): static
127
126
{
128
- $ this ->value = Str::esc ($ this ->value ?? '' , $ context );
129
- return $ this ;
127
+ return $ this ->value (fn ($ value ) => Str::esc ($ value ?? '' , $ context ));
130
128
}
131
129
132
130
/**
133
131
* Creates an excerpt of the field value without html
134
132
* or any other formatting.
135
- * @return $this
136
133
*/
137
134
public function excerpt (
138
135
int $ chars = 0 ,
139
136
bool $ strip = true ,
140
137
string $ rep = ' … '
141
138
): static {
142
- $ value = $ this ->kirbytext ()->value ();
143
- $ this ->value = Str::excerpt ($ value , $ chars , $ strip , $ rep );
144
- return $ this ;
139
+ return $ this ->value (Str::excerpt (
140
+ string: $ this ->kirbytext ()->value (),
141
+ chars: $ chars ,
142
+ strip: $ strip ,
143
+ rep: $ rep
144
+ ));
145
145
}
146
146
147
147
/**
@@ -154,20 +154,17 @@ public function exists(): bool
154
154
155
155
/**
156
156
* Converts the field content to valid HTML
157
- * @return $this
158
157
*/
159
158
public function html (): static
160
159
{
161
- $ this ->value = Html::encode ($ this ->value );
162
- return $ this ;
160
+ return $ this ->value (fn ($ value ) => Html::encode ($ value ));
163
161
}
164
162
165
163
/**
166
164
* Strips all block-level HTML elements from the field value,
167
165
* it can be safely placed inside of other inline elements
168
166
* without the risk of breaking the HTML structure.
169
167
* @since 3.3.0
170
- * @return $this
171
168
*/
172
169
public function inline (): static
173
170
{
@@ -176,8 +173,9 @@ public function inline(): static
176
173
// Obsolete elements, script tags, image maps and form elements have
177
174
// been excluded for safety reasons and as they are most likely not
178
175
// needed in most cases.
179
- $ this ->value = strip_tags ($ this ->value ?? '' , Html::$ inlineList );
180
- return $ this ;
176
+ return $ this ->value (
177
+ fn ($ value ) => strip_tags ($ value ?? '' , Html::$ inlineList )
178
+ );
181
179
}
182
180
183
181
/**
@@ -249,44 +247,37 @@ public function kirby(): App
249
247
250
248
/**
251
249
* Parses all KirbyTags without also parsing Markdown
252
- * @return $this
253
250
*/
254
251
public function kirbytags (): static
255
252
{
256
- $ this ->value = $ this ->kirby ()->kirbytags (
257
- text: $ this -> value ,
253
+ return $ this ->value ( fn ( $ value , $ field ) => $ field ->kirby ()->kirbytags (
254
+ text: $ value ,
258
255
data: [
259
- 'parent ' => $ this ->parent (),
260
- 'field ' => $ this
256
+ 'parent ' => $ field ->parent (),
257
+ 'field ' => $ field
261
258
]
262
- );
263
-
264
- return $ this ;
259
+ ));
265
260
}
266
261
267
262
/**
268
263
* Converts the field content from Markdown/Kirbytext to valid HTML
269
- * @return $this
270
264
*/
271
265
public function kirbytext (array $ options = []): static
272
266
{
273
- $ this ->value = $ this ->kirby ()->kirbytext (
274
- text: $ this -> value ,
267
+ return $ this ->value ( fn ( $ value , $ field ) => $ field ->kirby ()->kirbytext (
268
+ text: $ value ,
275
269
options: [
276
270
...$ options ,
277
- 'parent ' => $ this ->parent (),
278
- 'field ' => $ this
271
+ 'parent ' => $ field ->parent (),
272
+ 'field ' => $ field
279
273
]
280
- );
281
-
282
- return $ this ;
274
+ ));
283
275
}
284
276
285
277
/**
286
278
* Converts the field content from inline Markdown/Kirbytext
287
279
* to valid HTML
288
280
* @since 3.1.0
289
- * @return $this
290
281
*/
291
282
public function kirbytextInline (array $ options = []): static
292
283
{
@@ -305,25 +296,21 @@ public function length(): int
305
296
306
297
/**
307
298
* Converts the field content to lowercase
308
- * @return $this
309
299
*/
310
300
public function lower (): static
311
301
{
312
- $ this ->value = Str::lower ($ this ->value );
313
- return $ this ;
302
+ return $ this ->value (fn ($ value ) => Str::lower ($ value ));
314
303
}
315
304
316
305
/**
317
306
* Converts markdown to valid HTML
318
- * @return $this
319
307
*/
320
308
public function markdown (array $ options = []): static
321
309
{
322
- $ this ->value = $ this ->kirby ()->markdown (
323
- text: $ this -> value ,
310
+ return $ this ->value ( fn ( $ value ) => $ this ->kirby ()->markdown (
311
+ text: $ value ,
324
312
options: $ options
325
- );
326
- return $ this ;
313
+ ));
327
314
}
328
315
329
316
/**
@@ -337,12 +324,10 @@ public function model(): ModelWithContent|null
337
324
/**
338
325
* Converts all line breaks in the field content to `<br>` tags.
339
326
* @since 3.3.0
340
- * @return $this
341
327
*/
342
328
public function nl2br (): static
343
329
{
344
- $ this ->value = nl2br ($ this ->value ?? '' , false );
345
- return $ this ;
330
+ return $ this ->value (fn ($ value ) => nl2br ($ value ?? '' , false ));
346
331
}
347
332
348
333
/**
@@ -360,9 +345,7 @@ public function or(mixed $fallback = null): static
360
345
return $ fallback ;
361
346
}
362
347
363
- $ field = clone $ this ;
364
- $ field ->value = $ fallback ;
365
- return $ field ;
348
+ return $ this ->value ($ fallback );
366
349
}
367
350
368
351
/**
@@ -381,13 +364,13 @@ public function parent(): ModelWithContent|null
381
364
* This method is still experimental! You can use
382
365
* it to solve potential problems with permalinks
383
366
* already, but it might change in the future.
384
- *
385
- * @return $this
386
367
*/
387
368
public function permalinksToUrls (): static
388
369
{
389
- if ($ this ->isNotEmpty () === true ) {
390
- $ dom = new Dom ($ this ->value );
370
+ $ field = clone $ this ;
371
+
372
+ if ($ field ->isNotEmpty () === true ) {
373
+ $ dom = new Dom ($ field ->value );
391
374
$ attributes = ['href ' , 'src ' ];
392
375
$ elements = $ dom ->query ('//*[ ' . implode (' | ' , A::map ($ attributes , fn ($ attribute ) => '@ ' . $ attribute )) . '] ' );
393
376
@@ -405,10 +388,10 @@ public function permalinksToUrls(): static
405
388
}
406
389
}
407
390
408
- $ this ->value = $ dom ->toString ();
391
+ $ field ->value = $ dom ->toString ();
409
392
}
410
393
411
- return $ this ;
394
+ return $ field ;
412
395
}
413
396
414
397
/**
@@ -432,7 +415,6 @@ public function query(
432
415
* It parses any queries found in the field value.
433
416
*
434
417
* @param string|null $fallback Fallback for tokens in the template that cannot be replaced (`null` to keep the original token)
435
- * @return $this
436
418
*/
437
419
public function replace (
438
420
array $ data = [],
@@ -441,20 +423,23 @@ public function replace(
441
423
if ($ parent = $ this ->parent ()) {
442
424
// Never pass `null` as the $template
443
425
// to avoid the fallback to the model ID
444
- $ this ->value = $ parent ->toString (
445
- $ this -> value ?? '' ,
426
+ return $ this ->value ( fn ( $ value ) => $ parent ->toString (
427
+ $ value ?? '' ,
446
428
$ data ,
447
429
$ fallback
448
- );
449
- } else {
450
- $ this ->value = Str::template ($ this ->value , array_replace ([
451
- 'kirby ' => $ app = $ this ->kirby (),
452
- 'site ' => $ app ->site (),
453
- 'page ' => $ app ->page ()
454
- ], $ data ), ['fallback ' => $ fallback ]);
430
+ ));
455
431
}
456
432
457
- return $ this ;
433
+ return $ this ->value (fn ($ value ) => Str::template (
434
+ $ value ,
435
+ [
436
+ 'kirby ' => $ app = $ this ->kirby (),
437
+ 'site ' => $ app ->site (),
438
+ 'page ' => $ app ->page (),
439
+ ...$ data
440
+ ],
441
+ ['fallback ' => $ fallback ]
442
+ ));
458
443
}
459
444
460
445
/**
@@ -463,34 +448,30 @@ public function replace(
463
448
*
464
449
* @param int $length The number of characters in the string
465
450
* @param string $appendix An optional replacement for the missing rest
466
- * @return $this
467
451
*/
468
452
public function short (
469
453
int $ length ,
470
454
string $ appendix = '… '
471
455
): static {
472
- $ this ->value = Str::short ($ this ->value , $ length , $ appendix );
473
- return $ this ;
456
+ return $ this ->value (
457
+ fn ($ value ) => Str::short ($ value , $ length , $ appendix )
458
+ );
474
459
}
475
460
476
461
/**
477
462
* Converts the field content to a slug
478
- * @return $this
479
463
*/
480
464
public function slug (): static
481
465
{
482
- $ this ->value = Str::slug ($ this ->value );
483
- return $ this ;
466
+ return $ this ->value (fn ($ value ) => Str::slug ($ value ));
484
467
}
485
468
486
469
/**
487
470
* Applies SmartyPants to the field
488
- * @return $this
489
471
*/
490
472
public function smartypants (): static
491
473
{
492
- $ this ->value = $ this ->kirby ()->smartypants ($ this ->value );
493
- return $ this ;
474
+ return $ this ->value (fn ($ value ) => $ this ->kirby ()->smartypants ($ value ));
494
475
}
495
476
496
477
/**
@@ -801,12 +782,10 @@ public function toUsers(string $separator = 'yaml'): Users
801
782
802
783
/**
803
784
* Converts the field content to uppercase
804
- * @return $this
805
785
*/
806
786
public function upper (): static
807
787
{
808
- $ this ->value = Str::upper ($ this ->value );
809
- return $ this ;
788
+ return $ this ->value (fn ($ value ) => Str::upper ($ value ));
810
789
}
811
790
812
791
/**
@@ -820,11 +799,12 @@ public function value(string|Closure|null $value = null): mixed
820
799
return $ this ->value ;
821
800
}
822
801
802
+ $ clone = clone $ this ;
803
+
823
804
if ($ value instanceof Closure) {
824
- $ value = $ value ->call ($ this , $ this ->value );
805
+ $ value = $ value ->call ($ this , $ this ->value , $ clone );
825
806
}
826
807
827
- $ clone = clone $ this ;
828
808
$ clone ->value = (string )$ value ;
829
809
830
810
return $ clone ;
@@ -833,12 +813,10 @@ public function value(string|Closure|null $value = null): mixed
833
813
/**
834
814
* Avoids typographical widows in strings by replacing
835
815
* the last space with ` `
836
- * @return $this
837
816
*/
838
817
public function widont (): static
839
818
{
840
- $ this ->value = Str::widont ($ this ->value );
841
- return $ this ;
819
+ return $ this ->value (fn ($ value ) => Str::widont ($ value ));
842
820
}
843
821
844
822
/**
@@ -851,12 +829,10 @@ public function words(): int
851
829
852
830
/**
853
831
* Converts the field content to valid XML
854
- * @return $this
855
832
*/
856
833
public function xml (): static
857
834
{
858
- $ this ->value = Xml::encode ($ this ->value );
859
- return $ this ;
835
+ return $ this ->value (fn ($ value ) => Xml::encode ($ value ));
860
836
}
861
837
862
838
/**
0 commit comments