Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 \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, &$value): string {
34
            if (null !== $arguments) {
35
                if (!\is_callable($callback)) {
36
                    $callback[0] = $callback[0]();
37
                    $callback[1] ??= '__invoke';
38
                }
39
                $value = $callback(...$arguments);
40
                $callback = self::getPrettyName($callback);
41
                $arguments = null;
42
            }
43
 
44
            return $value ?? '';
45
        };
46
 
47
        return $lazyString;
48
    }
49
 
50
    public static function fromStringable(string|int|float|bool|\Stringable $value): static
51
    {
52
        if (\is_object($value)) {
53
            return static::fromCallable($value->__toString(...));
54
        }
55
 
56
        $lazyString = new static();
57
        $lazyString->value = (string) $value;
58
 
59
        return $lazyString;
60
    }
61
 
62
    /**
63
     * Tells whether the provided value can be cast to string.
64
     */
65
    final public static function isStringable(mixed $value): bool
66
    {
67
        return \is_string($value) || $value instanceof \Stringable || \is_scalar($value);
68
    }
69
 
70
    /**
71
     * Casts scalars and stringable objects to strings.
72
     *
73
     * @throws \TypeError When the provided value is not stringable
74
     */
75
    final public static function resolve(\Stringable|string|int|float|bool $value): string
76
    {
77
        return $value;
78
    }
79
 
80
    public function __toString(): string
81
    {
82
        if (\is_string($this->value)) {
83
            return $this->value;
84
        }
85
 
86
        try {
87
            return $this->value = ($this->value)();
88
        } catch (\Throwable $e) {
89
            if (\TypeError::class === $e::class && __FILE__ === $e->getFile()) {
90
                $type = explode(', ', $e->getMessage());
91
                $type = substr(array_pop($type), 0, -\strlen(' returned'));
92
                $r = new \ReflectionFunction($this->value);
93
                $callback = $r->getStaticVariables()['callback'];
94
 
95
                $e = new \TypeError(sprintf('Return value of %s() passed to %s::fromCallable() must be of the type string, %s returned.', $callback, static::class, $type));
96
            }
97
 
98
            throw $e;
99
        }
100
    }
101
 
102
    public function __sleep(): array
103
    {
104
        $this->__toString();
105
 
106
        return ['value'];
107
    }
108
 
109
    public function jsonSerialize(): string
110
    {
111
        return $this->__toString();
112
    }
113
 
114
    private function __construct()
115
    {
116
    }
117
 
118
    private static function getPrettyName(callable $callback): string
119
    {
120
        if (\is_string($callback)) {
121
            return $callback;
122
        }
123
 
124
        if (\is_array($callback)) {
125
            $class = \is_object($callback[0]) ? get_debug_type($callback[0]) : $callback[0];
126
            $method = $callback[1];
127
        } elseif ($callback instanceof \Closure) {
128
            $r = new \ReflectionFunction($callback);
129
 
130
            if (str_contains($r->name, '{closure}') || !$class = \PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass()) {
131
                return $r->name;
132
            }
133
 
134
            $class = $class->name;
135
            $method = $r->name;
136
        } else {
137
            $class = get_debug_type($callback);
138
            $method = '__invoke';
139
        }
140
 
141
        return $class.'::'.$method;
142
    }
143
}