PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / vendor / wpfluent / framework / src / WPFluent / Database / Concerns / CompilesJsonPaths.php

CompilesJsonPaths.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at vendor/wpfluent/framework/src/WPFluent/Database/Concerns/CompilesJsonPaths.php

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