Exemplo n.º 1
0
 public function testClearReturnsDocumentToDefaultState()
 {
     // set the document boost
     $this->_fixture->setBoost(0.5);
     // set a field
     $this->_fixture->someField = "some value";
     // clear the document to remove boost and fields
     $this->_fixture->clear();
     // document boost should now be false
     $this->assertFalse($this->_fixture->getBoost());
     // document fields should now be empty
     $this->assertEquals(0, count($this->_fixture->getFieldNames()));
     $this->assertEquals(0, count($this->_fixture->getFieldValues()));
     $this->assertEquals(0, count($this->_fixture->getFieldBoosts()));
     // document iterator should now be empty
     $this->assertEquals(0, iterator_count($this->_fixture));
 }
Exemplo n.º 2
0
 /**
  * Create an XML fragment from a {@link Apache_Solr_Document} instance appropriate for use inside a Solr add call
  *
  * @return string
  */
 protected function _documentToXmlFragment(Apache_Solr_Document $document)
 {
     $xml = '<doc';
     if ($document->getBoost() !== false) {
         $xml .= ' boost="' . $document->getBoost() . '"';
     }
     $xml .= '>';
     foreach ($document as $key => $value) {
         $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8');
         $fieldBoost = $document->getFieldBoost($key);
         if (is_array($value)) {
             foreach ($value as $multivalue) {
                 $xml .= '<field name="' . $key . '"';
                 if ($fieldBoost !== false) {
                     $xml .= ' boost="' . $fieldBoost . '"';
                     // only set the boost for the first field in the set
                     $fieldBoost = false;
                 }
                 $multivalue = htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8');
                 $xml .= '>' . $multivalue . '</field>';
             }
         } else {
             $xml .= '<field name="' . $key . '"';
             if ($fieldBoost !== false) {
                 $xml .= ' boost="' . $fieldBoost . '"';
             }
             $value = htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
             $xml .= '>' . $value . '</field>';
         }
     }
     $xml .= '</doc>';
     // replace any control characters to avoid Solr XML parser exception
     return $this->_stripCtrlChars($xml);
 }
 /**
  * Create an XML fragment from a {@link Apache_Solr_Document} instance appropriate for use inside a Solr add call
  *
  * @return string
  */
 protected function _documentToXmlFragment(Apache_Solr_Document $document)
 {
     $xml = '<doc';
     if ($document->getBoost() !== false) {
         $xml .= ' boost="' . $document->getBoost() . '"';
     }
     $xml .= '>';
     foreach ($document as $key => $value) {
         $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8');
         $fieldBoost = $document->getFieldBoost($key);
         if ($key) {
             if (is_array($value)) {
                 foreach ($value as $multivalue) {
                     $xml .= '<field name="' . $key . '"';
                     if ($fieldBoost !== false) {
                         $xml .= ' boost="' . $fieldBoost . '"';
                         // only set the boost for the first field in the set
                         $fieldBoost = false;
                     }
                     if (!mb_check_encoding($multivalue, 'UTF-8')) {
                         $multivalue = utf8_encode($multivalue);
                     }
                     $multivalue = htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8');
                     $xml .= '>' . $multivalue . '</field>';
                 }
             } else {
                 $xml .= '<field name="' . $key . '"';
                 if ($fieldBoost !== false) {
                     $xml .= ' boost="' . $fieldBoost . '"';
                 }
                 if (!mb_check_encoding($value, 'UTF-8')) {
                     $value = utf8_encode($value);
                 }
                 $value = htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
                 $xml .= '>' . $value . '</field>';
             }
         }
     }
     $xml .= '</doc>';
     return $xml;
 }