| 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 \Stringable, \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__, get_debug_type($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 string|int|float|bool|\Stringable $value |
| 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 a stringable object, "%s" given.', __METHOD__, get_debug_type($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) ? method_exists($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 |
/** |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
public function __toString() |
| 97 |
{ |
| 98 |
if (\is_string($this->value)) { |
| 99 |
return $this->value; |
| 100 |
} |
| 101 |
|
| 102 |
try { |
| 103 |
return $this->value = ($this->value)(); |
| 104 |
} catch (\Throwable $e) { |
| 105 |
if (\TypeError::class === \get_class($e) && __FILE__ === $e->getFile()) { |
| 106 |
$type = explode(', ', $e->getMessage()); |
| 107 |
$type = substr(array_pop($type), 0, -\strlen(' returned')); |
| 108 |
$r = new \ReflectionFunction($this->value); |
| 109 |
$callback = $r->getStaticVariables()['callback']; |
| 110 |
|
| 111 |
$e = new \TypeError(sprintf('Return value of %s() passed to %s::fromCallable() must be of the type string, %s returned.', $callback, static::class, $type)); |
| 112 |
} |
| 113 |
|
| 114 |
if (\PHP_VERSION_ID < 70400) { |
| 115 |
// leverage the ErrorHandler component with graceful fallback when it's not available |
| 116 |
return trigger_error($e, E_USER_ERROR); |
| 117 |
} |
| 118 |
|
| 119 |
throw $e; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function __sleep(): array |
| 124 |
{ |
| 125 |
$this->__toString(); |
| 126 |
|
| 127 |
return ['value']; |
| 128 |
} |
| 129 |
|
| 130 |
public function jsonSerialize(): string |
| 131 |
{ |
| 132 |
return $this->__toString(); |
| 133 |
} |
| 134 |
|
| 135 |
private function __construct() |
| 136 |
{ |
| 137 |
} |
| 138 |
|
| 139 |
private static function getPrettyName(callable $callback): string |
| 140 |
{ |
| 141 |
if (\is_string($callback)) { |
| 142 |
return $callback; |
| 143 |
} |
| 144 |
|
| 145 |
if (\is_array($callback)) { |
| 146 |
$class = \is_object($callback[0]) ? get_debug_type($callback[0]) : $callback[0]; |
| 147 |
$method = $callback[1]; |
| 148 |
} elseif ($callback instanceof \Closure) { |
| 149 |
$r = new \ReflectionFunction($callback); |
| 150 |
|
| 151 |
if (false !== strpos($r->name, '{closure}') || !$class = $r->getClosureScopeClass()) { |
| 152 |
return $r->name; |
| 153 |
} |
| 154 |
|
| 155 |
$class = $class->name; |
| 156 |
$method = $r->name; |
| 157 |
} else { |
| 158 |
$class = get_debug_type($callback); |
| 159 |
$method = '__invoke'; |
| 160 |
} |
| 161 |
|
| 162 |
return $class.'::'.$method; |
| 163 |
} |
| 164 |
} |
| 165 |
|