Skip to content

Commit 9bdbd71

Browse files
dplewisBenjamin Wilson Friedman
authored andcommitted
Add matches to ParseQuery
* add matches to query * Bad $options test * not-a-real-modifier
1 parent f6f795a commit 9bdbd71

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

src/Parse/ParseQuery.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,27 @@ public function notContainedIn($key, $values)
694694
return $this;
695695
}
696696

697+
/**
698+
* Adds a regular expression constraint for finding string values that match
699+
* the provided regular expression.
700+
* This may be slow for large datasets.
701+
*
702+
* @param string $key The key that the string to match is stored in.
703+
* @param string $regex The regular expression pattern to match.
704+
* @param string $modifiers Modifies the search, supports i, m
705+
*
706+
* @return ParseQuery Returns the query, so you can chain this call.
707+
*/
708+
public function matches($key, $regex, $modifiers = '')
709+
{
710+
$this->addCondition($key, '$regex', $regex);
711+
if (strlen($modifiers)) {
712+
$this->addCondition($key, '$options', $modifiers);
713+
}
714+
715+
return $this;
716+
}
717+
697718
/**
698719
* Add a constraint that requires that a key's value matches a ParseQuery
699720
* constraint.

tests/Parse/ParseQueryTest.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,103 @@ public function testStartsWithRegexQuestionmark()
330330
);
331331
}
332332

333+
public function testMatchesSingle()
334+
{
335+
$this->provideTestObjects(10);
336+
$query = new ParseQuery('TestObject');
337+
$query->matches('foo', 'bar0');
338+
$results = $query->find();
339+
$this->assertEquals(
340+
count($results),
341+
1,
342+
'Matches function did not return correct number of objects.'
343+
);
344+
$this->assertEquals(
345+
$results[0]->get('foo'),
346+
'bar0',
347+
'Matches function did not return the correct object.'
348+
);
349+
}
350+
351+
public function testMatchesMultiple()
352+
{
353+
$this->provideTestObjects(10);
354+
$query = new ParseQuery('TestObject');
355+
$query->matches('foo', 'bar');
356+
$results = $query->find();
357+
$this->assertEquals(
358+
count($results),
359+
10,
360+
'Matches function did not return correct number of objects.'
361+
);
362+
}
363+
364+
public function testMatchesRegexDelimiters()
365+
{
366+
$testObject = ParseObject::create('TestObject');
367+
$testObject->set('foo', "foob\E");
368+
$testObject->save();
369+
$query = new ParseQuery('TestObject');
370+
$query->matches('foo', 'foob\E');
371+
$results = $query->find();
372+
$this->assertEquals(
373+
count($results),
374+
1,
375+
'Matches function did not return correct number of objects.'
376+
);
377+
$query->matches('foo', 'foobE');
378+
$results = $query->find();
379+
$this->assertEquals(
380+
count($results),
381+
0,
382+
'Matches function did not return correct number of objects.'
383+
);
384+
}
385+
386+
public function testMatchesCaseInsensitiveModifier()
387+
{
388+
$testObject = ParseObject::create('TestObject');
389+
$testObject->set('foo', 'FOOBAR');
390+
$testObject->save();
391+
$query = new ParseQuery('TestObject');
392+
$query->matches('foo', 'foo', 'i');
393+
$results = $query->find();
394+
$this->assertEquals(
395+
count($results),
396+
1,
397+
'Matches function did not return correct number of objects.'
398+
);
399+
$this->assertEquals(
400+
$results[0]->get('foo'),
401+
'FOOBAR',
402+
'Matches function did not return correct number of objects.'
403+
);
404+
}
405+
406+
public function testMatchesMultilineModifier()
407+
{
408+
$testObject = ParseObject::create('TestObject');
409+
$testObject->set('foo', 'foo\nbar');
410+
$testObject->save();
411+
$query = new ParseQuery('TestObject');
412+
$query->matches('foo', 'bar', 'm');
413+
$results = $query->find();
414+
$this->assertEquals(
415+
count($results),
416+
1,
417+
'Matches function did not return correct number of objects.'
418+
);
419+
}
420+
421+
public function testMatchesBadOptions()
422+
{
423+
$this->provideTestObjects(10);
424+
$query = new ParseQuery('TestObject');
425+
$query->matches('foo', 'bar', 'not-a-real-modifier');
426+
$this->setExpectedException('Parse\ParseException', 'Bad $options value for query: not-a-real-modifier', 102);
427+
$query->find();
428+
}
429+
333430
public function testContainsSingle()
334431
{
335432
$testObject = ParseObject::create('TestObject');
@@ -381,7 +478,7 @@ public function testContainsNonExistent()
381478
'Contains should not find.'
382479
);
383480
}
384-
481+
385482
public function testGreaterThan()
386483
{
387484
$this->provideTestObjects(10);

0 commit comments

Comments
 (0)