diff --git a/.travis.yml b/.travis.yml index 9f8bc4e..69ffef5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - hhvm diff --git a/composer.json b/composer.json index 1ad436d..c53a97a 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ } ], "require": { + "php": ">=5.6.0", "lstrojny/functional-php": "dev-master", "internations/solr-utils": "~0.4" }, diff --git a/docs/01-expression-builder-usage.md b/docs/01-expression-builder-usage.md index 91a46aa..24631c6 100644 --- a/docs/01-expression-builder-usage.md +++ b/docs/01-expression-builder-usage.md @@ -85,6 +85,12 @@ TO 2012-12-31T23:59:56]`). $eb->field('publishedOn', $eb->dateRange(new DateTime('2012-01-01 00:00:00'), new DateTime('2012-12-31 23:59:59'))); ``` +We can also mix inclusive “[]” and exclusive “{}” endpoints, so expressions like `date:[2015-08-27T15:35:04Z/DAY TO 2015-08-27T15:35:04Z/DAY+1DAY}` are possible (as of Solr 4.0, more information http://lucidworks.com/blog/date-math-now-and-filter-queries/) + +```php +$eb->field('date', $eb->dateRange(new DateTime('2015-08-27 15:35:04') . '/DAY', new DateTime('2015-08-27 15:35:04') . '/DAY+1DAY'), true, false); +``` + ## Grouping Grouping is a powerful feature of Lucene’s search syntax. Let’s search for a list of product names diff --git a/src/InterNations/Component/Solr/Expression/ExpressionBuilder.php b/src/InterNations/Component/Solr/Expression/ExpressionBuilder.php index 04f73f0..6f12553 100644 --- a/src/InterNations/Component/Solr/Expression/ExpressionBuilder.php +++ b/src/InterNations/Component/Solr/Expression/ExpressionBuilder.php @@ -139,12 +139,13 @@ public function fzz($expr, $similarity = null) * * @param string $start * @param string $end - * @param boolean $inclusive + * @param boolean $inclusiveFrom + * @param boolean $inclusiveTo * @return ExpressionInterface */ - public function range($start = null, $end = null, $inclusive = true) + public function range($start = null, $end = null, $inclusiveFrom = true, $inclusiveTo = true) { - return new RangeExpression($start, $end, $inclusive); + return new RangeExpression($start, $end, $inclusiveFrom, $inclusiveTo); } /** @@ -156,7 +157,7 @@ public function range($start = null, $end = null, $inclusive = true) */ public function btwnRange($start = null, $end = null) { - return new RangeExpression($start, $end, false); + return new RangeExpression($start, $end, false, false); } /** @@ -403,12 +404,17 @@ public function date(DateTime $date = null, $timezone = false) * Create a range between two dates (one side may be unlimited which is indicated by passing null) * * @param DateTime $from + * @param boolean $inclusiveFrom * @param DateTime $to - * @param boolean $inclusive + * @param boolean $inclusiveTo * @param boolean $timezone * @return ExpressionInterface|null */ - public function dateRange(DateTime $from = null, DateTime $to = null, $inclusive = true, $timezone = false) + public function dateRange(DateTime $from = null, + DateTime $to = null, + $inclusiveFrom = true, + $inclusiveTo = true, + $timezone = false) { if ($from === null && $to === null) { return null; @@ -417,7 +423,8 @@ public function dateRange(DateTime $from = null, DateTime $to = null, $inclusive return $this->range( $this->lit($this->date($from, $timezone)), $this->lit($this->date($to, $timezone)), - $inclusive + $inclusiveFrom, + $inclusiveTo ); } diff --git a/src/InterNations/Component/Solr/Expression/RangeExpression.php b/src/InterNations/Component/Solr/Expression/RangeExpression.php index b39e1e7..8c530c8 100644 --- a/src/InterNations/Component/Solr/Expression/RangeExpression.php +++ b/src/InterNations/Component/Solr/Expression/RangeExpression.php @@ -6,7 +6,7 @@ /** * Range expression class * - * Let you specify range queries in the like of field:[ TO ] or field:{ TO } + * Let you specify range queries in the like of field:[ TO ] or field:{ TO } or also mixing inclusive/exclusive:[ TO } as of Solr 4.0 */ class RangeExpression extends Expression { @@ -25,24 +25,33 @@ class RangeExpression extends Expression protected $end; /** - * Inclusive or exclusive the range start/end? + * Inclusive or exclusive the range start? * * @var boolean */ - protected $inclusive; + protected $inclusiveFrom; + + /** + * Inclusive or exclusive the range end? + * + * @var boolean + */ + protected $inclusiveTo; /** * Create new range query object * * @param string|integer|Expression $start * @param string|integer|Expression $end - * @param boolean $inclusive + * @param boolean $inclusiveFrom + * @param boolean $inclusiveTo */ - public function __construct($start = null, $end = null, $inclusive = true) + public function __construct($start = null, $end = null, $inclusiveFrom = true, $inclusiveTo = true) { $this->start = $start; $this->end = $end; - $this->inclusive = (bool) $inclusive; + $this->inclusiveFrom = (bool) $inclusiveFrom; + $this->inclusiveTo = (bool) $inclusiveTo; } /** @@ -52,10 +61,10 @@ public function __toString() { return sprintf( '%s%s TO %s%s', - $this->inclusive ? '[' : '{', + $this->inclusiveFrom ? '[' : '{', $this->cast($this->start), $this->cast($this->end), - $this->inclusive ? ']' : '}' + $this->inclusiveTo ? ']' : '}' ); } diff --git a/tests/InterNations/Component/Solr/Tests/Expression/ExpressionBuilderTest.php b/tests/InterNations/Component/Solr/Tests/Expression/ExpressionBuilderTest.php index dd568a2..9f208b8 100644 --- a/tests/InterNations/Component/Solr/Tests/Expression/ExpressionBuilderTest.php +++ b/tests/InterNations/Component/Solr/Tests/Expression/ExpressionBuilderTest.php @@ -426,27 +426,30 @@ public static function getDateRangeData() 'Europe/Berlin' ), array( - '[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]', + '{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]', '2010-10-11 02:00:00', 'Europe/Berlin', '2010-10-22 01:59:59', 'Europe/Berlin', - true + false ), array( - '[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]', + '{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}', '2010-10-11 02:00:00', 'Europe/Berlin', '2010-10-22 01:59:59', - 'Europe/Berlin' + 'Europe/Berlin', + false, + false ), array( - '[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]', + '[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}', '2010-10-11 02:00:00', 'Europe/Berlin', '2010-10-22 01:59:59', 'Europe/Berlin', - true + true, + false ), array( '[* TO 2010-10-21T23:59:59Z]', @@ -456,11 +459,22 @@ public static function getDateRangeData() 'Europe/Berlin' ), array( - '[2010-10-11T00:00:00Z TO *]', + '{* TO 2010-10-21T23:59:59Z}', + null, + null, + '2010-10-22 01:59:59', + 'Europe/Berlin', + false, + false + ), + array( + '{2010-10-11T00:00:00Z TO *]', '2010-10-11 02:00:00', 'Europe/Berlin', null, - null + null, + false, + true ), array( '{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}', @@ -468,6 +482,7 @@ public static function getDateRangeData() 'Europe/Berlin', '2010-10-22 01:59:59', 'Europe/Berlin', + false, false ), array( @@ -478,12 +493,13 @@ public static function getDateRangeData() null ), array( - '[2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z]', + '{2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z}', '2010-10-11 02:00:00', 'Europe/Berlin', '2010-10-22 01:59:59', 'Europe/Berlin', - null, + false, + false, 'null', 'Europe/Moscow', ), @@ -494,16 +510,18 @@ public static function getDateRangeData() '2010-10-22 01:59:59', 'Europe/Berlin', true, + true, 'null', 'Europe/Moscow', ), array( - '[2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z]', + '{2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z}', '2010-10-11 02:00:00', 'Europe/Berlin', '2010-10-22 01:59:59', 'Europe/Berlin', - null, + false, + false, 'null', 'Europe/Moscow', ), @@ -514,26 +532,29 @@ public static function getDateRangeData() '2010-10-22 01:59:59', 'Europe/Berlin', true, + true, 'null', 'Europe/Moscow', ), array( - '[* TO 2010-10-22T03:59:59Z]', + '{* TO 2010-10-22T03:59:59Z}', null, null, '2010-10-22 01:59:59', 'Europe/Berlin', - null, + false, + false, 'null', 'Europe/Moscow' ), array( - '[2010-10-11T04:00:00Z TO *]', + '{2010-10-11T04:00:00Z TO *}', '2010-10-11 02:00:00', 'Europe/Berlin', null, null, - null, + false, + false, 'null', 'Europe/Moscow', ), @@ -544,6 +565,7 @@ public static function getDateRangeData() '2010-10-22 01:59:59', 'Europe/Berlin', false, + false, 'null', 'Europe/Moscow', ), @@ -554,6 +576,7 @@ public static function getDateRangeData() '2010-10-22 01:59:59', 'Europe/Berlin', false, + false, 'Europe/Berlin', 'Europe/Moscow', ), @@ -567,7 +590,8 @@ public function testDateRange( $fromTimezone, $to, $toTimezone, - $inclusive = null, + $inclusiveFrom = null, + $inclusiveTo = null, $solrTimezone = 'null', $defaultTimezone = null ) @@ -583,11 +607,13 @@ public function testDateRange( } $arguments = array($from, $to); - if ($inclusive !== null) { - $arguments[] = $inclusive; - - if ($solrTimezone !== 'null') { - $arguments[] = $solrTimezone; + if ($inclusiveFrom !== null) { + $arguments[] = $inclusiveFrom; + if ($inclusiveTo !== null) { + $arguments[] = $inclusiveTo; + if ($solrTimezone !== 'null') { + $arguments[] = $solrTimezone; + } } } @@ -598,8 +624,10 @@ public function testDateRange( public function testRange() { $this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z')); - $this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z', true)); - $this->assertSame('{"A" TO "Z"}', (string) $this->eb->range('A', 'Z', false)); + $this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z', true, true)); + $this->assertSame('{"A" TO "Z"}', (string) $this->eb->range('A', 'Z', false, false)); + $this->assertSame('{"A" TO "Z"]', (string) $this->eb->range('A', 'Z', false, true)); + $this->assertSame('["A" TO "Z"}', (string) $this->eb->range('A', 'Z', true, false)); } public function testFunc() diff --git a/tests/InterNations/Component/Solr/Tests/Expression/ExpressionTest.php b/tests/InterNations/Component/Solr/Tests/Expression/ExpressionTest.php index f971106..71bed0b 100644 --- a/tests/InterNations/Component/Solr/Tests/Expression/ExpressionTest.php +++ b/tests/InterNations/Component/Solr/Tests/Expression/ExpressionTest.php @@ -111,13 +111,15 @@ public function testProximityExpression() public function testRangeExpression() { - $this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', true)); + $this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', true, true)); + $this->assertSame('{"foo" TO "bar"}', (string) new RangeExpression('foo', 'bar', false, false)); + $this->assertSame('{"foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', false, true)); $this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar')); $this->assertSame('["foo" TO "foo bar"]', (string) new RangeExpression('foo', new PhraseExpression('foo bar'))); $this->assertSame('{"foo" TO "foo bar"}', (string) new RangeExpression('foo', new PhraseExpression('foo bar'), null, false)); $this->assertSame( '{"foo" TO "foo bar?"}', - (string) new RangeExpression('foo', new WildcardExpression('?', new PhraseExpression('foo bar')), false) + (string) new RangeExpression('foo', new WildcardExpression('?', new PhraseExpression('foo bar')), false, false) ); $this->assertSame('[-1 TO 0]', (string) new RangeExpression(-1, 0)); $this->assertSame('[0 TO 1]', (string) new RangeExpression(0, 1)); diff --git a/tests/InterNations/Component/Solr/Tests/Query/QueryStringTest.php b/tests/InterNations/Component/Solr/Tests/Query/QueryStringTest.php index b944526..ab26a16 100644 --- a/tests/InterNations/Component/Solr/Tests/Query/QueryStringTest.php +++ b/tests/InterNations/Component/Solr/Tests/Query/QueryStringTest.php @@ -72,9 +72,14 @@ public function testQueryWithPlaceholder_Boolean() public function testQueryWithPlaceholder_Expression() { $query = new QueryString('field:'); - $query->setPlaceholder('ph', new RangeExpression(0, 100, false)); - + $query->setPlaceholder('ph', new RangeExpression(0, 100, false, false)); $this->assertSame('field:{0 TO 100}', (string) $query); + $query = new QueryString('field:'); + $query->setPlaceholder('ph', new RangeExpression(0, 100, false, true)); + $this->assertSame('field:{0 TO 100]', (string) $query); + $query = new QueryString('field:'); + $query->setPlaceholder('ph', new RangeExpression(0, 100, true, false)); + $this->assertSame('field:[0 TO 100}', (string) $query); } public function testSetPlaceholders()