Commit f8a74094 authored by Dorian Villet's avatar Dorian Villet

Add strong type hinting on the DocumentInterface for 'getDocument(s)'-like methods.

parent bc946ef5
...@@ -38,9 +38,10 @@ ...@@ -38,9 +38,10 @@
*/ */
namespace Solarium\QueryType\Analysis\Query; namespace Solarium\QueryType\Analysis\Query;
use Solarium\Core\Client\Client; use Solarium\Core\Client\Client;
use Solarium\Exception\RuntimeException;
use Solarium\QueryType\Analysis\ResponseParser\Document as ResponseParser; use Solarium\QueryType\Analysis\ResponseParser\Document as ResponseParser;
use Solarium\QueryType\Analysis\RequestBuilder\Document as RequestBuilder; use Solarium\QueryType\Analysis\RequestBuilder\Document as RequestBuilder;
use Solarium\QueryType\Select\Result\Document as ResultDocument; use Solarium\QueryType\Select\Result\DocumentInterface;
/** /**
* Analysis document query * Analysis document query
...@@ -51,7 +52,7 @@ class Document extends Query ...@@ -51,7 +52,7 @@ class Document extends Query
/** /**
* Documents to analyze * Documents to analyze
* *
* @var ResultDocument[] * @var DocumentInterface[]
*/ */
protected $documents = array(); protected $documents = array();
...@@ -99,10 +100,10 @@ class Document extends Query ...@@ -99,10 +100,10 @@ class Document extends Query
/** /**
* Add a single document * Add a single document
* *
* @param object $document * @param DocumentInterface $document
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function addDocument($document) public function addDocument(DocumentInterface $document)
{ {
$this->documents[] = $document; $this->documents[] = $document;
...@@ -113,10 +114,17 @@ class Document extends Query ...@@ -113,10 +114,17 @@ class Document extends Query
* Add multiple documents * Add multiple documents
* *
* @param DocumentInterface[] $documents * @param DocumentInterface[] $documents
* @return self fluent interface * @return self Provides fluent interface
* @throws RuntimeException If any of the given documents does not implement DocumentInterface
*/ */
public function addDocuments($documents) public function addDocuments($documents)
{ {
foreach ($documents as $document) {
if (!($document instanceof DocumentInterface)) {
throw new RuntimeException('Documents must implement DocumentInterface.');
}
}
$this->documents = array_merge($this->documents, $documents); $this->documents = array_merge($this->documents, $documents);
return $this; return $this;
...@@ -125,7 +133,7 @@ class Document extends Query ...@@ -125,7 +133,7 @@ class Document extends Query
/** /**
* Get all documents * Get all documents
* *
* @return ResultDocument[] * @return DocumentInterface[]
*/ */
public function getDocuments() public function getDocuments()
{ {
......
...@@ -134,7 +134,7 @@ class Query extends BaseQuery ...@@ -134,7 +134,7 @@ class Query extends BaseQuery
* @param DocumentInterface $document * @param DocumentInterface $document
* @return self * @return self
*/ */
public function setDocument($document) public function setDocument(DocumentInterface $document)
{ {
return $this->setOption('document', $document); return $this->setOption('document', $document);
} }
......
...@@ -73,12 +73,8 @@ class Add extends Command ...@@ -73,12 +73,8 @@ class Add extends Command
* @param DocumentInterface $document * @param DocumentInterface $document
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function addDocument($document) public function addDocument(DocumentInterface $document)
{ {
if (!($document instanceof DocumentInterface)) {
throw new RuntimeException('Documents must implement the document interface');
}
$this->documents[] = $document; $this->documents[] = $document;
return $this; return $this;
...@@ -89,9 +85,16 @@ class Add extends Command ...@@ -89,9 +85,16 @@ class Add extends Command
* *
* @param array|\Traversable $documents * @param array|\Traversable $documents
* @return self Provides fluent interface * @return self Provides fluent interface
* @throws RuntimeException If any of the given documents does not implement DocumentInterface
*/ */
public function addDocuments($documents) public function addDocuments($documents)
{ {
foreach ($documents as $document) {
if (!($document instanceof DocumentInterface)) {
throw new RuntimeException('Documents must implement DocumentInterface.');
}
}
//if we don't have documents so far, accept arrays or Traversable objects as-is //if we don't have documents so far, accept arrays or Traversable objects as-is
if (empty($this->documents)) { if (empty($this->documents)) {
$this->documents = $documents; $this->documents = $documents;
......
...@@ -354,7 +354,7 @@ class Query extends BaseQuery ...@@ -354,7 +354,7 @@ class Query extends BaseQuery
* @param int $commitWithin * @param int $commitWithin
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function addDocument($document, $overwrite = null, public function addDocument(DocumentInterface $document, $overwrite = null,
$commitWithin = null) $commitWithin = null)
{ {
return $this->addDocuments(array($document), $overwrite, $commitWithin); return $this->addDocuments(array($document), $overwrite, $commitWithin);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment