Author: as
Date: Mon Aug 6 11:11:37 2007
New Revision: 5819
Log:
- Implemented feature request #10896: Allow aggregation of unordered parameter
values if the parameter names appear more than once.
Modified:
trunk/Url/docs/tutorial.txt
trunk/Url/docs/tutorial/tutorial_get_params_aggregate.php
trunk/Url/docs/tutorial/tutorial_set_params.php
trunk/Url/src/url.php
trunk/Url/src/url_configuration.php
trunk/Url/tests/url_test.php
Modified: trunk/Url/docs/tutorial.txt
==============================================================================
--- trunk/Url/docs/tutorial.txt [iso-8859-1] (original)
+++ trunk/Url/docs/tutorial.txt [iso-8859-1] Mon Aug 6 11:11:37 2007
@@ -209,9 +209,10 @@
'http://www.example.com/(param1)/x/(param1)/y/z'), then by default only the
last
encountered values are returned when calling getParam().
-To return all the values (to aggregate values), use
+To return all the values (to aggregate values) as an array of arrays (for
+example, array( array( 'x' ), array( 'y', 'z' ) ) for the above URL), use
ezcUrlConfiguration::AGGREGATE_ARGUMENTS as the second parameter when calling
-addUnorderedParameter(), like in the example:
+addUnorderedParameter(), like in this example:
.. include:: tutorial/tutorial_get_params_aggregate.php
:literal:
@@ -225,16 +226,21 @@
[1]=>
string(1) "z"
}
- array(3) {
+ array(2) {
[0]=>
- string(1) "x"
+ array(1) {
+ [0]=>
+ string(1) "x"
+ }
[1]=>
- string(1) "y"
- [2]=>
- string(1) "z"
+ array(2) {
+ [0]=>
+ string(1) "y"
+ [1]=>
+ string(1) "z"
+ }
}
string(46) "http://www.example.com/(param1)/x/(param1)/y/z"
-
Setting parameters using a url configuration
--------------------------------------------
@@ -252,6 +258,9 @@
string(79) "http://www.example.com/mydir/groups/Games/Adventure/Kids/
(game)/Monkey_Island/3"
+
+ string(113) "http://www.example.com/mydir/groups/Games/Adventure/Kids/
+ (game)/Monkey_Island/3/(patches)/beta1/(patches)/rc1/rc2"
Changing a url configuration dynamically
----------------------------------------
Modified: trunk/Url/docs/tutorial/tutorial_get_params_aggregate.php
==============================================================================
--- trunk/Url/docs/tutorial/tutorial_get_params_aggregate.php [iso-8859-1]
(original)
+++ trunk/Url/docs/tutorial/tutorial_get_params_aggregate.php [iso-8859-1] Mon
Aug 6 11:11:37 2007
@@ -17,7 +17,7 @@
// multiple parameter values with aggregation
$urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
$url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z', $urlCfg );
-var_dump( $url->getParam( 'param1' ) ); // will output array( "x", "y", "z" )
+var_dump( $url->getParam( 'param1' ) ); // will output array( array( "x" ),
array( "y", "z" ) )
// output the url (it will be similar to the input url)
var_dump( $url->buildUrl() );
Modified: trunk/Url/docs/tutorial/tutorial_set_params.php
==============================================================================
--- trunk/Url/docs/tutorial/tutorial_set_params.php [iso-8859-1] (original)
+++ trunk/Url/docs/tutorial/tutorial_set_params.php [iso-8859-1] Mon Aug 6
11:11:37 2007
@@ -19,6 +19,7 @@
// define unordered parameters
$urlCfg->addUnorderedParameter( 'game',
ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
+$urlCfg->addUnorderedParameter( 'patches',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
// create a new ezcUrl object from a string url and use the above $urlCfg
$url = new ezcUrl(
'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7',
$urlCfg );
@@ -29,4 +30,6 @@
$url->setParam( 'game', array( 'Monkey_Island', '3' ) );
var_dump( $url->buildUrl() );
+$url->setParam( 'patches', array( array( 'beta1' ), array( 'rc1', 'rc2' ) ) );
+var_dump( $url->buildUrl() );
?>
Modified: trunk/Url/src/url.php
==============================================================================
--- trunk/Url/src/url.php [iso-8859-1] (original)
+++ trunk/Url/src/url.php [iso-8859-1] Mon Aug 6 11:11:37 2007
@@ -42,6 +42,16 @@
* $category = $url->getParam( 'category' ); // will be "Adventure"
* $subcategory = $url->getParam( 'subcategory' ); // will be "Adult"
* $game = $url->getParam( 'game' ); // will be array( "Larry", "7" )
+ * </code>
+ *
+ * Example of aggregating values for unordered parameters:
+ * <code>
+ * $urlCfg = new ezcUrlConfiguration();
+ *
+ * $urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
+ * $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
+ *
+ * $param1 = $url->getParam( 'param1' ); // will be array( array( "x" ),
array( "y", "z" ) )
* </code>
*
* @property string $host
@@ -353,7 +363,7 @@
* $urlCfg = new ezcUrlConfiguration();
*
* // single parameter value
- * $urlCfg->addUnorderedParameter( 'param1' );
+ * $urlCfg->addUnorderedParameter( 'param1' ); // type is SINGLE_ARGUMENT
by default
* $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
* $param1 = $url->getParam( 'param1' ); // will return "y"
*
@@ -365,7 +375,7 @@
* // multiple parameter values with aggregation
* $urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
* $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
- * $param1 = $url->getParam( 'param1' ); // will return array( "x", "y",
"z" )
+ * $param1 = $url->getParam( 'param1' ); // will return array( array( "x"
), array( "y", "z" ) )
* </code>
*
* @param array(string) $config An array of unordered parameters names,
from the URL configuration used in parsing
@@ -537,7 +547,7 @@
* $urlCfg = new ezcUrlConfiguration();
*
* // single parameter value
- * $urlCfg->addUnorderedParameter( 'param1' );
+ * $urlCfg->addUnorderedParameter( 'param1' ); // type is SINGLE_ARGUMENT
by default
* $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
* $param1 = $url->getParam( 'param1' ); // will return "y"
*
@@ -549,7 +559,7 @@
* // multiple parameter values with aggregation
* $urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
* $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
- * $param1 = $url->getParam( 'param1' ); // will return array( "x", "y",
"z" )
+ * $param1 = $url->getParam( 'param1' ); // will return array( array( "x"
), array( "y", "z" ) )
* </code>
*
* Ordered parameter examples:
@@ -607,11 +617,7 @@
{
if ( $urlCfg->unorderedParameters[$name] ===
ezcUrlConfiguration::AGGREGATE_ARGUMENTS )
{
- $result = array();
- foreach ( $uparams[$name] as $encounter => $values )
- {
- $result = array_merge( $result,
$uparams[$name][$encounter] );
- }
+ $result = $uparams[$name];
return $result;
}
else
@@ -631,12 +637,45 @@
/**
* Sets the specified parameter in the URL based on the URL configuration.
*
+ * For ordered parameters, the value cannot be an array, otherwise an
+ * ezcBaseValueException will be thrown.
+ *
+ * For unordered parameters, the value can be one of:
+ * - string
+ * - array(string)
+ * - array(array(string))
+ *
+ * Any of these values can be assigned to an unordered parameter, whatever
the
+ * parameter type (SINGLE_ARGUMENT, MULTIPLE_ARGUMENTS,
AGGREGATE_ARGUMENTS).
+ *
+ * If there are ordered and unordered parameters with the same name, only
the
+ * ordered parameter value will be set.
+ *
+ * Examples:
+ * <code>
+ * $urlCfg = new ezcUrlConfiguration();
+ * $urlCfg->addUnorderedParameter( 'param1' );
+ *
+ * $url = new ezcUrl( 'http://www.example.com' );
+ *
+ * $url->setParam( 'param1', 'x' );
+ * echo $url->buildUrl(); // will output http://www.example.com/(param1)/x
+ *
+ * $url->setParam( 'param1', array( 'x', 'y' ) );
+ * echo $url->buildUrl(); // will output
http://www.example.com/(param1)/x/y
+ *
+ * $url->setParam( 'param1', array( array( 'x' ), array( 'y', 'z' ) ) );
+ * echo $url->buildUrl(); // will output
http://www.example.com/(param1)/x/(param1)/y/z
+ * </code>
+ *
+ * @throws ezcBaseValueException
+ * if trying to assign an array value to an ordered parameter
* @throws ezcUrlNoConfigurationException
* if an URL configuration is not defined
* @throws ezcUrlInvalidParameterException
* if the specified parameter is not defined in the URL
configuration
* @param string $name The name of the parameter to set
- * @param string $value The new value of the parameter
+ * @param string|array(string=>mixed) $value The new value of the parameter
*/
public function setParam( $name, $value )
{
@@ -651,9 +690,17 @@
if ( isset( $urlCfg->orderedParameters[$name] ) )
{
- $this->properties['params'][$urlCfg->orderedParameters[$name]]
= $value;
+ if ( !is_array( $value ) )
+ {
+
$this->properties['params'][$urlCfg->orderedParameters[$name]] = $value;
+ }
+ else
+ {
+ throw new ezcBaseValueException( $name, $value, 'string' );
+ }
return;
}
+
if ( isset( $urlCfg->unorderedParameters[$name] ) )
{
if ( !isset( $this->properties['uparams'][$name] ) )
@@ -663,7 +710,20 @@
if ( is_array( $value ) )
{
- $this->properties['uparams'][$name][count(
$this->properties['uparams'][$name] ) - 1] = $value;
+ $multiple = false;
+ foreach ( $value as $part )
+ {
+ if ( is_array( $part ) )
+ {
+ $this->properties['uparams'][$name] = $value;
+ $multiple = true;
+ break;
+ }
+ }
+ if ( !$multiple )
+ {
+ $this->properties['uparams'][$name][count(
$this->properties['uparams'][$name] ) - 1] = $value;
+ }
}
else
{
Modified: trunk/Url/src/url_configuration.php
==============================================================================
--- trunk/Url/src/url_configuration.php [iso-8859-1] (original)
+++ trunk/Url/src/url_configuration.php [iso-8859-1] Mon Aug 6 11:11:37 2007
@@ -56,9 +56,9 @@
* $urlCfg = new ezcUrlConfiguration();
*
* $urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
- * $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y', $urlCfg
);
- *
- * $param1 = $url->getParam( 'param1' ); // will be array( "x", "y" )
+ * $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
+ *
+ * $param1 = $url->getParam( 'param1' ); // will be array( array( "x" ),
array( "y", "z" ) )
* </code>
*
* @property string $basedir
@@ -105,8 +105,8 @@
*
* For example, if the URL is
'http://www.example.com/(param1)/x/(param1)/y/z',
* then all values will be considered for the parameter param1. So
- * $url->getParam( 'param1' ) will return array( "x", "y", "z" ), if $url
is
- * an ezcUrl object created from the above URL.
+ * $url->getParam( 'param1' ) will return array( array( "x" ), array( "y",
"z" ) ),
+ * if $url is an ezcUrl object created from the above URL.
*/
const AGGREGATE_ARGUMENTS = 4;
@@ -274,7 +274,7 @@
* $urlCfg = new ezcUrlConfiguration();
*
* // single parameter value
- * $urlCfg->addUnorderedParameter( 'param1' );
+ * $urlCfg->addUnorderedParameter( 'param1' ); // type is SINGLE_ARGUMENT
by default
* $url = new ezcUrl( 'http://www.example.com/(param1)/x', $urlCfg );
* $param1 = $url->getParam( 'param1' ); // will return "x"
*
@@ -285,8 +285,8 @@
*
* // multiple parameter values with aggregation
* $urlCfg->addUnorderedParameter( 'param1',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
- * $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y',
$urlCfg );
- * $param1 = $url->getParam( 'param1' ); // will return array( "x", "y" )
+ * $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z',
$urlCfg );
+ * $param1 = $url->getParam( 'param1' ); // will return array( array( "x"
), array( "y", "z" ) )
* </code>
*
* @param string $name The name of the unordered parameter to add to the
configuration
Modified: trunk/Url/tests/url_test.php
==============================================================================
--- trunk/Url/tests/url_test.php [iso-8859-1] (original)
+++ trunk/Url/tests/url_test.php [iso-8859-1] Mon Aug 6 11:11:37 2007
@@ -673,7 +673,7 @@
$this->assertEquals( 'view', $url->getParam( 'view' ) );
$this->assertEquals( 'trunk', $url->getParam( 'content' ) );
$this->assertEquals( 'a', $url->getParam( 'param1' ) );
- $this->assertEquals( array( 'x', 'y', 'z' ), $url->getParam( 'param2'
) );
+ $this->assertEquals( array( array( 'x' ), array( 'y', 'z' ) ),
$url->getParam( 'param2' ) );
}
public function
testGetUnorderedParametersMultipleValuesTypeAggregateSingleSingle()
@@ -695,7 +695,7 @@
$this->assertEquals( 'view', $url->getParam( 'view' ) );
$this->assertEquals( 'trunk', $url->getParam( 'content' ) );
$this->assertEquals( 'a', $url->getParam( 'param1' ) );
- $this->assertEquals( array( 'x', 'y' ), $url->getParam( 'param2' ) );
+ $this->assertEquals( array( array( 'x' ), array( 'y' ) ),
$url->getParam( 'param2' ) );
}
public function
testGetUnorderedParametersMultipleValuesTypeAggregateMultipleSingle()
@@ -717,7 +717,193 @@
$this->assertEquals( 'view', $url->getParam( 'view' ) );
$this->assertEquals( 'trunk', $url->getParam( 'content' ) );
$this->assertEquals( 'a', $url->getParam( 'param1' ) );
- $this->assertEquals( array( 'x', 'y', 'z' ), $url->getParam( 'param2'
) );
+ $this->assertEquals( array( array( 'x', 'y' ), array( 'z' ) ),
$url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterSingleArraySingleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2' );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( 'y', $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterSingleArraySingleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2' );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y/z';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y', 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( 'y', $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterSingleArrayMultipleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2' );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( 'z', $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterSingleArrayMultipleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2' );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z/t';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z', 't' )
) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( 'z', $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterMultipleArraySingleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( 'y' ), $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterMultipleArraySingleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y/z';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y', 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( 'y', 'z' ), $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterMultipleArrayMultipleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( 'z' ), $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterMultipleArrayMultipleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z/t';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z', 't' )
) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( 'z', 't' ), $url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterAggregateArraySingleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( array( 'x' ), array( 'y' ) ),
$url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterAggregateArraySingleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/(param2)/y/z';
+
+ $url->setParam( 'param2', array( array( 'x' ), array( 'y', 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( array( 'x' ), array( 'y', 'z' ) ),
$url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterAggregateArrayMultipleSingle()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z' ) ) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( array( 'x', 'y' ), array( 'z' ) ),
$url->getParam( 'param2' ) );
+ }
+
+ public function testSetUnorderedParameterAggregateArrayMultipleMultiple()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addUnorderedParameter( 'param2',
ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+ $expected = 'http://www.example.com/(param2)/x/y/(param2)/z/t';
+
+ $url->setParam( 'param2', array( array( 'x', 'y' ), array( 'z', 't' )
) );
+ $this->assertEquals( $expected, $url->buildUrl() );
+
+ $this->assertEquals( array( array( 'x', 'y' ), array( 'z', 't' ) ),
$url->getParam( 'param2' ) );
+ }
+
+ public function testSetOrderedParameterArrayFail()
+ {
+ $urlCfg = new ezcUrlConfiguration();
+ $urlCfg->addOrderedParameter( 'param2' );
+
+ $url = new ezcUrl( 'http://www.example.com', $urlCfg );
+
+ try
+ {
+ $url->setParam( 'param2', array( 'x' ) );
+ $this->fail( 'Expected exception was not thrown.' );
+ }
+ catch ( ezcBaseValueException $e )
+ {
+ $this->assertEquals( "The value 'a:1:{i:0;s:1:\"x\";}' that you
were trying to assign to setting 'param2' is invalid. Allowed values are:
string.", $e->getMessage() );
+ }
}
public function testIsSet()
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components