Source for file Document.php

Documentation is available at Document.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2007 Conduit Internet Technologies, Inc. (http://conduit-it.com)
  4.  * @license Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
  5.  *
  6.  *  Licensed under the Apache License, Version 2.0 (the "License");
  7.  *  you may not use this file except in compliance with the License.
  8.  *  You may obtain a copy of the License at
  9.  *
  10.  *      http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  *  Unless required by applicable law or agreed to in writing, software
  13.  *  distributed under the License is distributed on an "AS IS" BASIS,
  14.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  *  See the License for the specific language governing permissions and
  16.  *  limitations under the License.
  17.  *
  18.  * @package Apache
  19.  * @subpackage Solr
  20.  * @author Donovan Jimenez <djimenez@conduit-it.com>
  21.  */
  22.  
  23. /**
  24.  * Holds Key / Value pairs that represent a Solr Document. Field values can be accessed
  25.  * by direct dereferencing such as:
  26.  * <code>
  27.  * ...
  28.  * $document->title = 'Something';
  29.  * echo $document->title;
  30.  * ...
  31.  * </code>
  32.  *
  33.  * Additionally, the field values can be iterated with foreach
  34.  *
  35.  * <code>
  36.  * foreach ($document as $key => $value)
  37.  * {
  38.  * ...
  39.  * }
  40.  * </code>
  41.  */
  42. class Apache_Solr_Document implements Iterator
  43. {
  44.     protected $_fields = array();
  45.  
  46.     /**
  47.      * Magic get for field values
  48.      *
  49.      * @param string $key 
  50.      * @return mixed 
  51.      */
  52.     public function __get($key)
  53.     {
  54.         return $this->_fields[$key];
  55.     }
  56.  
  57.     /**
  58.      * Magic set for field values. Multi-valued fields should be set as arrays
  59.      * or instead use the setMultiValue(...) function which will automatically
  60.      * make sure the field is an array.
  61.      *
  62.      * @param string $key 
  63.      * @param mixed $value 
  64.      */
  65.     public function __set($key$value)
  66.     {
  67.         $this->_fields[$key$value;
  68.     }
  69.  
  70.     /**
  71.      * Magic isset for fields values.  Do no call directly. Allows usage:
  72.      *
  73.      * <code>
  74.      * isset($document->some_field);
  75.      * </code>
  76.      *
  77.      * @param string $key 
  78.      * @return boolean 
  79.      */
  80.     public function __isset($key)
  81.     {
  82.         return isset($this->_fields[$key]);
  83.     }
  84.  
  85.     /**
  86.      * Magic unset for field values. Do no call directly. Allows usage:
  87.      *
  88.      * <code>
  89.      * unset($document->some_field);
  90.      * </code>
  91.      *
  92.      * @param string $key 
  93.      */
  94.     public function __unset($key)
  95.     {
  96.         unset($this->_fields[$key]);
  97.     }
  98.  
  99.     /**
  100.      * Handle the array manipulation for a multi-valued field
  101.      *
  102.      * @param string $key 
  103.      * @param string $value 
  104.      */
  105.     public function setMultiValue($key$value)
  106.     {
  107.         if (!isset($this->_fields[$key]))
  108.         {
  109.             $this->_fields[$keyarray();
  110.         }
  111.  
  112.         if (!is_array($this->_fields[$key]))
  113.         {
  114.             $this->_fields[$keyarray($this->_fields[$key]);
  115.         }
  116.  
  117.         $this->_fields[$key][$value;
  118.     }
  119.  
  120.     /**
  121.      * Get the names of all fields in this document
  122.      *
  123.      * @return array 
  124.      */
  125.     public function getFieldNames()
  126.     {
  127.         return array_keys($this->_fields);
  128.     }
  129.  
  130.     /**
  131.      * Iterator implementation function, proxies to _fields. Allows usage:
  132.      *
  133.      * <code>
  134.      * foreach ($document as $key => $value)
  135.      * {
  136.      *     ...
  137.      * }
  138.      * </code>
  139.      */
  140.     public function rewind({
  141.         reset($this->_fields);
  142.     }
  143.  
  144.     /**
  145.      * Iterator implementation function, proxies to _fields. Allows usage:
  146.      *
  147.      * <code>
  148.      * foreach ($document as $key => $value)
  149.      * {
  150.      *     ...
  151.      * }
  152.      * </code>
  153.      */
  154.     public function current({
  155.         return current($this->_fields);
  156.     }
  157.  
  158.     /**
  159.      * Iterator implementation function, proxies to _fields. Allows usage:
  160.      *
  161.      * <code>
  162.      * foreach ($document as $key => $value)
  163.      * {
  164.      *     ...
  165.      * }
  166.      * </code>
  167.      */
  168.     public function key({
  169.         return key($this->_fields);
  170.     }
  171.  
  172.     /**
  173.      * Iterator implementation function, proxies to _fields. Allows usage:
  174.      *
  175.      * <code>
  176.      * foreach ($document as $key => $value)
  177.      * {
  178.      *     ...
  179.      * }
  180.      * </code>
  181.      */
  182.     public function next({
  183.         return next($this->_fields);
  184.     }
  185.  
  186.     /**
  187.      * Iterator implementation function, proxies to _fields. Allows usage:
  188.      *
  189.      * <code>
  190.      * foreach ($document as $key => $value)
  191.      * {
  192.      *     ...
  193.      * }
  194.      * </code>
  195.      */
  196.     public function valid({
  197.         return current($this->_fields!== false;
  198.     }
  199. }

Documentation generated on Tue, 02 Sep 2008 10:45:04 -0400 by phpDocumentor 1.4.0