| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 18 |
*/ |
| 19 |
class LazyString implements \Stringable, \JsonSerializable |
| 20 |
{ |
| 21 |
private \Closure|string $value; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param callable|array $callback A callable or a [Closure, method] lazy-callable |
| 25 |
*/ |
| 26 |
public static function fromCallable(callable|array $callback, mixed ...$arguments): static |
| 27 |
{ |
| 28 |
if (\is_array($callback) && !\is_callable($callback) && !(($callback[0] ?? null) instanceof \Closure || 2 < \count($callback))) { |
| 29 |
throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a callable or a [Closure, method] lazy-callable, "%s" given.', __METHOD__, '['.implode(', ', array_map('get_debug_type', $callback)).']')); |
| 30 |
} |
| 31 |
|
| 32 |
$lazyString = new static(); |
| 33 |
$lazyString->value = static function () use (&$callback, &$arguments): string { |
| 34 |
static $value; |
| 35 |
|
| 36 |
if (null !== $arguments) { |
| 37 |
if (!\is_callable($callback)) { |
| 38 |
$callback[0] = $callback[0](); |
| 39 |
$callback[1] ??= '__invoke'; |
| 40 |
} |
| 41 |
$value = $callback(...$arguments); |
| 42 |
$callback = !\is_scalar($value) && !$value instanceof \Stringable ? self::getPrettyName($callback) : 'callable'; |
| 43 |
$arguments = null; |
| 44 |
} |
| 45 |
|
| 46 |
return $value ?? ''; |
| 47 |
}; |
| 48 |
|
| 49 |
return $lazyString; |
| 50 |
} |
| 51 |
|
| 52 |
public static function fromStringable(string|int|float|bool|\Stringable $value): static |
| 53 |
{ |
| 54 |
if (\is_object($value)) { |
| 55 |
return static::fromCallable($value->__toString(...)); |
| 56 |
} |
| 57 |
|
| 58 |
$lazyString = new static(); |
| 59 |
$lazyString->value = (string) $value; |
| 60 |
|
| 61 |
return $lazyString; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Tells whether the provided value can be cast to string. |
| 66 |
*/ |
| 67 |
final public static function isStringable(mixed $value): bool |
| 68 |
{ |
| 69 |
return \is_string($value) || $value instanceof \Stringable || \is_scalar($value); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Casts scalars and stringable objects to strings. |
| 74 |
* |
| 75 |
* @throws \TypeError When the provided value is not stringable |
| 76 |
*/ |
| 77 |
final public static function resolve(\Stringable|string|int|float|bool $value): string |
| 78 |
{ |
| 79 |
return $value; |
| 80 |
} |
| 81 |
|
| 82 |
public function __toString(): string |
| 83 |
{ |
| 84 |
if (\is_string($this->value)) { |
| 85 |
return $this->value; |
| 86 |
} |
| 87 |
|
| 88 |
try { |
| 89 |
return $this->value = ($this->value)(); |
| 90 |
} catch (\Throwable $e) { |
| 91 |
if (\TypeError::class === $e::class && __FILE__ === $e->getFile()) { |
| 92 |
$type = explode(', ', $e->getMessage()); |
| 93 |
$type = substr(array_pop($type), 0, -\strlen(' returned')); |
| 94 |
$r = new \ReflectionFunction($this->value); |
| 95 |
$callback = $r->getStaticVariables()['callback']; |
| 96 |
|
| 97 |
$e = new \TypeError(\sprintf('Return value of %s() passed to %s::fromCallable() must be of the type string, %s returned.', $callback, static::class, $type)); |
| 98 |
} |
| 99 |
|
| 100 |
throw $e; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function __serialize(): array |
| 105 |
{ |
| 106 |
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) { |
| 107 |
$this->__toString(); |
| 108 |
|
| 109 |
return ['value' => $this->value]; |
| 110 |
} |
| 111 |
|
| 112 |
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this)); |
| 113 |
|
| 114 |
$data = []; |
| 115 |
foreach ($this->__sleep() as $key) { |
| 116 |
try { |
| 117 |
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) { |
| 118 |
$data[$key] = $r->getValue($this); |
| 119 |
} |
| 120 |
} catch (\ReflectionException) { |
| 121 |
$data[$key] = $this->$key; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $data; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0 |
| 130 |
*/ |
| 131 |
public function __sleep(): array |
| 132 |
{ |
| 133 |
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this)); |
| 134 |
|
| 135 |
$this->__toString(); |
| 136 |
|
| 137 |
return ['value']; |
| 138 |
} |
| 139 |
|
| 140 |
public function jsonSerialize(): string |
| 141 |
{ |
| 142 |
return $this->__toString(); |
| 143 |
} |
| 144 |
|
| 145 |
private function __construct() |
| 146 |
{ |
| 147 |
} |
| 148 |
|
| 149 |
private static function getPrettyName(callable $callback): string |
| 150 |
{ |
| 151 |
if (\is_string($callback)) { |
| 152 |
return $callback; |
| 153 |
} |
| 154 |
|
| 155 |
if (\is_array($callback)) { |
| 156 |
$class = \is_object($callback[0]) ? get_debug_type($callback[0]) : $callback[0]; |
| 157 |
$method = $callback[1]; |
| 158 |
} elseif ($callback instanceof \Closure) { |
| 159 |
$r = new \ReflectionFunction($callback); |
| 160 |
|
| 161 |
if ($r->isAnonymous() || !$class = $r->getClosureCalledClass()) { |
| 162 |
return $r->name; |
| 163 |
} |
| 164 |
|
| 165 |
$class = $class->name; |
| 166 |
$method = $r->name; |
| 167 |
} else { |
| 168 |
$class = get_debug_type($callback); |
| 169 |
$method = '__invoke'; |
| 170 |
} |
| 171 |
|
| 172 |
return $class.'::'.$method; |
| 173 |
} |
| 174 |
} |