Skip to content

Commit 6f3d1fb

Browse files
committed
Ensuring altering field methods clone object
1 parent 65708d8 commit 6f3d1fb

File tree

1 file changed

+57
-76
lines changed

1 file changed

+57
-76
lines changed

src/Content/Field.php

Lines changed: 57 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,27 @@ public function callback(Closure $callback): mixed
121121
* templates without the risk of XSS attacks
122122
*
123123
* @param string $context Location of output (`html`, `attr`, `js`, `css`, `url` or `xml`)
124-
* @return $this
125124
*/
126125
public function escape(string $context = 'html'): static
127126
{
128-
$this->value = Str::esc($this->value ?? '', $context);
129-
return $this;
127+
return $this->value(fn ($value) => Str::esc($value ?? '', $context));
130128
}
131129

132130
/**
133131
* Creates an excerpt of the field value without html
134132
* or any other formatting.
135-
* @return $this
136133
*/
137134
public function excerpt(
138135
int $chars = 0,
139136
bool $strip = true,
140137
string $rep = ''
141138
): 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+
));
145145
}
146146

147147
/**
@@ -154,20 +154,17 @@ public function exists(): bool
154154

155155
/**
156156
* Converts the field content to valid HTML
157-
* @return $this
158157
*/
159158
public function html(): static
160159
{
161-
$this->value = Html::encode($this->value);
162-
return $this;
160+
return $this->value(fn ($value) => Html::encode($value));
163161
}
164162

165163
/**
166164
* Strips all block-level HTML elements from the field value,
167165
* it can be safely placed inside of other inline elements
168166
* without the risk of breaking the HTML structure.
169167
* @since 3.3.0
170-
* @return $this
171168
*/
172169
public function inline(): static
173170
{
@@ -176,8 +173,9 @@ public function inline(): static
176173
// Obsolete elements, script tags, image maps and form elements have
177174
// been excluded for safety reasons and as they are most likely not
178175
// 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+
);
181179
}
182180

183181
/**
@@ -249,44 +247,43 @@ public function kirby(): App
249247

250248
/**
251249
* Parses all KirbyTags without also parsing Markdown
252-
* @return $this
253250
*/
254251
public function kirbytags(): static
255252
{
256-
$this->value = $this->kirby()->kirbytags(
257-
text: $this->value,
253+
$field = clone $this;
254+
$field->value = $field->kirby()->kirbytags(
255+
text: $field->value,
258256
data: [
259-
'parent' => $this->parent(),
260-
'field' => $this
257+
'parent' => $field->parent(),
258+
'field' => $field
261259
]
262260
);
263261

264-
return $this;
262+
return $field;
265263
}
266264

267265
/**
268266
* Converts the field content from Markdown/Kirbytext to valid HTML
269-
* @return $this
270267
*/
271268
public function kirbytext(array $options = []): static
272269
{
273-
$this->value = $this->kirby()->kirbytext(
274-
text: $this->value,
270+
$field = clone $this;
271+
$field->value = $this->kirby()->kirbytext(
272+
text: $field->value,
275273
options: [
276274
...$options,
277-
'parent' => $this->parent(),
278-
'field' => $this
275+
'parent' => $field->parent(),
276+
'field' => $field
279277
]
280278
);
281279

282-
return $this;
280+
return $field;
283281
}
284282

285283
/**
286284
* Converts the field content from inline Markdown/Kirbytext
287285
* to valid HTML
288286
* @since 3.1.0
289-
* @return $this
290287
*/
291288
public function kirbytextInline(array $options = []): static
292289
{
@@ -305,25 +302,21 @@ public function length(): int
305302

306303
/**
307304
* Converts the field content to lowercase
308-
* @return $this
309305
*/
310306
public function lower(): static
311307
{
312-
$this->value = Str::lower($this->value);
313-
return $this;
308+
return $this->value(fn ($value) => Str::lower($value));
314309
}
315310

316311
/**
317312
* Converts markdown to valid HTML
318-
* @return $this
319313
*/
320314
public function markdown(array $options = []): static
321315
{
322-
$this->value = $this->kirby()->markdown(
323-
text: $this->value,
316+
return $this->value(fn ($value) => $this->kirby()->markdown(
317+
text: $value,
324318
options: $options
325-
);
326-
return $this;
319+
));
327320
}
328321

329322
/**
@@ -337,12 +330,10 @@ public function model(): ModelWithContent|null
337330
/**
338331
* Converts all line breaks in the field content to `<br>` tags.
339332
* @since 3.3.0
340-
* @return $this
341333
*/
342334
public function nl2br(): static
343335
{
344-
$this->value = nl2br($this->value ?? '', false);
345-
return $this;
336+
return $this->value(fn ($value) => nl2br($value ?? '', false));
346337
}
347338

348339
/**
@@ -360,9 +351,7 @@ public function or(mixed $fallback = null): static
360351
return $fallback;
361352
}
362353

363-
$field = clone $this;
364-
$field->value = $fallback;
365-
return $field;
354+
return $this->value($fallback);
366355
}
367356

368357
/**
@@ -381,13 +370,13 @@ public function parent(): ModelWithContent|null
381370
* This method is still experimental! You can use
382371
* it to solve potential problems with permalinks
383372
* already, but it might change in the future.
384-
*
385-
* @return $this
386373
*/
387374
public function permalinksToUrls(): static
388375
{
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);
391380
$attributes = ['href', 'src'];
392381
$elements = $dom->query('//*[' . implode(' | ', A::map($attributes, fn ($attribute) => '@' . $attribute)) . ']');
393382

@@ -405,10 +394,10 @@ public function permalinksToUrls(): static
405394
}
406395
}
407396

408-
$this->value = $dom->toString();
397+
$field->value = $dom->toString();
409398
}
410399

