Commit a3646929 authored by Bas de Nooijer's avatar Bas de Nooijer

Implemented interfaces for documents

parent b3963553
......@@ -59,6 +59,11 @@ class ResponseParser implements ResponseParserInterface
// create document instances
$documentClass = $query->getOption('documentclass');
$classes = class_implements($documentClass);
if (!in_array('Solarium\Query\Select\Result\DocumentInterface',$classes) && !in_array('Solarium\Query\Update\Query\DocumentInterface',$classes)) {
throw new \Solarium\Core\Exception('The result document class must implement a document interface');
}
$documents = array();
if (isset($data['response']['docs'])) {
foreach ($data['response']['docs'] as $doc) {
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Query\Select\Result;
use Solarium\Core\Exception;
/**
* Document base functionality, used by readonly and readwrite documents
*/
abstract class AbstractDocument implements \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* All fields in this document
*
* @var array
*/
protected $fields;
/**
* Get all fields
*
* @return array
*/
public function getFields()
{
return $this->fields;
}
/**
* Get field value by name
*
* Magic access method for accessing fields as properties of this document
* object, by field name.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if (!isset($this->fields[$name])) {
return null;
}
return $this->fields[$name];
}
/**
* IteratorAggregate implementation
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->fields);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->fields);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return ($this->__get($offset) !== null);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
$this->__set($offset, null);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}
}
......@@ -45,7 +45,7 @@ use Solarium\Core\Exception;
* This is the default Solr document type returned by a select query. You can
* access the fields as object properties or iterate over all fields.
*/
class Document implements \IteratorAggregate, \Countable, \ArrayAccess
class Document extends AbstractDocument implements DocumentInterface
{
/**
......@@ -65,34 +65,6 @@ class Document implements \IteratorAggregate, \Countable, \ArrayAccess
$this->fields = $fields;
}
/**
* Get all fields
*
* @return array
*/
public function getFields()
{
return $this->fields;
}
/**
* Get field value by name
*
* Magic access method for accessing fields as properties of this document
* object, by field name.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if (!isset($this->fields[$name])) {
return null;
}
return $this->fields[$name];
}
/**
* Set field value
*
......@@ -108,69 +80,4 @@ class Document implements \IteratorAggregate, \Countable, \ArrayAccess
throw new Exception('A readonly document cannot be altered');
}
/**
* IteratorAggregate implementation
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->fields);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->fields);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return ($this->__get($offset) !== null);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
$this->__set($offset, null);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}
}
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Query\Select\Result;
/**
* Solr result document interface
*/
interface DocumentInterface
{
/**
* Constructor
*
* @param array $fields
*/
public function __construct(array $fields);
}
......@@ -38,7 +38,7 @@
*/
namespace Solarium\Query\Update\Query\Command;
use Solarium\Query\Update\Query\Query as UpdateQuery;
use Solarium\Query\Update\Query\DocumentInterface;
/**
* Update query add command
*
......@@ -67,11 +67,15 @@ class Add extends Command
/**
* Add a single document
*
* @param object $document
* @param DocumentInterface $document
* @return self Provides fluent interface
*/
public function addDocument($document)
{
if (!($document instanceof DocumentInterface)) {
throw new \Solarium\Core\Exception('Documents must implement the document interface');
}
$this->documents[] = $document;
return $this;
......
......@@ -37,7 +37,7 @@
* @namespace
*/
namespace Solarium\Query\Update\Query;
use Solarium\Query\Select\Result\Document as ReadOnlyDocument;
use Solarium\Query\Select\Result\AbstractDocument;
/**
* Read/Write Solr document
......@@ -52,7 +52,7 @@ use Solarium\Query\Select\Result\Document as ReadOnlyDocument;
* stored. You will loose that data because it is impossible to retrieve it from
* Solr. Always update from the original data source.
*/
class Document extends ReadOnlyDocument
class Document extends AbstractDocument implements DocumentInterface
{
/**
......@@ -77,7 +77,7 @@ class Document extends ReadOnlyDocument
* @param array $fields
* @param array $boosts
*/
public function __construct($fields = array(), $boosts = array())
public function __construct(array $fields = array(), array $boosts = array())
{
$this->fields = $fields;
$this->fieldBoosts = $boosts;
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Query\Update\Query;
/**
* Solr update document interface
*/
interface DocumentInterface
{
/**
* Constructor
*
* @param array $fields
*/
public function __construct(array $fields, array $boosts);
}
......@@ -84,6 +84,39 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($components, $result['components']);
}
public function testParseWithInvalidDocumentClass()
{
$data = array(
'response' => array(
'docs' => array(
array('fieldA' => 1, 'fieldB' => 'Test'),
array('fieldA' => 2, 'fieldB' => 'Test2')
),
'numFound' => 503
),
'responseHeader' => array(
'status' => 1,
'QTime' => 13,
)
);
$query = new Query(array('documentclass' => 'StdClass'));
$query->getFacetSet();
$resultStub = $this->getMock('Solarium\Query\Select\Result\Result', array(), array(), '', false);
$resultStub->expects($this->once())
->method('getData')
->will($this->returnValue($data));
$resultStub->expects($this->once())
->method('getQuery')
->will($this->returnValue($query));
$parser = new ResponseParser();
$this->setExpectedException('Solarium\Core\Exception');
$parser->parse($resultStub);
}
public function testParseWithoutNumFound()
{
$data = array(
......
......@@ -61,6 +61,13 @@ class AddTest extends \PHPUnit_Framework_TestCase
);
}
public function testAddDocumentWithInvalidDocument()
{
$doc = new \StdClass();
$this->setExpectedException('Solarium\Core\Exception');
$this->command->addDocument($doc);
}
public function testAddDocuments()
{
$doc1 = new Document(array('id' => 1));
......
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