@@ -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,43 @@ 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
+ $ field = clone $ this ;
254
+ $ field ->value = $ field ->kirby ()->kirbytags (
255
+ text: $ field ->value ,
258
256
data: [
259
- 'parent ' => $ this ->parent (),
260
- 'field ' => $ this
257
+ 'parent ' => $ field ->parent (),
258
+ 'field ' => $ field
261
259
]
262
260
);
263
261
264
- return $ this ;
262
+ return $ field ;
265
263
}
266
264
267
265
/**
268
266
* Converts the field content from Markdown/Kirbytext to valid HTML
269
- * @return $this
270
267
*/
271
268
public function kirbytext (array $ options = []): static
272
269
{
273
- $ this ->value = $ this ->kirby ()->kirbytext (
274
- text: $ this ->value ,
270
+ $ field = clone $ this ;
271
+ $ field ->value = $ this ->kirby ()->kirbytext (
272
+ text: $ field ->value ,
275
273
options: [
276
274
...$ options ,
277
- 'parent ' => $ this ->parent (),
278
- 'field ' => $ this
275
+ 'parent ' => $ field ->parent (),
276
+ 'field ' => $ field
279
277
]
280
278
);
281
279
282
- return $ this ;
280
+ return $ field ;
283
281
}
284
282
285
283
/**
286
284
* Converts the field content from inline Markdown/Kirbytext
287
285
* to valid HTML
288
286
* @since 3.1.0
289
- * @return $this
290
287
*/
291
288
public function kirbytextInline (array $ options = []): static
292
289
{
@@ -305,25 +302,21 @@ public function length(): int
305
302
306
303
/**
307
304
* Converts the field content to lowercase
308
- * @return $this
309
305
*/
310
306
public function lower (): static
311
307
{
312
- $ this ->value = Str::lower ($ this ->value );
313
- return $ this ;
308
+ return $ this ->value (fn ($ value ) => Str::lower ($ value ));
314
309
}
315
310
316
311
/**
317
312
* Converts markdown to valid HTML
318
- * @return $this
319
313
*/
320
314
public function markdown (array $ options = []): static
321
315
{
322
- $ this ->value = $ this ->kirby ()->markdown (
323
- text: $ this -> value ,
316
+ return $ this ->value ( fn ( $ value ) => $ this ->kirby ()->markdown (
317
+ text: $ value ,
324
318
options: $ options
325
- );
326
- return $ this ;
319
+ ));
327
320
}
328
321
329
322
/**
@@ -337,12 +330,10 @@ public function model(): ModelWithContent|null
337
330
/**
338
331
* Converts all line breaks in the field content to `<br>` tags.
339
332
* @since 3.3.0
340
- * @return $this
341
333
*/
342
334
public function nl2br (): static
343
335
{
344
- $ this ->value = nl2br ($ this ->value ?? '' , false );
345
- return $ this ;
336
+ return $ this ->value (fn ($ value ) => nl2br ($ value ?? '' , false ));
346
337
}
347
338
348
339
/**
@@ -360,9 +351,7 @@ public function or(mixed $fallback = null): static
360
351
return $ fallback ;
361
352
}
362
353
363
- $ field = clone $ this ;
364
- $ field ->value = $ fallback ;
365
- return $ field ;
354
+ return $ this ->value ($ fallback );
366
355
}
367
356
368
357
/**
@@ -381,13 +370,13 @@ public function parent(): ModelWithContent|null
381
370
* This method is still experimental! You can use
382
371
* it to solve potential problems with permalinks
383
372
* already, but it might change in the future.
384
- *
385
- * @return $this
386
373
*/
387
374
public function permalinksToUrls (): static
388
375
{
389
- if ($ this ->isNotEmpty () === true ) {
390
- $ dom = new Dom ($ this ->value );
376
+ $ field = clone $ this ;
377
+
378
+ if ($ field ->isNotEmpty () === true ) {
379
+ $ dom = new Dom ($ field ->value );
391
380
$ attributes = ['href ' , 'src ' ];
392
381
$ elements = $ dom ->query ('//*[ ' . implode (' | ' , A::map ($ attributes , fn ($ attribute ) => '@ ' . $ attribute )) . '] ' );
393
382
@@ -405,10 +394,10 @@ public function permalinksToUrls(): static
405
394
}
406
395
}
407
396
408
- $ this ->value = $ dom ->toString ();
397
+ $ field ->value = $ dom ->toString ();
409
398
}
410
399
411
- return $ this ;
400
+ return $ field ;
412
401
}
413
402
414
403
/**
@@ -432,7 +421,6 @@ public function query(
432
421
* It parses any queries found in the field value.
433
422
*
434
423
* @param string|null $fallback Fallback for tokens in the template that cannot be replaced (`null` to keep the original token)
435
- * @return $this
436
424
*/
437
425
public function replace (
438
426
array $ data = [],
@@ -441,20 +429,23 @@ public function replace(
441
429
if ($ parent = $ this ->parent ()) {
442
430
// Never pass `null` as the $template
443
431
// to avoid the fallback to the model ID
444
- $ this ->value = $ parent ->toString (
445
- $ this -> value ?? '' ,
432
+ return $ this ->value ( fn ( $ value ) => $ parent ->toString (
433
+ $ value ?? '' ,
446
434
$ data ,
447
435
$ 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 ]);
436
+ ));
455
437
}
456
438
457
- return $ this ;
439
+ return $ this ->value (fn ($ value ) => Str::template (
440
+ $ value ,
441
+ [
442
+ 'kirby ' => $ app = $ this ->kirby (),
443
+ 'site ' => $ app ->site (),
444
+ 'page ' => $ app ->page (),
445
+ ...$ data
446
+ ],
447
+ ['fallback ' => $ fallback ]
448
+ ));
458
449
}
459
450
460
451
/**
@@ -463,34 +454,30 @@ public function replace(
463
454
*
464
455
* @param int $length The number of characters in the string
465
456
* @param string $appendix An optional replacement for the missing rest
466
- * @return $this
467
457
*/
468
458
public function short (
469
459
int $ length ,
470
460
string $ appendix = '… '
471
461
): static {
472
- $ this ->value = Str::short ($ this ->value , $ length , $ appendix );
473
- return $ this ;
462
+ return $ this ->value (
463
+ fn ($ value ) => Str::short ($ value , $ length , $ appendix )
464
+ );
474
465
}
475
466
476
467
/**
477
468
* Converts the field content to a slug
478
- * @return $this
479
469
*/
480
470
public function slug (): static
481
471
{
482
- $ this ->value = Str::slug ($ this ->value );
483
- return $ this ;
472
+ return $ this ->value (fn ($ value ) => Str::slug ($ value ));
484
473
}
485
474
486
475
/**
487
476
* Applies SmartyPants to the field
488
- * @return $this
489
477
*/
490
478
public function smartypants (): static
491
479
{
492
- $ this ->value = $ this ->kirby ()->smartypants ($ this ->value );
493
- return $ this ;
480
+ return $ this ->value (fn ($ value ) => $ this ->kirby ()->smartypants ($ value ));
494
481
}
495
482
496
483
/**
@@ -801,12 +788,10 @@ public function toUsers(string $separator = 'yaml'): Users
801
788
802
789
/**
803
790
* Converts the field content to uppercase
804
- * @return $this
805
791
*/
806
792
public function upper (): static
807
793
{
808
- $ this ->value = Str::upper ($ this ->value );
809
- return $ this ;
794
+ return $ this ->value (fn ($ value ) => Str::upper ($ value ));
810
795
}
811
796
812
797
/**
@@ -824,7 +809,7 @@ public function value(string|Closure|null $value = null): mixed
824
809
$ value = $ value ->call ($ this , $ this ->value );
825
810
}
826
811
827
- $ clone = clone $ this ;
812
+ $ clone = clone $ this ;
828
813
$ clone ->value = (string )$ value ;
829
814
830
815
return $ clone ;
@@ -833,12 +818,10 @@ public function value(string|Closure|null $value = null): mixed
833
818
/**
834
819
* Avoids typographical widows in strings by replacing
835
820
* the last space with ` `
836
- * @return $this
837
821
*/
838
822
public function widont (): static
839
823
{
840
- $ this ->value = Str::widont ($ this ->value );
841
- return $ this ;
824
+ return $ this ->value (fn ($ value ) => Str::widont ($ value ));
842
825
}
843
826
844
827
/**
@@ -851,12 +834,10 @@ public function words(): int
851
834
852
835
/**
853
836
* Converts the field content to valid XML
854
- * @return $this
855
837
*/
856
838
public function xml (): static
857
839
{
858
- $ this ->value = Xml::encode ($ this ->value );
859
- return $ this ;
840
+ return $ this ->value (fn ($ value ) => Xml::encode ($ value ));
860
841
}
861
842
862
843
/**
0 commit comments