summaryrefslogtreecommitdiff
path: root/vendor/symfony/string/LazyString.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/string/LazyString.php
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/string/LazyString.php')
-rw-r--r--vendor/symfony/string/LazyString.php145
1 files changed, 145 insertions, 0 deletions
diff --git a/vendor/symfony/string/LazyString.php b/vendor/symfony/string/LazyString.php
new file mode 100644
index 0000000..8f2bbbf
--- /dev/null
+++ b/vendor/symfony/string/LazyString.php
@@ -0,0 +1,145 @@
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
12namespace 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 */
19class 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 __sleep(): array
105 {
106 $this->__toString();
107
108 return ['value'];
109 }
110
111 public function jsonSerialize(): string
112 {
113 return $this->__toString();
114 }
115
116 private function __construct()
117 {
118 }
119
120 private static function getPrettyName(callable $callback): string
121 {
122 if (\is_string($callback)) {
123 return $callback;
124 }
125
126 if (\is_array($callback)) {
127 $class = \is_object($callback[0]) ? get_debug_type($callback[0]) : $callback[0];
128 $method = $callback[1];
129 } elseif ($callback instanceof \Closure) {
130 $r = new \ReflectionFunction($callback);
131
132 if ($r->isAnonymous() || !$class = $r->getClosureCalledClass()) {
133 return $r->name;
134 }
135
136 $class = $class->name;
137 $method = $r->name;
138 } else {
139 $class = get_debug_type($callback);
140 $method = '__invoke';
141 }
142
143 return $class.'::'.$method;
144 }
145}