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 / app / Models / Query / QueryParser.php

QueryParser.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Models/Query/QueryParser.php

115 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Models\Query;
4
5
6 use FluentCart\App\Models\Concerns\CanSearch;
7 use FluentCart\Framework\Database\Orm\Builder;
8 use FluentCart\Framework\Support\Arr;
9
10 class QueryParser
11 {
12 use CanSearch;
13
14
15 static function make(): QueryParser
16 {
17 return new static();
18 }
19
20 public function parse(Builder $query, array $condition)
21 {
22 $conditionType = $this->resolveCondition($condition);
23 $this->parseQuery($query, $condition, $conditionType, true, $conditionType);
24 }
25
26 private function resolveCondition($condition): string
27 {
28 return strtolower(Arr::get($condition, 'type', 'and')) === 'or' ? 'or' : 'and';
29 }
30
31
32 public function parseQuery(Builder $query, array $condition, $conditionType = 'and')
33 {
34 if (isset($condition['isCondition'])) {
35 $this->handleCondition($query, $condition, $conditionType);
36 } else if (isset($condition['isRelation'])) {
37 $this->handleRelation($query, $condition, $conditionType);
38 } else if (isset($condition['isOperator'])) {
39 $this->handleOperator($query, $condition, $conditionType);
40 }
41 }
42
43
44 public function handleCondition($query, $condition, $conditionTypeOriginal)
45 {
46 $conditions = Arr::get($condition, 'conditions', []);
47 if (!is_array($conditions) || empty($conditions)) {
48 return;
49 }
50
51 $conditionType = $this->resolveCondition($condition);
52
53
54 $groupUsing = $conditionTypeOriginal === 'or' ? 'orWhere' : 'where';
55
56 $query->{$groupUsing}(function ($q) use ($conditions, $conditionType, $condition, $groupUsing) {
57 foreach ($conditions as $index => $childCondition) {
58 $this->parseQuery($q, $childCondition, $conditionType);
59 }
60 });
61 }
62
63 public function handleOperator($query, $condition, $conditionType = 'and')
64 {
65 $columns = Arr::get($condition, 'column');
66 if (empty($columns)) {
67 return;
68 }
69
70
71 $columns = Arr::wrap($columns);
72 $value = sanitize_text_field(Arr::get($condition, 'value'));
73
74 foreach ($columns as $index => $column) {
75
76 $operator = Arr::get($condition, 'operator');
77 if ($conditionType === 'or') {
78 $operator = 'or_' . $operator;
79 }
80
81 $query->search(
82 [
83 $column => [
84 "column" => $column,
85 "operator" => $operator,
86 "value" => $value
87 ]
88 ]
89 );
90
91 }
92 }
93
94 public function handleRelation($query, $condition, $conditionType = 'and')
95 {
96
97 $relationMethod = $conditionType === 'and' ? 'whereHas' : 'orWhereHas';
98 $relationName = $condition['name'];
99
100 $innerConditions = Arr::get($condition, 'conditions', []);
101
102 if (empty($innerConditions)) {
103 $query->{$relationMethod}($relationName);
104 } else {
105 $query->{$relationMethod}($relationName, function ($query) use ($innerConditions, $conditionType) {
106 foreach ($innerConditions as $condition) {
107 $this->parseQuery($query, $condition, $conditionType);
108 }
109 });
110 }
111 }
112
113
114 }
115