Skip to content

Commit

Permalink
Version 2.1.0 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
janhartigan authored Jul 5, 2017
1 parent 06f835e commit 604dad5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog

### 2.1.0
- Added term property for global text searches

### 2.0.0
- Changing JSON/arrayable top-level filters to filterSet for more clarity
- Some minor comment cleanup

### 1.2.0
- Added a `getSkip()` method to the search request to help people avoid making that calcuation.
- Added contributing guidelines.
Expand Down
36 changes: 36 additions & 0 deletions src/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class SearchRequest {
*/
protected $sorts = [];

/**
* The global search term
*
* @var string
*/
protected $term;

/**
* The requested page
*
Expand Down Expand Up @@ -42,6 +49,7 @@ public function __construct($json = null)
{
$inputs = json_decode($json, true);

$this->term = $inputs['term'];
$this->page = $inputs['page'];
$this->limit = $inputs['limit'];
$this->addSortsFromArray($inputs['sorts']);
Expand Down Expand Up @@ -78,6 +86,33 @@ public function addFilterSetFromArray(array $filterSet)
$this->filterSet->addFiltersFromArray($filterSet['filters']);
}

/**
* Adds the provided global search term
*
* @param string $term
*
* @return $this
*/
public function term($term)
{
if (!is_string($term) && !is_null($term))
throw new InvalidArgumentException("A search term can only be a string or null.");

$this->term = $term;

return $this;
}

/**
* Gets the current global search term
*
* @return mixed
*/
public function getTerm()
{
return $this->term;
}

/**
* Overrides all sorts and sets the given field/direction as the primary sort
*
Expand Down Expand Up @@ -226,6 +261,7 @@ public function getFilters()
public function toArray()
{
return [
'term' => $this->term,
'page' => $this->page,
'limit' => $this->limit,
'sorts' => array_map(function(Sort $sort) {return $sort->toArray();}, $this->sorts),
Expand Down
2 changes: 2 additions & 0 deletions tests/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function getExpectedRequest()
$request = new SearchRequest;

$request->page(5)->limit(50)
->term('search this')
->addSort('something', 'asc')->addSort('otherThing', 'desc')
->where('fun', 'more')->orWhere(function($filterSet)
{
Expand All @@ -52,6 +53,7 @@ protected function getExpectedRequest()
protected function getExpectedJson()
{
return json_encode([
'term' => 'search this',
'page' => 5,
'limit' => 50,
'sorts' => [
Expand Down
56 changes: 56 additions & 0 deletions tests/Term/InvalidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php namespace Monger\SearchRequest\Tests\Term;

use Monger\SearchRequest\SearchRequest;

class InvalidationTest extends \PHPUnit_Framework_TestCase {

/**
* @var \Monger\SearchRequest\SearchRequest
*/
protected $request;

/**
* Set up before each test
*/
public function setup()
{
$this->request = new SearchRequest;
}

/**
* @test
* @expectedException InvalidArgumentException
*/
public function pageArray()
{
$this->request->term(['not an integer']);
}

/**
* @test
* @expectedException InvalidArgumentException
*/
public function pageFloat()
{
$this->request->term(56.54);
}

/**
* @test
* @expectedException InvalidArgumentException
*/
public function pageNegative()
{
$this->request->term(-5);
}

/**
* @test
* @expectedException InvalidArgumentException
*/
public function pageInt()
{
$this->request->term(6);
}

}
42 changes: 42 additions & 0 deletions tests/Term/TermTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace Monger\SearchRequest\Tests\Term;

use Monger\SearchRequest\SearchRequest;

class TermTest extends \PHPUnit_Framework_TestCase {

/**
* @test
*/
public function defaults()
{
$request = new SearchRequest;

$this->assertEquals(null, $request->getTerm());
}

/**
* @test
*/
public function string()
{
$request = new SearchRequest;

$request->term('something');

$this->assertEquals('something', $request->getTerm());
}

/**
* @test
*/
public function clear()
{
$request = new SearchRequest;

$request->term('something');
$request->term(null);

$this->assertEquals(null, $request->getTerm());
}

}

0 comments on commit 604dad5

Please sign in to comment.