|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\String; |
| 13 | + |
| 14 | +/** |
| 15 | + * A string whose value is computed lazily by a callback. |
| 16 | + * |
| 17 | + * @author Nicolas Grekas <p@tchwork.com> |
| 18 | + */ |
| 19 | +class LazyString implements \JsonSerializable |
| 20 | +{ |
| 21 | + private $value; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param callable|array $callback A callable or a [Closure, method] lazy-callable |
| 25 | + * |
| 26 | + * @return static |
| 27 | + */ |
| 28 | + public static function fromCallable($callback, ...$arguments): self |
| 29 | + { |
| 30 | + if (!\is_callable($callback) && !(\is_array($callback) && isset($callback[0]) && $callback[0] instanceof \Closure && 2 >= \count($callback))) { |
| 31 | + throw new \TypeError(sprintf('Argument 1 passed to %s() must be a callable or a [Closure, method] lazy-callable, %s given.', __METHOD__, \gettype($callback))); |
| 32 | + } |
| 33 | + |
| 34 | + $lazyString = new static(); |
| 35 | + $lazyString->value = static function () use (&$callback, &$arguments, &$value): string { |
| 36 | + if (null !== $arguments) { |
| 37 | + if (!\is_callable($callback)) { |
| 38 | + $callback[0] = $callback[0](); |
| 39 | + $callback[1] = $callback[1] ?? '__invoke'; |
| 40 | + } |
| 41 | + $value = $callback(...$arguments); |
| 42 | + $callback = self::getPrettyName($callback); |
| 43 | + $arguments = null; |
| 44 | + } |
| 45 | + |
| 46 | + return $value ?? ''; |
| 47 | + }; |
| 48 | + |
| 49 | + return $lazyString; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param object|string|int|float|bool $value A scalar or an object that implements the __toString() magic method |
| 54 | + * |
| 55 | + * @return static |
| 56 | + */ |
| 57 | + public static function fromStringable($value): self |
| 58 | + { |
| 59 | + if (!self::isStringable($value)) { |
| 60 | + throw new \TypeError(sprintf('Argument 1 passed to %s() must be a scalar or an object that implements the __toString() magic method, %s given.', __METHOD__, \is_object($value) ? \get_class($value) : \gettype($value))); |
| 61 | + } |
| 62 | + |
| 63 | + if (\is_object($value)) { |
| 64 | + return static::fromCallable([$value, '__toString']); |
| 65 | + } |
| 66 | + |
| 67 | + $lazyString = new static(); |
| 68 | + $lazyString->value = (string) $value; |
| 69 | + |
| 70 | + return $lazyString; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Tells whether the provided value can be cast to string. |
| 75 | + */ |
| 76 | + final public static function isStringable($value): bool |
| 77 | + { |
| 78 | + return \is_string($value) || $value instanceof self || (\is_object($value) ? \is_callable([$value, '__toString']) : is_scalar($value)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Casts scalars and stringable objects to strings. |
| 83 | + * |
| 84 | + * @param object|string|int|float|bool $value |
| 85 | + * |
| 86 | + * @throws \TypeError When the provided value is not stringable |
| 87 | + */ |
| 88 | + final public static function resolve($value): string |
| 89 | + { |
| 90 | + return $value; |
| 91 | + } |
| 92 | + |
| 93 | + public function __toString() |
| 94 | + { |
| 95 | + if (\is_string($this->value)) { |
| 96 | + return $this->value; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + return $this->value = ($this->value)(); |
| 101 | + } catch (\Throwable $e) { |
| 102 | + if (\TypeError::class === \get_class($e) && __FILE__ === $e->getFile()) { |
| 103 | + $type = explode(', ', $e->getMessage()); |
| 104 | + $type = substr(array_pop($type), 0, -\strlen(' returned')); |
| 105 | + $r = new \ReflectionFunction($this->value); |
| 106 | + $callback = $r->getStaticVariables()['callback']; |
| 107 | + |
| 108 | + $e = new \TypeError(sprintf('Return value of %s() passed to %s::fromCallable() must be of the type string, %s returned.', $callback, static::class, $type)); |
| 109 | + } |
| 110 | + |
| 111 | + if (\PHP_VERSION_ID < 70400) { |
| 112 | + // leverage the ErrorHandler component with graceful fallback when it's not available |
| 113 | + return trigger_error($e, E_USER_ERROR); |
| 114 | + } |
| 115 | + |
| 116 | + throw $e; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public function __sleep(): array |
| 121 | + { |
| 122 | + $this->__toString(); |
| 123 | + |
| 124 | + return ['value']; |
| 125 | + } |
| 126 | + |
| 127 | + public function jsonSerialize(): string |
| 128 | + { |
| 129 | + return $this->__toString(); |
| 130 | + } |
| 131 | + |
| 132 | + private function __construct() |
| 133 | + { |
| 134 | + } |
| 135 | + |
| 136 | + private static function getPrettyName(callable $callback): string |
| 137 | + { |
| 138 | + if (\is_string($callback)) { |
| 139 | + return $callback; |
| 140 | + } |
| 141 | + |
| 142 | + if (\is_array($callback)) { |
| 143 | + $class = \is_object($callback[0]) ? \get_class($callback[0]) : $callback[0]; |
| 144 | + $method = $callback[1]; |
| 145 | + } elseif ($callback instanceof \Closure) { |
| 146 | + $r = new \ReflectionFunction($callback); |
| 147 | + |
| 148 | + if (false !== strpos($r->name, '{closure}') || !$class = $r->getClosureScopeClass()) { |
| 149 | + return $r->name; |
| 150 | + } |
| 151 | + |
| 152 | + $class = $class->name; |
| 153 | + $method = $r->name; |
| 154 | + } else { |
| 155 | + $class = \get_class($callback); |
| 156 | + $method = '__invoke'; |
| 157 | + } |
| 158 | + |
| 159 | + if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) { |
| 160 | + $class = (get_parent_class($class) ?: key(class_implements($class))).'@anonymous'; |
| 161 | + } |
| 162 | + |
| 163 | + return $class.'::'.$method; |
| 164 | + } |
| 165 | +} |
0 commit comments