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
| 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 |