Commit c14a5f2f authored by Bas de Nooijer's avatar Bas de Nooijer

Added assemble method to query helper

parent e8ee11b1
...@@ -47,6 +47,20 @@ ...@@ -47,6 +47,20 @@
class Solarium_Query_Helper class Solarium_Query_Helper
{ {
/**
* Placeholder pattern for use in the assemble method
*
* @var string
*/
protected $_placeHolderPattern = '/%(L|P|T|)([0-9]+)%/i';
/**
* Array of parts to use for assembling a query string
*
* @var array
*/
protected $_assembleParts;
/** /**
* Escape a term * Escape a term
* *
...@@ -218,4 +232,59 @@ class Solarium_Query_Helper ...@@ -218,4 +232,59 @@ class Solarium_Query_Helper
return $name . '(' . implode($params, ',') . ')'; return $name . '(' . implode($params, ',') . ')';
} }
/**
* Assemble a querystring with placeholders
*
* These placeholder modes are supported:
* %1% = no mode, will default to literal
* %L2% = literal
* %P3% = phrase-escaped
* %T4% = term-escaped
*
* Numbering starts at 1, so number 1 refers to the first entry
* of $parts (which has array key 0)
* You can use the same part multiple times, even in multiple modes.
* The mode letters are not case sensitive.
*
* The mode matching pattern can be customized by overriding the
* value of $this->_placeHolderPattern
*
* @param string $query
* @param array $parts Array of strings
* @return string
*/
public function assemble($query, $parts)
{
$this->_assembleParts = $parts;
return preg_replace_callback(
$this->_placeHolderPattern,
array($this, '_renderPlaceHolder'),
$query);
}
protected function _renderPlaceHolder($matches)
{
$partNumber = $matches[2];
$partMode = strtoupper($matches[1]);
if (isset($this->_assembleParts[$partNumber-1])) {
$value = $this->_assembleParts[$partNumber-1];
} else {
throw new Solarium_Exception('No value supplied for part #' . $partNumber . ' in query assembler');
}
switch($partMode)
{
case 'P':
$value = $this->escapePhrase($value);
break;
case 'T':
$value = $this->escapeTerm($value);
break;
}
return $value;
}
} }
\ No newline at end of file
...@@ -152,4 +152,43 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -152,4 +152,43 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
); );
} }
public function testAssemble()
{
// test single basic placeholder
$this->assertEquals(
'id:456 AND cat:2',
$this->_helper->assemble('id:%1% AND cat:2',array(456))
);
// test multiple basic placeholders and placeholder repeat
$this->assertEquals(
'(id:456 AND cat:2) OR (id:456 AND cat:1)',
$this->_helper->assemble('(id:%1% AND cat:%2%) OR (id:%1% AND cat:%3%)',array(456, 2, 1))
);
// test literal placeholder (same as basic)
$this->assertEquals(
'id:456 AND cat:2',
$this->_helper->assemble('id:%L1% AND cat:2',array(456))
);
// test term placeholder
$this->assertEquals(
'cat:2 AND content:a\\+b',
$this->_helper->assemble('cat:2 AND content:%T1%',array('a+b'))
);
// test term placeholder case-insensitive
$this->assertEquals(
'cat:2 AND content:a\\+b',
$this->_helper->assemble('cat:2 AND content:%t1%',array('a+b'))
);
// test phrase placeholder
$this->assertEquals(
'cat:2 AND content:"a+\\"b"',
$this->_helper->assemble('cat:2 AND content:%P1%',array('a+"b'))
);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment