From de913b9f99386caa1b7b7de60488f252b32459c5 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Sat, 4 Oct 2014 20:49:10 +1000 Subject: [PATCH] Moved ResultSet creation to a static factory method --- changes.txt | 3 +++ lib/Elastica/ResultSet.php | 32 ++++++++++++++++++++++++++++++++ lib/Elastica/Search.php | 4 ++-- lib/Elastica/Type.php | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/changes.txt b/changes.txt index 7d375fb867..66d57c7930 100644 --- a/changes.txt +++ b/changes.txt @@ -1,5 +1,8 @@ CHANGES +2014-10-05 + - ResultSet creation moved to static ResultSet::create() method + 2014-10-04 - Release v1.3.4.0 - Update to elasticsearch 1.3.4 #691 diff --git a/lib/Elastica/ResultSet.php b/lib/Elastica/ResultSet.php index 3bb94fb07a..64f8901a96 100644 --- a/lib/Elastica/ResultSet.php +++ b/lib/Elastica/ResultSet.php @@ -15,6 +15,13 @@ */ class ResultSet implements \Iterator, \Countable, \ArrayAccess { + /** + * Class for the static create method to use. + * + * @var string + */ + protected static $class = 'Elastica\\ResultSet'; + /** * Results * @@ -76,6 +83,31 @@ public function __construct(Response $response, Query $query) $this->_query = $query; } + /** + * Creates a new ResultSet object. Can be configured to return a different + * implementation of the ResultSet class. + * + * @param Response $response + * @param Query $query + * @return ResultSet + */ + public static function create(Response $response, Query $query) + { + $class = static::$class; + + return new $class($response, $query); + } + + /** + * Sets the class to be used for the static create method. + * + * @param string $class + */ + public static function setClass($class) + { + static::$class = $class; + } + /** * Loads all data into the results object (initialisation) * diff --git a/lib/Elastica/Search.php b/lib/Elastica/Search.php index 4163a1a27b..482e8afb6d 100644 --- a/lib/Elastica/Search.php +++ b/lib/Elastica/Search.php @@ -436,7 +436,7 @@ public function search($query = '', $options = null) $params ); - return new ResultSet($response, $query); + return ResultSet::create($response, $query); } /** @@ -458,7 +458,7 @@ public function count($query = '', $fullResult = false) $query->toArray(), array(self::OPTION_SEARCH_TYPE => self::OPTION_SEARCH_TYPE_COUNT) ); - $resultSet = new ResultSet($response, $query); + $resultSet = ResultSet::create($response, $query); return $fullResult ? $resultSet : $resultSet->getTotalHits(); } diff --git a/lib/Elastica/Type.php b/lib/Elastica/Type.php index 1ed952db87..c784eb76ae 100644 --- a/lib/Elastica/Type.php +++ b/lib/Elastica/Type.php @@ -492,7 +492,7 @@ public function moreLikeThis(Document $doc, $params = array(), $query = array()) $response = $this->request($path, Request::GET, $query->toArray(), $params); - return new ResultSet($response, $query); + return ResultSet::create($response, $query); } /**