Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Concerns;
4
 
5
use Illuminate\Support\Str;
6
 
7
trait CompilesJsonPaths
8
{
9
    /**
10
     * Split the given JSON selector into the field and the optional path and wrap them separately.
11
     *
12
     * @param  string  $column
13
     * @return array
14
     */
15
    protected function wrapJsonFieldAndPath($column)
16
    {
17
        $parts = explode('->', $column, 2);
18
 
19
        $field = $this->wrap($parts[0]);
20
 
21
        $path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1], '->') : '';
22
 
23
        return [$field, $path];
24
    }
25
 
26
    /**
27
     * Wrap the given JSON path.
28
     *
29
     * @param  string  $value
30
     * @param  string  $delimiter
31
     * @return string
32
     */
33
    protected function wrapJsonPath($value, $delimiter = '->')
34
    {
35
        $value = preg_replace("/([\\\\]+)?\\'/", "''", $value);
36
 
37
        $jsonPath = collect(explode($delimiter, $value))
38
            ->map(fn ($segment) => $this->wrapJsonPathSegment($segment))
39
            ->join('.');
40
 
41
        return "'$".(str_starts_with($jsonPath, '[') ? '' : '.').$jsonPath."'";
42
    }
43
 
44
    /**
45
     * Wrap the given JSON path segment.
46
     *
47
     * @param  string  $segment
48
     * @return string
49
     */
50
    protected function wrapJsonPathSegment($segment)
51
    {
52
        if (preg_match('/(\[[^\]]+\])+$/', $segment, $parts)) {
53
            $key = Str::beforeLast($segment, $parts[0]);
54
 
55
            if (! empty($key)) {
56
                return '"'.$key.'"'.$parts[0];
57
            }
58
 
59
            return $parts[0];
60
        }
61
 
62
        return '"'.$segment.'"';
63
    }
64
}