411-
return $this;
400+
return $field;
412401
}
413402

414403
/**
@@ -432,7 +421,6 @@ public function query(
432421
* It parses any queries found in the field value.
433422
*
434423
* @param string|null $fallback Fallback for tokens in the template that cannot be replaced (`null` to keep the original token)
435-
* @return $this
436424
*/
437425
public function replace(
438426
array $data = [],
@@ -441,20 +429,23 @@ public function replace(
441429
if ($parent = $this->parent()) {
442430
// Never pass `null` as the $template
443431
// 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 ?? '',
446434
$data,
447435
$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+
));
455437
}
456438

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+
));
458449
}
459450

460451
/**
@@ -463,34 +454,30 @@ public function replace(
463454
*
464455
* @param int $length The number of characters in the string
465456
* @param string $appendix An optional replacement for the missing rest
466-
* @return $this
467457
*/
468458
public function short(
469459
int $length,
470460
string $appendix = ''
471461
): 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+
);
474465
}
475466

476467
/**
477468
* Converts the field content to a slug
478-
* @return $this
479469
*/
480470
public function slug(): static
481471
{
482-
$this->value = Str::slug($this->value);
483-
return $this;
472+
return $this->value(fn ($value) => Str::slug($value));
484473
}
485474

486475
/**
487476
* Applies SmartyPants to the field
488-
* @return $this
489477
*/
490478
public function smartypants(): static
491479
{
492-
$this->value = $this->kirby()->smartypants($this->value);
493-
return $this;
480+
return $this->value(fn ($value) => $this->kirby()->smartypants($value));
494481
}
495482

496483
/**
@@ -801,12 +788,10 @@ public function toUsers(string $separator = 'yaml'): Users
801788

802789
/**
803790
* Converts the field content to uppercase
804-
* @return $this
805791
*/
806792
public function upper(): static
807793
{
808-
$this->value = Str::upper($this->value);
809-
return $this;
794+
return $this->value(fn ($value) => Str::upper($value));
810795
}
811796

812797
/**
@@ -824,7 +809,7 @@ public function value(string|Closure|null $value = null): mixed
824809
$value = $value->call($this, $this->value);
825810
}
826811

827-
$clone = clone $this;
812+
$clone = clone $this;
828813
$clone->value = (string)$value;
829814

830815
return $clone;
@@ -833,12 +818,10 @@ public function value(string|Closure|null $value = null): mixed
833818
/**
834819
* Avoids typographical widows in strings by replacing
835820
* the last space with `&nbsp;`
836-
* @return $this
837821
*/
838822
public function widont(): static
839823
{
840-
$this->value = Str::widont($this->value);
841-
return $this;
824+
return $this->value(fn ($value) => Str::widont($value));
842825
}
843826

844827
/**
@@ -851,12 +834,10 @@ public function words(): int
851834

852835
/**
853836
* Converts the field content to valid XML
854-
* @return $this
855837
*/
856838
public function xml(): static
857839
{
858-
$this->value = Xml::encode($this->value);
859-
return $this;
840+
return $this->value(fn ($value) => Xml::encode($value));
860841
}
861842

862843
/**

0 commit comments

Comments
 (0)