Skip to content

Commit 1955919

Browse files
committed
Moved ResultSet creation to a static factory method
1 parent a775593 commit 1955919

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

changes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CHANGES
22

3+
2014-10-04
4+
5+
- ResultSet creation moved to dedicated object
6+
37
2014-09-22
48
- Update the branch alias in composer.json to match the library version
59

lib/Elastica/ResultSet.php

+32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
class ResultSet implements \Iterator, \Countable, \ArrayAccess
1717
{
18+
/**
19+
* Class for the static create method to use.
20+
*
21+
* @var string
22+
*/
23+
protected static $class = 'Elastica\\ResultSet';
24+
1825
/**
1926
* Results
2027
*
@@ -76,6 +83,31 @@ public function __construct(Response $response, Query $query)
7683
$this->_query = $query;
7784
}
7885

86+
/**
87+
* Creates a new ResultSet object. Can be configured to return a different
88+
* implementation of the ResultSet class.
89+
*
90+
* @param Response $response
91+
* @param Query $query
92+
* @return ResultSet
93+
*/
94+
public static function create(Response $response, Query $query)
95+
{
96+
$class = static::$class;
97+
98+
return new $class($response, $query);
99+
}
100+
101+
/**
102+
* Sets the class to be used for the static create method.
103+
*
104+
* @param string $class
105+
*/
106+
public static function setClass($class)
107+
{
108+
static::$class = $class;
109+
}
110+
79111
/**
80112
* Loads all data into the results object (initialisation)
81113
*

lib/Elastica/Search.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function search($query = '', $options = null)
436436
$params
437437
);
438438

439-
return new ResultSet($response, $query);
439+
return ResultSet::create($response, $query);
440440
}
441441

442442
/**
@@ -458,7 +458,7 @@ public function count($query = '', $fullResult = false)
458458
$query->toArray(),
459459
array(self::OPTION_SEARCH_TYPE => self::OPTION_SEARCH_TYPE_COUNT)
460460
);
461-
$resultSet = new ResultSet($response, $query);
461+
$resultSet = ResultSet::create($response, $query);
462462

463463
return $fullResult ? $resultSet : $resultSet->getTotalHits();
464464
}

lib/Elastica/Type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public function moreLikeThis(Document $doc, $params = array(), $query = array())
492492

493493
$response = $this->request($path, Request::GET, $query->toArray(), $params);
494494

495-
return new ResultSet($response, $query);
495+
return ResultSet::create($response, $query);
496496
}
497497

498498
/**

0 commit comments

Comments
 (0)