PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.92
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.92
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Database / Concerns / CompilesJsonPaths.php

CompilesJsonPaths.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.92, 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 FluentCommunity\Framework\Database\Concerns;
4
5 use FluentCommunity\Framework\Support\Str;
6 use FluentCommunity\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