Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
c14a5f2f
Commit
c14a5f2f
authored
Jul 20, 2011
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added assemble method to query helper
parent
e8ee11b1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
0 deletions
+108
-0
library/Solarium/Query/Helper.php
library/Solarium/Query/Helper.php
+69
-0
tests/Solarium/Query/HelperTest.php
tests/Solarium/Query/HelperTest.php
+39
-0
No files found.
library/Solarium/Query/Helper.php
View file @
c14a5f2f
...
@@ -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
tests/Solarium/Query/HelperTest.php
View file @
c14a5f2f
...
@@ -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'
))
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment