/**
  * Initializes default values.
  *
  * @param xfField $field The field
  * @param string $value The value
  * @param string $encoding The encoding (optional)
  */
 public function __construct(xfField $field, $value, $encoding = 'utf8')
 {
     $this->field = $field;
     $this->value = $field->transformValue($value);
     $this->encoding = $encoding;
 }
$response = $engine->rewriteDocument($doc);
$t->isa_ok($response, 'Zend_Search_Lucene_Document', '->rewriteDocument() creates a Zend_Search_Lucene_Document');
$t->is($response->getFieldValue('__guid'), 'guid', '->rewriteDocument() writes the GUID correctly');
$fields = array(xfField::STORED => 'isStored', xfField::INDEXED => 'isIndexed', xfField::TOKENIZED => 'isTokenized', xfField::BINARY => 'isBinary');
foreach ($fields as $type => $property) {
    $name = 'type' . $type;
    $doc->addField(new xfFieldValue(new xfField($name, $type), 'bar'));
    $response = $engine->rewriteDocument($doc)->getField($name);
    $t->ok($response->{$property}, '->rewriteDocument() can handle "' . $property . '"');
    $others = $fields;
    unset($others[$type]);
    foreach ($others as $notproperty) {
        $t->ok(!$response->{$notproperty}, '->rewriteDocument() does not mark "' . $notproperty . '" with "' . $property . '"');
    }
}
$field = new xfField('foo', xfField::KEYWORD);
$field->setBoost(4);
$doc->addField(new xfFieldValue($field, 'bar', 'ascii'));
$field = $engine->rewriteDocument($doc)->getField('foo');
$t->is($field->name, 'foo', '->rewriteDocument() rewrites the name');
$t->is($field->value, 'bar', '->rewriteDocument() rewrites the value');
$t->is($field->encoding, 'ascii', '->rewriteDocument() rewrites the encoding');
$t->is($field->boost, 4, '->rewriteDocument() rewrites the boost');
$child = new xfDocument('child');
$doc->addChild($child);
$t->is($engine->rewriteDocument($doc)->getField('__sub_documents')->value, serialize(array('child')), '->rewriteDocument() caches child GUID');
$t->diag('->unwriteDocument()');
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Keyword('__guid', 'guid'));
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('__boosts', serialize(array())));
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('__sub_documents', serialize(array())));
<?php

/**
 * This file is part of the sfSearch package.
 * (c) Carl Vondrick <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require dirname(__FILE__) . '/../../bootstrap/unit.php';
require 'document/xfFieldValue.class.php';
require 'document/xfField.class.php';
$t = new lime_test(3, new lime_output_color());
$field = new xfField('name', xfField::KEYWORD);
$field->registerCallback('md5');
$value = new xfFieldValue($field, 'value', 'utf8');
$t->is($value->getField(), $field, '->getField() returns the field');
$t->is($value->getValue(), md5('value'), '->getValue() returns the transformed value');
$t->is($value->getEncoding(), 'utf8', '->getEncoding() returns the encoding');
        $t->pass($msg);
    } catch (Exception $e) {
        $t->fail($constructorMsg);
        $t->skip($msg);
    }
}
foreach ($invalidTypes as $type) {
    $msg = '->__construct() rejects the invalid type ' . $type;
    try {
        $field = new xfField('foobar', $type);
        $t->fail($msg);
    } catch (Exception $e) {
        $t->pass($msg);
    }
}
$t->diag('->getName(), ->getType()');
$field = new xfField('foobar', xfField::KEYWORD);
$t->is($field->getName(), 'foobar', '->getName() returns the name');
$t->is($field->getType(), xfField::KEYWORD, '->getType() returns the type');
$t->diag('->registerCallback(), ->getCallbacks(), ->transformValue()');
$field = new xfField('foobar', xfField::KEYWORD);
$field->registerCallback('strtoupper');
$field->registerCallback('md5');
$t->is($field->transformValue('foobar'), md5(strtoupper('foobar')), '->transformValue() calls callbacks in registered order');
$t->diag('->setBoost(), ->getBoost()');
$field = new xfField('foobar', xfField::KEYWORD);
$t->is($field->getBoost(), 1.0, '->getBoost() is 1.0 initially');
$field->setBoost(M_PI);
$t->is($field->getBoost(), M_PI, '->setBoost() changes the boost');
$field->setBoost('42foobar');
$t->is($field->getBoost(), 42, '->setBoost() casts the input to a float');
 /**
  * Unrewrites a Zend_Search_Lucene document into a xfDocument
  *
  * @param Zend_Search_Lucene_Document $zdoc
  * @returns xfDocument
  */
 public function unwriteDocument(Zend_Search_Lucene_Document $zdoc)
 {
     $doc = new xfDocument($zdoc->getFieldValue('__guid'));
     $boosts = unserialize($zdoc->getFieldValue('__boosts'));
     foreach ($zdoc->getFieldNames() as $name) {
         // ignore internal fields
         if (substr($name, 0, 2) != '__') {
             $zfield = $zdoc->getField($name);
             $type = 0;
             if ($zfield->isStored) {
                 $type |= xfField::STORED;
             }
             if ($zfield->isIndexed) {
                 $type |= xfField::INDEXED;
             }
             if ($zfield->isTokenized) {
                 $type |= xfField::TOKENIZED;
             }
             if ($zfield->isBinary) {
                 $type |= xfField::BINARY;
             }
             $field = new xfField($name, $type);
             $field->setBoost($boosts[$name]);
             $value = new xfFieldValue($field, $zfield->value);
             $doc->addField($value);
         }
     }
     foreach (unserialize($zdoc->getFieldValue('__sub_documents')) as $guid) {
         $doc->addChild($this->findGuid($guid));
     }
     return $doc;
 }