Skip to content

Commit acb7314

Browse files
author
Arthur Cinader
committed
Add contains filter to query object.
1 parent 3fd16b6 commit acb7314

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/Parse/ParseQuery.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function startsWith($key, $value)
267267

268268
return $this;
269269
}
270-
270+
271271
/**
272272
* Add a constraint to the query that requires a particular key's value to
273273
* end with the provided value.
@@ -283,7 +283,23 @@ public function endsWith($key, $value)
283283

284284
return $this;
285285
}
286-
286+
287+
/**
288+
* Adds a constraint for finding string values that contain a provided
289+
* string. This may be slow for large datasets.
290+
*
291+
* @param string $key The key to check.
292+
* @param mixed $value The substring that the value must contain.
293+
*
294+
* @return ParseQuery Returns this query, so you can chain this call.
295+
*/
296+
public function contains($key, $value)
297+
{
298+
$this->addCondition($key, '$regex', $this->quote($value));
299+
300+
return $this;
301+
}
302+
287303
/**
288304
* Returns an associative array of the query constraints.
289305
*

tests/Parse/ParseQueryTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,58 @@ public function testStartsWithRegexQuestionmark()
330330
);
331331
}
332332

333+
public function testContainsSingle()
334+
{
335+
$testObject = ParseObject::create('TestObject');
336+
$testObject->set('foo', 'foobarfoo');
337+
$testObject->save();
338+
$query = new ParseQuery('TestObject');
339+
$query->contains('foo', 'bar');
340+
$results = $query->find();
341+
$this->assertEquals(
342+
count($results),
343+
1,
344+
'Contains should find the string.'
345+
);
346+
}
347+
348+
public function testContainsMultiple()
349+
{
350+
$this->provideTestObjects(10);
351+
$query = new ParseQuery('TestObject');
352+
$query->contains('foo', 'bar');
353+
$results = $query->find();
354+
$this->assertEquals(
355+
count($results),
356+
10,
357+
'Contains function did not return correct number of objects.'
358+
);
359+
360+
$query = new ParseQuery('TestObject');
361+
$query->contains('foo', '8');
362+
$results = $query->find();
363+
$this->assertEquals(
364+
count($results),
365+
1,
366+
'Contains function did not return correct number of objects.'
367+
);
368+
}
369+
370+
public function testContainsNonExistent()
371+
{
372+
$testObject = ParseObject::create('TestObject');
373+
$testObject->set('foo', 'foobarfoo');
374+
$testObject->save();
375+
$query = new ParseQuery('TestObject');
376+
$query->contains('foo', 'baz');
377+
$results = $query->find();
378+
$this->assertEquals(
379+
count($results),
380+
0,
381+
'Contains should not find.'
382+
);
383+
}
384+
333385
public function testGreaterThan()
334386
{
335387
$this->provideTestObjects(10);

0 commit comments

Comments
 (0)