PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.10
WindPress – Tailwind CSS integration for WordPress v3.0.10
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / vendor / symfony / string / LazyString.php

LazyString.php in WindPress – Tailwind CSS integration for WordPress 3.0.10, at vendor/symfony/string/LazyString.php

140 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 namespace WindPressDeps\Symfony\Component\String;
12
13 /**
14 * A string whose value is computed lazily by a callback.
15 *
16 * @author Nicolas Grekas <p@tchwork.com>
17 */
18 class LazyString implements \Stringable, \JsonSerializable
19 {
20 private $value;
21 /**
22 * @param callable|array $callback A callable or a [Closure, method] lazy-callable
23 *
24 * @return static
25 */
26 public static function fromCallable($callback, ...$arguments) : self
27 {
28 if (!\is_callable($callback) && !(\is_array($callback) && isset($callback[0]) && $callback[0] 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__, \get_debug_type($callback)));
30 }
31 $lazyString = new static();
32 $lazyString->value = static function () use(&$callback, &$arguments, &$value) : string {
33 if (null !== $arguments) {
34 if (!\is_callable($callback)) {
35 $callback[0] = $callback[0]();
36 $callback[1] = $callback[1] ?? '__invoke';
37 }
38 $value = $callback(...$arguments);
39 $callback = self::getPrettyName($callback);
40 $arguments = null;
41 }
42 return $value ?? '';
43 };
44 return $lazyString;
45 }
46 /**
47 * @param string|int|float|bool|\Stringable $value
48 *
49 * @return static
50 */
51 public static function fromStringable($value) : self
52 {
53 if (!self::isStringable($value)) {
54 throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a scalar or a stringable object, "%s" given.', __METHOD__, \get_debug_type($value)));
55 }
56 if (\is_object($value)) {
57 return static::fromCallable([$value, '__toString']);
58 }
59 $lazyString = new static();
60 $lazyString->value = (string) $value;
61 return $lazyString;
62 }
63 /**
64 * Tells whether the provided value can be cast to string.
65 */
66 public static final function isStringable($value) : bool
67 {
68 return \is_string($value) || $value instanceof self || (\is_object($value) ? \method_exists($value, '__toString') : \is_scalar($value));
69 }
70 /**
71 * Casts scalars and stringable objects to strings.
72 *
73 * @param object|string|int|float|bool $value
74 *
75 * @throws \TypeError When the provided value is not stringable
76 */
77 public static final function resolve($value) : string
78 {
79 return $value;
80 }
81 /**
82 * @return string
83 */
84 public function __toString()
85 {
86 if (\is_string($this->value)) {
87 return $this->value;
88 }
89 try {
90 return $this->value = ($this->value)();
91 } catch (\Throwable $e) {
92 if (\TypeError::class === \get_class($e) && __FILE__ === $e->getFile()) {
93 $type = \explode(', ', $e->getMessage());
94 $type = \substr(\array_pop($type), 0, -\strlen(' returned'));
95 $r = new \ReflectionFunction($this->value);
96 $callback = $r->getStaticVariables()['callback'];
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 if (\PHP_VERSION_ID < 70400) {
100 // leverage the ErrorHandler component with graceful fallback when it's not available
101 return \trigger_error($e, \E_USER_ERROR);
102 }
103 throw $e;
104 }
105 }
106 public function __sleep() : array
107 {
108 $this->__toString();
109 return ['value'];
110 }
111 public function jsonSerialize() : string
112 {
113 return $this->__toString();
114 }
115 private function __construct()
116 {
117 }
118 private static function getPrettyName(callable $callback) : string
119 {
120 if (\is_string($callback)) {
121 return $callback;
122 }
123 if (\is_array($callback)) {
124 $class = \is_object($callback[0]) ? \get_debug_type($callback[0]) : $callback[0];
125 $method = $callback[1];
126 } elseif ($callback instanceof \Closure) {
127 $r = new \ReflectionFunction($callback);
128 if (\str_contains($r->name, '{closure') || !($class = \PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass())) {
129 return $r->name;
130 }
131 $class = $class->name;
132 $method = $r->name;
133 } else {
134 $class = \get_debug_type($callback);
135 $method = '__invoke';
136 }
137 return $class . '::' . $method;
138 }
139 }
140