@@ -481,6 +481,55 @@ letter A with ring above"*) or a sequence of two code points (``U+0061`` =
481481 u('å')->normalize(UnicodeString::NFD);
482482 u('å')->normalize(UnicodeString::NFKD);
483483
484+ Lazily-loaded Strings
485+ ---------------------
486+
487+ Additionally to other types of strings presented above, the
488+ :class: `Symfony\\ Component\\ String\\ LazyString ` allows to store
489+ a string whose value is only generated when you need it. This is
490+ useful when the string needs a heavy computation to determine its value,
491+ like a hash for example. A lazy string can be declared like this::
492+
493+ use Symfony\Component\String\LazyString;
494+
495+ $lazyString = LazyString::fromCallable(function () {
496+ // Compute the string value...
497+ $value = ...;
498+
499+ // Then return the final value
500+ return $value;
501+ });
502+
503+ The callback will only be executed when the value of the lazy string is
504+ requested in the program execution.
505+
506+ A lazy string can also be created from a ``Stringable `` object::
507+
508+ class Hash implements \Stringable
509+ {
510+ public function __toString(): string
511+ {
512+ return $this->computeHash();
513+ }
514+
515+ private function computeHash(): string
516+ {
517+ // Compute hash value with potentially heavy processing
518+ $hash = ...;
519+
520+ return $hash;
521+ }
522+ }
523+
524+ // Then create a lazy string from this hash, which will trigger
525+ // hash computation only if it's needed
526+ $lazyHash = LazyString::fromStringable(new Hash());
527+
528+ .. versionadded :: 5.1
529+
530+ The :class: `Symfony\\ Component\\ String\\ LazyString ` class was introduced
531+ in Symfony 5.1.
532+
484533Slugger
485534-------
486535
0 commit comments