Skip to content

Commit 680f220

Browse files
authored
Merge pull request #29 from GeneaLabs/laravel-5.5
Refactor where clause parsing
2 parents 7213a54 + 5434cf4 commit 680f220

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

Diff for: phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
convertNoticesToExceptions="true"
77
convertWarningsToExceptions="true"
88
processIsolation="false"
9-
stopOnFailure="false"
9+
stopOnFailure="true"
1010
syntaxCheck="false"
1111
bootstrap="vendor/autoload.php"
1212
>

Diff for: src/CacheKey.php

+44-15
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,54 @@ protected function getWhereClauses(array $wheres = []) : string
9696
{
9797
return $this->getWheres($wheres)
9898
->reduce(function ($carry, $where) {
99-
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
100-
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
101-
}
99+
$value = $this->getNestedClauses($where);
100+
$value .= $this->getColumnClauses($where);
101+
$value .= $this->getRawClauses($where);
102+
$value .= $this->getOtherClauses($where, $carry);
102103

103-
if ($where['type'] === 'Column') {
104-
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
105-
}
104+
return $value;
105+
})
106+
. '';
107+
}
106108

107-
if ($where['type'] === 'raw') {
108-
return "_{$where['boolean']}_" . str_slug($where['sql']);
109-
}
109+
protected function getNestedClauses(array $where) : string
110+
{
111+
if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
112+
return '';
113+
}
110114

111-
$value = array_get($where, 'value');
112-
$value .= $this->getTypeClause($where);
113-
$value .= $this->getValuesClause($where);
115+
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
116+
}
114117

115-
return "{$carry}-{$where['column']}_{$value}";
116-
})
117-
. '';
118+
protected function getColumnClauses(array $where) : string
119+
{
120+
if ($where['type'] !== 'Column') {
121+
return '';
122+
}
123+
124+
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
125+
}
126+
127+
protected function getRawClauses(array $where) : string
128+
{
129+
if ($where['type'] !== 'raw') {
130+
return '';
131+
}
132+
133+
return "_{$where['boolean']}_" . str_slug($where['sql']);
134+
}
135+
136+
protected function getOtherClauses(array $where, string $carry = null) : string
137+
{
138+
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists', 'raw', 'Column'])) {
139+
return '';
140+
}
141+
142+
$value = array_get($where, 'value');
143+
$value .= $this->getTypeClause($where);
144+
$value .= $this->getValuesClause($where);
145+
146+
return "{$carry}-{$where['column']}_{$value}";
118147
}
119148

120149
protected function getWheres(array $wheres) : Collection

Diff for: tests/Unit/CachedBuilderTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@ public function testCountModelResultsCreatesCache()
269269
$this->assertEquals($liveResults, $cachedResults);
270270
}
271271

272-
/**
273-
* @group test
274-
*/
275272
public function testCursorModelResultsCreatesCache()
276273
{
277274
$authors = (new Author)

0 commit comments

Comments
 (0)