Skip to content

Commit 3029e3f

Browse files
committed
支持闭包
1 parent ccfa40c commit 3029e3f

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "library",
1616
"minimum-stability": "dev",
1717
"require": {
18-
"php": ">=7.0",
18+
"php": ">=7.1",
1919
"fideloper/proxy": "^4.0",
2020
"laravel/framework": ">=5.5"
2121
},

src/build/resolves/QueryBuilder.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace zyimm\query\build\resolves;
44

55

6+
use Closure;
67
use Illuminate\Support\Str;
78

89
/**
@@ -20,6 +21,8 @@ class QueryBuilder
2021

2122
public $aliasCount = [];
2223

24+
private $call = [];
25+
2326
private $alias, $field, $operator;
2427

2528
/**
@@ -93,13 +96,22 @@ public function build(): array
9396
continue;
9497
}
9598
// iteration fields
96-
foreach ($fields as $field) {
97-
if (!isset($this->params[$field])) {
98-
continue;
99-
}
99+
foreach ($fields as $key => $field) {
100100
$this->operator = strtolower($operator);
101-
// defineAliasField
102-
$this->defineAliasField($field)->handle();
101+
if (is_string($field)) {
102+
if (!isset($this->params[$field]) || is_null($this->params[$field])) {
103+
continue;
104+
}
105+
// defineAliasField
106+
$this->defineAliasField($field);
107+
}
108+
if ($field instanceof Closure) {
109+
$this->call = [
110+
'call' => $field,
111+
'field' => $key
112+
];
113+
}
114+
$this->handle();
103115
}
104116
}
105117
return $this->where;
@@ -109,19 +121,18 @@ public function build(): array
109121
* defineAliasField
110122
*
111123
* @param string $field
112-
* @return $this
124+
* @return void
113125
*/
114-
private function defineAliasField(string $field = ''): QueryBuilder
126+
private function defineAliasField(string $field = ''): void
115127
{
116128
if (stripos($field, '.')) {
117129
list ($this->alias, $this->field) = explode('.', $field);
118-
$this->alias .= '.';
119-
array_push($this->aliasCount, $this->alias);
130+
$this->alias .= '.';
131+
$this->aliasCount[] = $this->alias;
120132
} else {
121133
$this->alias = '';
122134
}
123135
$this->field = $field;
124-
return $this;
125136
}
126137

127138
/**
@@ -141,6 +152,24 @@ public function inBetween(): QueryBuilder
141152
return $this;
142153
}
143154

155+
/**
156+
* execClosure
157+
*
158+
* @return $this
159+
*/
160+
public function execClosure(): QueryBuilder
161+
{
162+
$field = $this->call['field'];
163+
if ($this->call['call'] instanceof Closure) {
164+
$this->where[$field.$this->operator] = [
165+
$field,
166+
'where',
167+
$this->call['call']
168+
];
169+
}
170+
return $this;
171+
}
172+
144173
/**
145174
* general
146175
*

0 commit comments

Comments
 (0)