Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\VarDumper\Caster;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Uid\Ulid;
16
use Symfony\Component\Uid\Uuid;
17
use Symfony\Component\VarDumper\Cloner\Stub;
18
use Symfony\Component\VarExporter\Internal\LazyObjectState;
19
 
20
/**
21
 * @final
22
 */
23
class SymfonyCaster
24
{
25
    private const REQUEST_GETTERS = [
26
        'pathInfo' => 'getPathInfo',
27
        'requestUri' => 'getRequestUri',
28
        'baseUrl' => 'getBaseUrl',
29
        'basePath' => 'getBasePath',
30
        'method' => 'getMethod',
31
        'format' => 'getRequestFormat',
32
    ];
33
 
34
    public static function castRequest(Request $request, array $a, Stub $stub, bool $isNested)
35
    {
36
        $clone = null;
37
 
38
        foreach (self::REQUEST_GETTERS as $prop => $getter) {
39
            $key = Caster::PREFIX_PROTECTED.$prop;
40
            if (\array_key_exists($key, $a) && null === $a[$key]) {
41
                $clone ??= clone $request;
42
                $a[Caster::PREFIX_VIRTUAL.$prop] = $clone->{$getter}();
43
            }
44
        }
45
 
46
        return $a;
47
    }
48
 
49
    public static function castHttpClient($client, array $a, Stub $stub, bool $isNested)
50
    {
51
        $multiKey = sprintf("\0%s\0multi", $client::class);
52
        if (isset($a[$multiKey])) {
53
            $a[$multiKey] = new CutStub($a[$multiKey]);
54
        }
55
 
56
        return $a;
57
    }
58
 
59
    public static function castHttpClientResponse($response, array $a, Stub $stub, bool $isNested)
60
    {
61
        $stub->cut += \count($a);
62
        $a = [];
63
 
64
        foreach ($response->getInfo() as $k => $v) {
65
            $a[Caster::PREFIX_VIRTUAL.$k] = $v;
66
        }
67
 
68
        return $a;
69
    }
70
 
71
    public static function castLazyObjectState($state, array $a, Stub $stub, bool $isNested)
72
    {
73
        if (!$isNested) {
74
            return $a;
75
        }
76
 
77
        $stub->cut += \count($a) - 1;
78
 
688 lars 79
        $instance = $a['realInstance'] ?? null;
80
 
81
        $a = ['status' => new ConstStub(match ($a['status']) {
148 lars 82
            LazyObjectState::STATUS_INITIALIZED_FULL => 'INITIALIZED_FULL',
83
            LazyObjectState::STATUS_INITIALIZED_PARTIAL => 'INITIALIZED_PARTIAL',
84
            LazyObjectState::STATUS_UNINITIALIZED_FULL => 'UNINITIALIZED_FULL',
85
            LazyObjectState::STATUS_UNINITIALIZED_PARTIAL => 'UNINITIALIZED_PARTIAL',
86
        }, $a['status'])];
688 lars 87
 
88
        if ($instance) {
89
            $a['realInstance'] = $instance;
90
            --$stub->cut;
91
        }
92
 
93
        return $a;
148 lars 94
    }
95
 
96
    public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested)
97
    {
98
        $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $uuid->toBase58();
99
        $a[Caster::PREFIX_VIRTUAL.'toBase32'] = $uuid->toBase32();
100
 
101
        // symfony/uid >= 5.3
102
        if (method_exists($uuid, 'getDateTime')) {
103
            $a[Caster::PREFIX_VIRTUAL.'time'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C');
104
        }
105
 
106
        return $a;
107
    }
108
 
109
    public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested)
110
    {
111
        $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $ulid->toBase58();
112
        $a[Caster::PREFIX_VIRTUAL.'toRfc4122'] = $ulid->toRfc4122();
113
 
114
        // symfony/uid >= 5.3
115
        if (method_exists($ulid, 'getDateTime')) {
116
            $a[Caster::PREFIX_VIRTUAL.'time'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C');
117
        }
118
 
119
        return $a;
120
    }
121
}