mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-21142 moodle_url improvements:
1/ new __toString() 2/ strict method parameters checking, this should help discover wrong uses 3/ new remove_all_params() which prevents problems when using remove_params()
This commit is contained in:
parent
c39e5ba20f
commit
2b3fcef98d
1 changed files with 83 additions and 30 deletions
113
lib/weblib.php
113
lib/weblib.php
|
@ -290,9 +290,10 @@ class moodle_url {
|
||||||
* @param array $params these params override anything in the query string
|
* @param array $params these params override anything in the query string
|
||||||
* where params have the same name.
|
* where params have the same name.
|
||||||
*/
|
*/
|
||||||
public function __construct($url = null, $params = array()) {
|
public function __construct($url = null, array $params = null) {
|
||||||
if ($url === '') {
|
if ($url === '') {
|
||||||
// Leave URL blank.
|
// Leave URL blank.
|
||||||
|
|
||||||
} else if (is_a($url, 'moodle_url')) {
|
} else if (is_a($url, 'moodle_url')) {
|
||||||
$this->scheme = $url->scheme;
|
$this->scheme = $url->scheme;
|
||||||
$this->host = $url->host;
|
$this->host = $url->host;
|
||||||
|
@ -302,6 +303,7 @@ class moodle_url {
|
||||||
$this->path = $url->path;
|
$this->path = $url->path;
|
||||||
$this->fragment = $url->fragment;
|
$this->fragment = $url->fragment;
|
||||||
$this->params = $url->params;
|
$this->params = $url->params;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if ($url === null) {
|
if ($url === null) {
|
||||||
global $ME;
|
global $ME;
|
||||||
|
@ -322,23 +324,34 @@ class moodle_url {
|
||||||
$this->$key = $value;
|
$this->$key = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->params($params);
|
$this->params($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an array of params to the params for this page.
|
* Add an array of params to the params for this url.
|
||||||
*
|
*
|
||||||
* The added params override existing ones if they have the same name.
|
* The added params override existing ones if they have the same name.
|
||||||
*
|
*
|
||||||
* @param array $params Defaults to null. If null then returns all params.
|
* @param array $params Defaults to null. If null then returns all params.
|
||||||
* @return array Array of Params for url.
|
* @return array Array of Params for url.
|
||||||
*/
|
*/
|
||||||
public function params($params = null) {
|
public function params(array $params = null) {
|
||||||
if (!is_null($params)) {
|
$params = (array)$params;
|
||||||
return $this->params = $params + $this->params;
|
|
||||||
} else {
|
foreach ($params as $key=>$value) {
|
||||||
return $this->params;
|
if (is_int($key)) {
|
||||||
|
throw new coding_error('Url parameters can not have numeric keys!');
|
||||||
|
}
|
||||||
|
if (is_array($value)) {
|
||||||
|
throw new coding_error('Url parameters values can not be arrays!');
|
||||||
|
}
|
||||||
|
if (is_object($value) and !method_exists($value, '__toString')) {
|
||||||
|
throw new coding_error('Url parameters values can not be objects, unless __toString() is defined!');
|
||||||
|
}
|
||||||
|
$this->params[$key] = (string)$value;
|
||||||
}
|
}
|
||||||
|
return $this->params;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -350,42 +363,71 @@ class moodle_url {
|
||||||
*
|
*
|
||||||
* @param mixed $params either an array of param names, or a string param name,
|
* @param mixed $params either an array of param names, or a string param name,
|
||||||
* @param string $params,... any number of additional param names.
|
* @param string $params,... any number of additional param names.
|
||||||
|
* @return array url parameters
|
||||||
*/
|
*/
|
||||||
public function remove_params($params = NULL) {
|
public function remove_params($params = null) {
|
||||||
if (empty($params)) {
|
|
||||||
$this->params = array();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!is_array($params)) {
|
if (!is_array($params)) {
|
||||||
$params = func_get_args();
|
$params = func_get_args();
|
||||||
}
|
}
|
||||||
foreach ($params as $param) {
|
foreach ($params as $param) {
|
||||||
if (isset($this->params[$param])) {
|
unset($this->params[$param]);
|
||||||
unset($this->params[$param]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return $this->params;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a param to the params for this page.
|
* Remove all url parameters
|
||||||
|
* @param $params
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function remove_all_params($params = null) {
|
||||||
|
$this->params = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a param to the params for this url.
|
||||||
*
|
*
|
||||||
* The added param overrides existing one if theyhave the same name.
|
* The added param overrides existing one if they have the same name.
|
||||||
*
|
*
|
||||||
* @param string $paramname name
|
* @param string $paramname name
|
||||||
* @param string $param Param value. Defaults to null. If null then return value of param 'name'
|
* @param string $newvalue Param value. If new value specified current value is overriden or parameter is added
|
||||||
* @return void|string If $param was null then the value of $paramname was returned
|
* @return mixed string parameter value, null if parameter does not exist
|
||||||
* (null is returned if that param does not exist).
|
|
||||||
*/
|
*/
|
||||||
public function param($paramname, $param = null) {
|
public function param($paramname, $newvalue = '') {
|
||||||
if (!is_null($param)) {
|
if (func_num_args() > 1) {
|
||||||
$this->params = array($paramname => $param) + $this->params;
|
// set new value
|
||||||
} else if (array_key_exists($paramname, $this->params)) {
|
$this->params(array($paramname=>$newvalue));
|
||||||
|
}
|
||||||
|
if (isset($this->params[$paramname])) {
|
||||||
return $this->params[$paramname];
|
return $this->params[$paramname];
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges parameters and validates them
|
||||||
|
* @param array $overrideparams
|
||||||
|
* @return array merged parameters
|
||||||
|
*/
|
||||||
|
protected function merge_overrideparams(array $overrideparams = null) {
|
||||||
|
$overrideparams = (array)$overrideparams;
|
||||||
|
$params = $this->params;
|
||||||
|
foreach ($overrideparams as $key=>$value) {
|
||||||
|
if (is_int($key)) {
|
||||||
|
throw new coding_error('Overriden parameters can not have numeric keys!');
|
||||||
|
}
|
||||||
|
if (is_array($value)) {
|
||||||
|
throw new coding_error('Overriden parameters values can not be arrays!');
|
||||||
|
}
|
||||||
|
if (is_object($value) and !method_exists($value, '__toString')) {
|
||||||
|
throw new coding_error('Overriden parameters values can not be objects, unless __toString() is defined!');
|
||||||
|
}
|
||||||
|
$params[$key] = (string)$value;
|
||||||
|
}
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the params as as a query string.
|
* Get the params as as a query string.
|
||||||
*
|
*
|
||||||
|
@ -394,9 +436,9 @@ class moodle_url {
|
||||||
* @param boolean $escaped Use & as params separator instead of plain &
|
* @param boolean $escaped Use & as params separator instead of plain &
|
||||||
* @return string query string that can be added to a url.
|
* @return string query string that can be added to a url.
|
||||||
*/
|
*/
|
||||||
public function get_query_string($overrideparams = array(), $escaped = true) {
|
public function get_query_string(array $overrideparams = null, $escaped = true) {
|
||||||
$arr = array();
|
$arr = array();
|
||||||
$params = $overrideparams + $this->params;
|
$params = $this->merge_overrideparams($overrideparams);
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
$arr[] = urlencode($key)."=".urlencode($val);
|
$arr[] = urlencode($key)."=".urlencode($val);
|
||||||
}
|
}
|
||||||
|
@ -416,10 +458,12 @@ class moodle_url {
|
||||||
* override existing ones with the same name.
|
* override existing ones with the same name.
|
||||||
* @return string html for form elements.
|
* @return string html for form elements.
|
||||||
*/
|
*/
|
||||||
public function hidden_params_out($exclude = array(), $indent = 0, $overrideparams=array()) {
|
public function hidden_params_out(array $exclude = null, $indent = 0, array $overrideparams = null) {
|
||||||
|
$exclude = (array)$exclude;
|
||||||
|
$params = $this->merge_overrideparams($overrideparams);
|
||||||
|
|
||||||
$tabindent = str_repeat("\t", $indent);
|
$tabindent = str_repeat("\t", $indent);
|
||||||
$str = '';
|
$str = '';
|
||||||
$params = $overrideparams + $this->params;
|
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
if (FALSE === array_search($key, $exclude)) {
|
if (FALSE === array_search($key, $exclude)) {
|
||||||
$val = s($val);
|
$val = s($val);
|
||||||
|
@ -429,6 +473,14 @@ class moodle_url {
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for printing of encoded URL.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString() {
|
||||||
|
$this->out(false, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output url
|
* Output url
|
||||||
*
|
*
|
||||||
|
@ -440,7 +492,7 @@ class moodle_url {
|
||||||
* @param boolean $escaped Use & as params separator instead of plain &
|
* @param boolean $escaped Use & as params separator instead of plain &
|
||||||
* @return string Resulting URL
|
* @return string Resulting URL
|
||||||
*/
|
*/
|
||||||
public function out($omitquerystring = false, $overrideparams = array(), $escaped = true) {
|
public function out($omitquerystring = false, array $overrideparams = null, $escaped = true) {
|
||||||
$uri = $this->scheme ? $this->scheme.':'.((strtolower($this->scheme) == 'mailto') ? '':'//'): '';
|
$uri = $this->scheme ? $this->scheme.':'.((strtolower($this->scheme) == 'mailto') ? '':'//'): '';
|
||||||
$uri .= $this->user ? $this->user.($this->pass? ':'.$this->pass:'').'@':'';
|
$uri .= $this->user ? $this->user.($this->pass? ':'.$this->pass:'').'@':'';
|
||||||
$uri .= $this->host ? $this->host : '';
|
$uri .= $this->host ? $this->host : '';
|
||||||
|
@ -489,7 +541,8 @@ class moodle_url {
|
||||||
* @param array $overrideparams Allows you to override params
|
* @param array $overrideparams Allows you to override params
|
||||||
* @return string url
|
* @return string url
|
||||||
*/
|
*/
|
||||||
public function out_action($overrideparams = array()) {
|
public function out_action(array $overrideparams = null) {
|
||||||
|
$overrideparams = (array)$overrideparams;
|
||||||
$overrideparams = array('sesskey'=> sesskey()) + $overrideparams;
|
$overrideparams = array('sesskey'=> sesskey()) + $overrideparams;
|
||||||
return $this->out(false, $overrideparams);
|
return $this->out(false, $overrideparams);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue