Exception
1 year ago
Inflector
6 months ago
Resources
6 months ago
Slugger
1 year ago
AbstractString.php
1 year ago
AbstractUnicodeString.php
6 months ago
ByteString.php
1 year ago
CodePointString.php
1 year ago
LazyString.php
1 year ago
UnicodeString.php
1 year ago
index.php
1 year ago
LazyString.php
103 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\String; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class LazyString implements \Stringable, \JsonSerializable |
| 5 | { |
| 6 | private $value; |
| 7 | public static function fromCallable($callback, ...$arguments) : self |
| 8 | { |
| 9 | if (!\is_callable($callback) && !(\is_array($callback) && isset($callback[0]) && $callback[0] instanceof \Closure && 2 >= \count($callback))) { |
| 10 | 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))); |
| 11 | } |
| 12 | $lazyString = new static(); |
| 13 | $lazyString->value = static function () use(&$callback, &$arguments, &$value) : string { |
| 14 | if (null !== $arguments) { |
| 15 | if (!\is_callable($callback)) { |
| 16 | $callback[0] = $callback[0](); |
| 17 | $callback[1] = $callback[1] ?? '__invoke'; |
| 18 | } |
| 19 | $value = $callback(...$arguments); |
| 20 | $callback = self::getPrettyName($callback); |
| 21 | $arguments = null; |
| 22 | } |
| 23 | return $value ?? ''; |
| 24 | }; |
| 25 | return $lazyString; |
| 26 | } |
| 27 | public static function fromStringable($value) : self |
| 28 | { |
| 29 | if (!self::isStringable($value)) { |
| 30 | throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a scalar or a stringable object, "%s" given.', __METHOD__, \get_debug_type($value))); |
| 31 | } |
| 32 | if (\is_object($value)) { |
| 33 | return static::fromCallable([$value, '__toString']); |
| 34 | } |
| 35 | $lazyString = new static(); |
| 36 | $lazyString->value = (string) $value; |
| 37 | return $lazyString; |
| 38 | } |
| 39 | public static final function isStringable($value) : bool |
| 40 | { |
| 41 | return \is_string($value) || $value instanceof self || (\is_object($value) ? \method_exists($value, '__toString') : \is_scalar($value)); |
| 42 | } |
| 43 | public static final function resolve($value) : string |
| 44 | { |
| 45 | return $value; |
| 46 | } |
| 47 | public function __toString() |
| 48 | { |
| 49 | if (\is_string($this->value)) { |
| 50 | return $this->value; |
| 51 | } |
| 52 | try { |
| 53 | return $this->value = ($this->value)(); |
| 54 | } catch (\Throwable $e) { |
| 55 | if (\TypeError::class === \get_class($e) && __FILE__ === $e->getFile()) { |
| 56 | $type = \explode(', ', $e->getMessage()); |
| 57 | $type = \substr(\array_pop($type), 0, -\strlen(' returned')); |
| 58 | $r = new \ReflectionFunction($this->value); |
| 59 | $callback = $r->getStaticVariables()['callback']; |
| 60 | $e = new \TypeError(\sprintf('Return value of %s() passed to %s::fromCallable() must be of the type string, %s returned.', $callback, static::class, $type)); |
| 61 | } |
| 62 | if (\PHP_VERSION_ID < 70400) { |
| 63 | // leverage the ErrorHandler component with graceful fallback when it's not available |
| 64 | return \trigger_error($e, \E_USER_ERROR); |
| 65 | } |
| 66 | throw $e; |
| 67 | } |
| 68 | } |
| 69 | public function __sleep() : array |
| 70 | { |
| 71 | $this->__toString(); |
| 72 | return ['value']; |
| 73 | } |
| 74 | public function jsonSerialize() : string |
| 75 | { |
| 76 | return $this->__toString(); |
| 77 | } |
| 78 | private function __construct() |
| 79 | { |
| 80 | } |
| 81 | private static function getPrettyName(callable $callback) : string |
| 82 | { |
| 83 | if (\is_string($callback)) { |
| 84 | return $callback; |
| 85 | } |
| 86 | if (\is_array($callback)) { |
| 87 | $class = \is_object($callback[0]) ? \get_debug_type($callback[0]) : $callback[0]; |
| 88 | $method = $callback[1]; |
| 89 | } elseif ($callback instanceof \Closure) { |
| 90 | $r = new \ReflectionFunction($callback); |
| 91 | if (\str_contains($r->name, '{closure') || !($class = \PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass())) { |
| 92 | return $r->name; |
| 93 | } |
| 94 | $class = $class->name; |
| 95 | $method = $r->name; |
| 96 | } else { |
| 97 | $class = \get_debug_type($callback); |
| 98 | $method = '__invoke'; |
| 99 | } |
| 100 | return $class . '::' . $method; |
| 101 | } |
| 102 | } |
| 103 |