Skip to content

Commit

Permalink
Version 5.4.1 (#23)
Browse files Browse the repository at this point in the history
* Upgrading to phpunit 6

* Fixing toJson() for DateTime values

* Downgrading phpunit

* Version bump
  • Loading branch information
janhartigan authored and sdsomma committed Dec 20, 2017
1 parent 70678ab commit 69724c4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This library provides a set of classes that help represent requests for complex data and provides a way to convert requests to and from a standard JSON format. If you have interfaces with tons of parameters ($filters, $groupings, $page, $rowsPerPage, etc.), or if you're just looking for a standard way to communicate complex requests to other apps without racking your brain over how to represent this data in JSON, you will like this library.

- **Version:** 5.4.0
- **Version:** 5.4.1

[![Build Status](https://travis-ci.org/mongerinc/search-request.png?branch=master)](https://travis-ci.org/mongerinc/search-request)

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

### 5.4.1
- Fixing `toJson()` for DateTime values

### 5.4.0
- Automatically resetting the page to 1 whenever a filter, term, sort, or grouping changes. Same for facets with the other filter/sort changes.

Expand Down
10 changes: 9 additions & 1 deletion src/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,15 @@ public function toArray()
*/
public function toJson()
{
return json_encode($this->toArray());
$request = $this->toArray();

array_walk_recursive($request, function(&$value)
{
if (is_a($value, 'DateTime'))
$value = $value->format('Y-m-d H:i:s');
});

return json_encode($request);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Json/JsonTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Monger\SearchRequest\Tests\Json;

use DateTime;
use Monger\SearchRequest\SearchRequest;

class JsonTest extends \PHPUnit_Framework_TestCase {
Expand All @@ -25,6 +26,40 @@ public function fromJson()
$this->assertEquals($expectedRequest->toJson(), $request->toJson());
}

/**
* @test
*/
public function dates()
{
$request = SearchRequest::create()->where('date', new DateTime('2017-01-15 23:34:10'))
->where(function($filterSet)
{
$filterSet->where('anotherDate', new DateTime('2016-12-25 01:41:32'));
});

$this->assertEquals(json_encode([
'term' => null,
'page' => 1,
'limit' => 10,
'selects' => [],
'groups' => [],
'sorts' => [],
'filterSet' => [
'boolean' => 'and',
'filters' => [
['field' => 'date', 'operator' => '=', 'value' => '2017-01-15 23:34:10', 'boolean' => 'and'],
[
'boolean' => 'and',
'filters' => [
['field' => 'anotherDate', 'operator' => '=', 'value' => '2016-12-25 01:41:32', 'boolean' => 'and'],
]
]
]
],
'facets' => [],
]), $request->toJson());
}

/**
* Gets the expected search request
*
Expand Down

0 comments on commit 69724c4

Please sign in to comment.