Skip to content

Commit 932015e

Browse files
committed
Merge pull request #837 from ruflin/php7-compatibility
PHP7 Compatibility: Rename Bool to BoolQuery and BoolFilter
2 parents 18aaa81 + e5afccb commit 932015e

File tree

17 files changed

+419
-268
lines changed

17 files changed

+419
-268
lines changed

.travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ matrix:
1515
- php: hhvm
1616
fast_finish: true
1717

18-
env:
19-
matrix:
20-
2118
install:
2219
- /bin/bash ansible/provision.sh
2320

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ All notable changes to this project will be documented in this file based on the
1111
- Support for retrieving id node
1212
- Scroll Iterator [#842](https://github.com/ruflin/Elastica/issues/842/)
1313
- Gitter Elastica Chat Room add for Elastica discussions: https://gitter.im/ruflin/Elastica
14+
- Introduce PHP7 compatibility and tests.
1415

1516

1617
### Improvements
1718
- Introduction of Changelog standard based on http://keepachangelog.com/. changes.txt moved to CHANGELOG.md [#844](https://github.com/ruflin/Elastica/issues/844/)
1819
- Make host for all tests dynamic to prepare it for a more dynamic test environment #846
1920
- Node information is retrieved based on id instead of name as multiple nodes can have the same name.
20-
- Remote NO_DEV builds from travis build matrix to speed up building. All builds include no dev packages.
21+
- Guzzle Http dependency updated to 5.3.*
22+
- Remove NO_DEV builds from travis build matrix to speed up building. All builds include no dev packages.
2123

2224
### Backward Compatibility Breaks
2325
- `Elastica\ScanAndScroll::$_lastScrollId` removed: `key()` now always returns the next scroll id [#842](https://github.com/ruflin/Elastica/issues/842/)
@@ -26,6 +28,7 @@ All notable changes to this project will be documented in this file based on the
2628
### Deprecated
2729
- Facets are deprecated. You are encouraged to migrate to aggregations instead. [#855](https://github.com/ruflin/Elastica/pull/855/)
2830
- Elastica\Query\Builder is deprecated. Use new Elastica\QueryBuilder instead. [#855](https://github.com/ruflin/Elastica/pull/855/)
31+
- For PHP 7 compatibility Elastica\Query\Bool was renamed to *\BoolQuery, Elastica\Filter\Bool was renamed to BoolFilter, Elastica\Transport\Null was renamed to NullTransport as Null and Bool are reserved phrases in PHP 7. Proxy objects for all three exist to keep backward compatibility. It is recommended to start using the new objects as the proxy classes will be deprecated as soon as PHP 7 is stable.
2932

3033

3134

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"suggest": {
2323
"munkie/elasticsearch-thrift-php": "Allow using thrift transport",
24-
"guzzlehttp/guzzle": "Allow using guzzle 4.x as the http transport (Requires php 5.4)",
24+
"guzzlehttp/guzzle": "Allow using guzzle 5.3.x as the http transport (Requires php 5.4)",
2525
"egeloen/http-adapter": "Allow using httpadapter transport",
2626
"monolog/monolog": "Logging request"
2727
},

lib/Elastica/Document.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Elastica\Bulk\Action;
66
use Elastica\Exception\InvalidException;
77
use Elastica\Exception\NotImplementedException;
8-
use Elastica\Filter\Bool;
8+
use Elastica\Filter\BoolFilter;
99

1010
/**
1111
* Single document stored in elastic search

lib/Elastica/Filter/Bool.php

+4-115
Original file line numberDiff line numberDiff line change
@@ -2,130 +2,19 @@
22

33
namespace Elastica\Filter;
44

5-
use Elastica\Exception\InvalidException;
5+
use Elastica\Filter\BoolFilter;
66

77
/**
88
* Bool Filter
99
*
10+
* This class is for backward compatibility reason for all php < 7 versions. For PHP 7 and above use BoolFilter as Bool is reserved.
11+
*
1012
* @category Xodoa
1113
* @package Elastica
1214
* @author Nicolas Ruflin <[email protected]>
1315
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html
1416
*/
15-
class Bool extends AbstractFilter
17+
class Bool extends BoolFilter
1618
{
17-
/**
18-
* Must
19-
*
20-
* @var array
21-
*/
22-
protected $_must = array();
23-
24-
/**
25-
* Should
26-
*
27-
* @var array
28-
*/
29-
protected $_should = array();
30-
31-
/**
32-
* Must not
33-
*
34-
* @var array
35-
*/
36-
protected $_mustNot = array();
37-
38-
/**
39-
* Adds should filter
40-
*
41-
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
42-
* @return $this
43-
*/
44-
public function addShould($args)
45-
{
46-
return $this->_addFilter('should', $args);
47-
}
48-
49-
/**
50-
* Adds must filter
51-
*
52-
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
53-
* @return $this
54-
*/
55-
public function addMust($args)
56-
{
57-
return $this->_addFilter('must', $args);
58-
}
59-
60-
/**
61-
* Adds mustNot filter
62-
*
63-
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
64-
* @return $this
65-
*/
66-
public function addMustNot($args)
67-
{
68-
return $this->_addFilter('mustNot', $args);
69-
}
70-
71-
/**
72-
* Adds general filter based on type
73-
*
74-
* @throws \Elastica\Exception\InvalidException
75-
*
76-
* @param string $type Filter type
77-
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
78-
* @return $this
79-
*/
80-
protected function _addFilter($type, $args)
81-
{
82-
if ($args instanceof AbstractFilter) {
83-
$args = $args->toArray();
84-
} elseif (!is_array($args)) {
85-
throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Filter');
86-
} else {
87-
$parsedArgs = array();
88-
foreach ($args as $filter) {
89-
if ($filter instanceof AbstractFilter) {
90-
$parsedArgs[] = $filter->toArray();
91-
}
92-
}
93-
$args = $parsedArgs;
94-
}
95-
96-
$varName = '_'.$type;
97-
$this->{$varName}[] = $args;
98-
99-
return $this;
100-
}
101-
102-
/**
103-
* Converts bool filter to array
104-
*
105-
* @see \Elastica\Filter\AbstractFilter::toArray()
106-
*
107-
* @return array Filter array
108-
*/
109-
public function toArray()
110-
{
111-
$args = array();
112-
113-
if (!empty($this->_must)) {
114-
$args['bool']['must'] = $this->_must;
115-
}
116-
117-
if (!empty($this->_should)) {
118-
$args['bool']['should'] = $this->_should;
119-
}
120-
121-
if (!empty($this->_mustNot)) {
122-
$args['bool']['must_not'] = $this->_mustNot;
123-
}
124-
125-
if (isset($args['bool'])) {
126-
$args['bool'] = array_merge($args['bool'], $this->getParams());
127-
}
12819

129-
return $args;
130-
}
13120
}

lib/Elastica/Filter/BoolFilter.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Elastica\Filter;
4+
5+
use Elastica\Exception\InvalidException;
6+
7+
/**
8+
* Bool Filter
9+
*
10+
* @category Xodoa
11+
* @package Elastica
12+
* @author Nicolas Ruflin <[email protected]>
13+
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html
14+
*/
15+
class BoolFilter extends AbstractFilter
16+
{
17+
/**
18+
* Must
19+
*
20+
* @var array
21+
*/
22+
protected $_must = array();
23+
24+
/**
25+
* Should
26+
*
27+
* @var array
28+
*/
29+
protected $_should = array();
30+
31+
/**
32+
* Must not
33+
*
34+
* @var array
35+
*/
36+
protected $_mustNot = array();
37+
38+
/**
39+
* Adds should filter
40+
*
41+
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
42+
* @return $this
43+
*/
44+
public function addShould($args)
45+
{
46+
return $this->_addFilter('should', $args);
47+
}
48+
49+
/**
50+
* Adds must filter
51+
*
52+
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
53+
* @return $this
54+
*/
55+
public function addMust($args)
56+
{
57+
return $this->_addFilter('must', $args);
58+
}
59+
60+
/**
61+
* Adds mustNot filter
62+
*
63+
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
64+
* @return $this
65+
*/
66+
public function addMustNot($args)
67+
{
68+
return $this->_addFilter('mustNot', $args);
69+
}
70+
71+
/**
72+
* Adds general filter based on type
73+
*
74+
* @throws \Elastica\Exception\InvalidException
75+
*
76+
* @param string $type Filter type
77+
* @param array|\Elastica\Filter\AbstractFilter $args Filter data
78+
* @return $this
79+
*/
80+
protected function _addFilter($type, $args)
81+
{
82+
if ($args instanceof AbstractFilter) {
83+
$args = $args->toArray();
84+
} elseif (!is_array($args)) {
85+
throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Filter');
86+
} else {
87+
$parsedArgs = array();
88+
foreach ($args as $filter) {
89+
if ($filter instanceof AbstractFilter) {
90+
$parsedArgs[] = $filter->toArray();
91+
}
92+
}
93+
$args = $parsedArgs;
94+
}
95+
96+
$varName = '_'.$type;
97+
$this->{$varName}[] = $args;
98+
99+
return $this;
100+
}
101+
102+
/**
103+
* Converts bool filter to array
104+
*
105+
* @see \Elastica\Filter\AbstractFilter::toArray()
106+
*
107+
* @return array Filter array
108+
*/
109+
public function toArray()
110+
{
111+
$args = array();
112+
113+
if (!empty($this->_must)) {
114+
$args['bool']['must'] = $this->_must;
115+
}
116+
117+
if (!empty($this->_should)) {
118+
$args['bool']['should'] = $this->_should;
119+
}
120+
121+
if (!empty($this->_mustNot)) {
122+
$args['bool']['must_not'] = $this->_mustNot;
123+
}
124+
125+
if (isset($args['bool'])) {
126+
$args['bool'] = array_merge($args['bool'], $this->getParams());
127+
}
128+
129+
return $args;
130+
}
131+
}

0 commit comments

Comments
 (0)