Question

I want to pass a row object to a function that uses the configuration information stored in the row to build a Zend Search Lucene Document and add it to my index.

The information stored looks like this:

protected $_index = array(
    'url' => array('UnIndexed', 'getUrl()'),
    'fragment' => array('UnIndexed', 'getFragment()'),
    'edited' => array('UnIndexed', 'edited'),
    'title' => array('Text', 'getTitle()'),
    'summary' => array('Text', 'getSummary()'),
    'text' => array('UnStored', 'getText()'),
);

Here's the function I call:

public static function addDoc(My_Db_Table_Row_Abstract $row)
{
    $config = $row->getIndexConfig();
    if (empty($config)) {
        return;
    }

    // setup the document
    $document = new Zend_Search_Lucene_Document();
    $document->addField(Zend_Search_Lucene_Field::keyword('table_name', $row->getTable()->info('name')))
            ->addField(Zend_Search_Lucene_Field::keyword('table_row_key', $row->getIndexRowKey()));

    // add the configured fields
    foreach ($config as $name => $opts) {
        $type = $opts[0];
        $value = eval('return $row->' . $opts[1]);
        switch ($type) {
            case 'Keyword' :
                $field = Zend_Search_Lucene_Field::keyword($name, $value);
                break;
            case 'UnIndexed' :
                $field = Zend_Search_Lucene_Field::unIndexed($name, $value);
                break;
            case 'Binary' :
                $field = Zend_Search_Lucene_Field::binary($name, $value);
                break;
            case 'Text' :
                $field = Zend_Search_Lucene_Field::text($name, $value);
                break;
            case 'UnStored ' :
                $field = Zend_Search_Lucene_Field::unStored($name, $value);
                break;
        }
        $document->addField($field);
    }

    // add to the index
    self::getIndex()->addDocument($document);
}

As you can see, I need to get information via function calls into the document fields. I am concerned about using eval(), but I don't know another way. Is there a better way to accomplish the same functionality?


SOLUTION

I changed the configuration data to use functions for all the fields (removing the parenthesis), and lower-cased the first letter of the type so I could use that for call_user_func as well:

protected $_index = array(
    'url' => array('unIndexed', 'getUrl'),
    'fragment' => array('unIndexed', 'getFragment'),
    'edited' => array('unIndexed', 'getEdited'),
    'active_self' => array('keyword', 'isActive'),
    'active_block' => array('keyword', 'isActiveBlock'),
    'active_page' => array('keyword', 'isActiveNav'),
    'members_only' => array('keyword', 'isMembersOnly'),
    'title' => array('text', 'getTitle'),
    'summary' => array('text', 'getSummary'),
    'text' => array('unStored', 'getText'),
);

And the function call, which ended up being much simpler:

public static function addDoc(My_Db_Table_Row_Abstract $row)
{
    $config = $row->getIndexConfig();
    if (empty($config)) {
        return;
    }

    // setup the document
    $document = new Zend_Search_Lucene_Document();
    $document->addField(Zend_Search_Lucene_Field::keyword('table_name', $row->getTable()->info('name')))
            ->addField(Zend_Search_Lucene_Field::keyword('table_row_key', $row->getIndexRowKey()));

    // add the configured fields
    foreach ($config as $name => $opts) {
        $value = call_user_func(array($row, $opts[1]));
        $field = call_user_func(array('Zend_Search_Lucene_Field', $opts[0]), $name, $value);
        $document->addField($field);
    }

    // add to the index
    self::getIndex()->addDocument($document);
}
Was it helpful?

Solution

You can also use call_user_func. Here is the PHP doc.

Example:

<?php
function barber($type)
{
    echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>

Output:

 You wanted a mushroom haircut, no problem
 You wanted a shave haircut, no problem

For you example, you need to change :

$value = eval('return $row->' . $opts[1]);

by

$value = call_user_func($row->' . $opts[1]);

OTHER TIPS

I guess you can do:

// ...
$type = $opts[0];
$method = $opts[1];
$value = $row->$method;

Or:

// ...
$type = $opts[0];
call_user_func(array($row, $opts[1]));

Remove the parens from your method names:

protected $_index = array(
    'url' => array('UnIndexed', 'getUrl'),
    'fragment' => array('UnIndexed', 'getFragment'),
    ...

Replace this attribute:

'edited' => array('UnIndexed', 'edited'),

With an equivalent getter method:

'edited' => array('UnIndexed', 'getEdited'),

Then just do this:

$method = $opts[1];
$value = $row->$method();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top