*/
/**
* Holds Key / Value pairs that represent a Solr Document. Field values can be accessed
* by direct dereferencing such as:
*
* ...
* $document->title = 'Something';
* echo $document->title;
* ...
*
*
* Additionally, the field values can be iterated with foreach
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
class Apache_Solr_Document implements Iterator
{
protected $_fields = array();
/**
* Magic get for field values
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->_fields[$key];
}
/**
* Magic set for field values. Multi-valued fields should be set as arrays
* or instead use the setMultiValue(...) function which will automatically
* make sure the field is an array.
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
$this->_fields[$key] = $value;
}
/**
* Magic isset for fields values. Do no call directly. Allows usage:
*
*
* isset($document->some_field);
*
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return isset($this->_fields[$key]);
}
/**
* Magic unset for field values. Do no call directly. Allows usage:
*
*
* unset($document->some_field);
*
*
* @param string $key
*/
public function __unset($key)
{
unset($this->_fields[$key]);
}
/**
* Handle the array manipulation for a multi-valued field
*
* @param string $key
* @param string $value
*/
public function setMultiValue($key, $value)
{
if (!isset($this->_fields[$key]))
{
$this->_fields[$key] = array();
}
if (!is_array($this->_fields[$key]))
{
$this->_fields[$key] = array($this->_fields[$key]);
}
$this->_fields[$key][] = $value;
}
/**
* Get the names of all fields in this document
*
* @return array
*/
public function getFieldNames()
{
return array_keys($this->_fields);
}
/**
* Iterator implementation function, proxies to _fields. Allows usage:
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
public function rewind() {
reset($this->_fields);
}
/**
* Iterator implementation function, proxies to _fields. Allows usage:
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
public function current() {
return current($this->_fields);
}
/**
* Iterator implementation function, proxies to _fields. Allows usage:
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
public function key() {
return key($this->_fields);
}
/**
* Iterator implementation function, proxies to _fields. Allows usage:
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
public function next() {
return next($this->_fields);
}
/**
* Iterator implementation function, proxies to _fields. Allows usage:
*
*
* foreach ($document as $key => $value)
* {
* ...
* }
*
*/
public function valid() {
return current($this->_fields) !== false;
}
}