PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.1.0
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.1.0
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Database / Concerns / CompilesJsonPaths.php

CompilesJsonPaths.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 2.1.0, 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 FluentBoards\Framework\Database\Concerns;
4
5 use FluentBoards\Framework\Support\Str;
6 use FluentBoards\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