From 69724c4689bc79f0654e9badb9959dc52a7d85c2 Mon Sep 17 00:00:00 2001 From: Jan Hartigan Date: Wed, 20 Dec 2017 14:26:12 -0800 Subject: [PATCH] Version 5.4.1 (#23) * Upgrading to phpunit 6 * Fixing toJson() for DateTime values * Downgrading phpunit * Version bump --- README.md | 2 +- changelog.md | 3 +++ src/SearchRequest.php | 10 +++++++++- tests/Json/JsonTest.php | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 759ef21..b0fbb0f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/changelog.md b/changelog.md index 7bb0877..16d588e 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/src/SearchRequest.php b/src/SearchRequest.php index 585b0da..c63b195 100644 --- a/src/SearchRequest.php +++ b/src/SearchRequest.php @@ -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); } /** diff --git a/tests/Json/JsonTest.php b/tests/Json/JsonTest.php index 0ce72e3..34fe866 100644 --- a/tests/Json/JsonTest.php +++ b/tests/Json/JsonTest.php @@ -1,5 +1,6 @@ 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 *