Skip to content

Commit 2dd709d

Browse files
committed
Merge pull request #87 from alpha0010/master
Switch check to Closure for match and subselect builders, fixes #82
2 parents 88602c2 + 5fd40dc commit 2dd709d

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/Match.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function create(SphinxQL $sphinxql)
6464
* $match->match($sub);
6565
* // (a | b)
6666
*
67-
* @param string|Match|callable $keywords The text or expression to match.
67+
* @param string|Match|Closure $keywords The text or expression to match.
6868
*/
6969
public function match($keywords = null)
7070
{
@@ -84,7 +84,7 @@ public function match($keywords = null)
8484
* $match->match('test')->orMatch('case');
8585
* // test | case
8686
*
87-
* @param string|Match|callable $keywords The text or expression to alternatively match.
87+
* @param string|Match|Closure $keywords The text or expression to alternatively match.
8888
*/
8989
public function orMatch($keywords = null)
9090
{
@@ -103,7 +103,7 @@ public function orMatch($keywords = null)
103103
* $match->match('test')->maybe('case');
104104
* // test MAYBE case
105105
*
106-
* @param string|Match|callable $keywords The text or expression to optionally match.
106+
* @param string|Match|Closure $keywords The text or expression to optionally match.
107107
*/
108108
public function maybe($keywords = null)
109109
{
@@ -267,7 +267,7 @@ public function quorum($keywords, $threshold)
267267
* $match->match('test')->before('case');
268268
* // test << case
269269
*
270-
* @param string|Match|callable $keywords The text or expression that must come after.
270+
* @param string|Match|Closure $keywords The text or expression that must come after.
271271
*/
272272
public function before($keywords = null)
273273
{
@@ -329,8 +329,8 @@ public function boost($keyword, $amount = null)
329329
* $match->match('test')->near('case', 3);
330330
* // test NEAR/3 case
331331
*
332-
* @param string|Match|callable $keywords The text or expression to match nearby.
333-
* @param int $distance Maximum distance to the match.
332+
* @param string|Match|Closure $keywords The text or expression to match nearby.
333+
* @param int $distance Maximum distance to the match.
334334
*/
335335
public function near($keywords, $distance = null)
336336
{
@@ -351,7 +351,7 @@ public function near($keywords, $distance = null)
351351
* $match->match('test')->sentence('case');
352352
* // test SENTENCE case
353353
*
354-
* @param string|Match|callable $keywords The text or expression that must be in the sentence.
354+
* @param string|Match|Closure $keywords The text or expression that must be in the sentence.
355355
*/
356356
public function sentence($keywords = null)
357357
{
@@ -370,7 +370,7 @@ public function sentence($keywords = null)
370370
* $match->match('test')->paragraph('case');
371371
* // test PARAGRAPH case
372372
*
373-
* @param string|Match|callable $keywords The text or expression that must be in the paragraph.
373+
* @param string|Match|Closure $keywords The text or expression that must be in the paragraph.
374374
*/
375375
public function paragraph($keywords = null)
376376
{
@@ -392,8 +392,8 @@ public function paragraph($keywords = null)
392392
* $match->zone('th', 'test');
393393
* // ZONE:(th) test
394394
*
395-
* @param string|array $zones The zone or zones to search.
396-
* @param string|Match|callable $keywords The text or expression that must be in these zones.
395+
* @param string|array $zones The zone or zones to search.
396+
* @param string|Match|Closure $keywords The text or expression that must be in these zones.
397397
*/
398398
public function zone($zones, $keywords = null)
399399
{
@@ -416,8 +416,8 @@ public function zone($zones, $keywords = null)
416416
* $match->zonespan('th', 'test');
417417
* // ZONESPAN:(th) test
418418
*
419-
* @param string $zone The zone to search.
420-
* @param string|Match|callable $keywords The text or expression that must be in this zone.
419+
* @param string $zone The zone to search.
420+
* @param string|Match|Closure $keywords The text or expression that must be in this zone.
421421
*/
422422
public function zonespan($zone, $keywords = null)
423423
{
@@ -438,7 +438,7 @@ public function compile()
438438
$query .= $token['MATCH']->value().' ';
439439
} elseif ($token['MATCH'] instanceof Match) {
440440
$query .= '('.$token['MATCH']->compile()->getCompiled().') ';
441-
} elseif (is_callable($token['MATCH'])) {
441+
} elseif ($token['MATCH'] instanceof \Closure) {
442442
$sub = new static($this->sphinxql);
443443
call_user_func($token['MATCH'], $sub);
444444
$query .= '('.$sub->compile()->getCompiled().') ';

src/SphinxQL.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public function compileMatch()
440440

441441
foreach ($this->match as $match) {
442442
$pre = '';
443-
if (is_callable($match['column'])) {
443+
if ($match['column'] instanceof \Closure) {
444444
$sub = new Match($this);
445445
call_user_func($match['column'], $sub);
446446
$pre .= $sub->compile()->getCompiled();
@@ -545,7 +545,7 @@ public function compileSelect()
545545
}
546546

547547
if (!empty($this->from)) {
548-
if (is_callable($this->from)) {
548+
if ($this->from instanceof \Closure) {
549549
$sub = new static($this->getConnection());
550550
call_user_func($this->from, $sub);
551551
$query .= 'FROM ('.$sub->compile()->getCompiled().') ';
@@ -883,7 +883,7 @@ public function from($array = null)
883883
$this->from = \func_get_args();
884884
}
885885

886-
if (is_array($array) || is_callable($array) || $array instanceof SphinxQL) {
886+
if (is_array($array) || $array instanceof \Closure || $array instanceof SphinxQL) {
887887
$this->from = $array;
888888
}
889889

@@ -893,7 +893,7 @@ public function from($array = null)
893893
/**
894894
* MATCH clause (Sphinx-specific)
895895
*
896-
* @param mixed $column The column name (can be array, string, function, or Match)
896+
* @param mixed $column The column name (can be array, string, Closure, or Match)
897897
* @param string $value The value
898898
* @param boolean $half Exclude ", |, - control characters from being escaped
899899
*

tests/SphinxQL/MatchTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,12 @@ public function testCompile()
297297
});
298298
$this->assertEquals('aaa -(bbb -(ccc ddd))', $match->compile()->getCompiled());
299299
}
300+
301+
// issue #82
302+
public function testClosureMisuse()
303+
{
304+
$match = Match::create(self::$sphinxql)
305+
->match('strlen');
306+
$this->assertEquals('strlen', $match->compile()->getCompiled());
307+
}
300308
}

tests/SphinxQL/SphinxQLTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -946,4 +946,26 @@ public function testFacet()
946946
$this->assertEquals('2', $result[1][3]['count(*)']);
947947
}
948948
}
949+
950+
// issue #82
951+
public function testClosureMisuse()
952+
{
953+
$query = SphinxQL::create(self::$conn)
954+
->select()
955+
->from('strlen')
956+
->orderBy('id', 'ASC');
957+
$this->assertEquals(
958+
'SELECT * FROM strlen ORDER BY id ASC',
959+
$query->compile()->getCompiled()
960+
);
961+
962+
$query = SphinxQL::create(self::$conn)
963+
->select()
964+
->from('rt')
965+
->match('strlen', 'value');
966+
$this->assertEquals(
967+
"SELECT * FROM rt WHERE MATCH('(@strlen value)')",
968+
$query->compile()->getCompiled()
969+
);
970+
}
949971
}

0 commit comments

Comments
 (0)