[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file redirect_composer.class.php
Documentation is available at
redirect_composer.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Redirect_Composer
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_already_exists.class.php'
)
;
/**
* Redirect_Composer's purpose is to compose address for redirection
*
*
@see
Redirect_Composer_Local
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
class
Redirect_Composer
extends
Tangra_Class
{
/**
* Target address
*
*
@var
string
*
@internal
*/
private
$target
;
/**
* Parameters that will be added to target address
*
*
@var
array
*
@internal
*/
private
$param_pairs
;
/**
* Constructor
*
*
@param
string
$target
Target address
*
@param
array
$param_pairs
Parameters that will be added to target address ($key => $value pairs)
*
@throws
Tangra_Exception
*/
function
__construct
(
$target
,
$param_pairs
=
array
(
))
{
$this
->
target
=
$target
;
if
(
is_array
(
$param_pairs
))
{
$this
->
param_pairs
=
$param_pairs
;
}
else
{
throw
new
Tangra_Exception
(
'Parameter $params_pairs is not an array. Current value: '
.
$param_pairs
)
;
}
}
/**
* Returns composed location that can be used directly by header()
*
*
@return
string
*/
public
function
get_location
(
)
{
$params
=
$this
->
get_params
(
)
;
$ret
=
'Location: '
.
$this
->
get_target_address
(
)
;
return
$ret
;
}
/**
* Sets $target
*
*
@param
Web_Context
$target
*
@internal
*/
private
function
set_target
(
Web_Context
$target
)
{
$this
->
_target
=
$target
;
}
/**
* Returns target
*
*
@return
string
*/
public
function
get_target
(
)
{
return
$this
->
target
;
}
/**
* Returns composed target address with parameters
*
*
@return
string
*/
public
function
get_target_address
(
)
{
$params
=
$this
->
get_params
(
)
;
return
$this
->
target
.
$params
;
}
/**
* Add new parameter pair
*
*
@param
string
$key
Parameter name
*
@param
string
$value
Parameter value
*
@throws
TE_Key_Already_Exists
*/
public
function
add_param_pair
(
$key
,
$value
)
{
if
(
!
array_key_exists
(
$key
,
$this
->
param_pairs
))
{
$this
->
param_pairs
[
$key
]
=
$value
;
}
else
{
throw
new
TE_Key_Already_Exists
(
$key
)
;
}
}
/**
* Removes param pair
*
*
@param
string
$key
Name of the param pair
*/
public
function
remove_param
(
$key
)
{
if
(
array_key_exists
(
$key
,
$this
->
param_pairs
))
{
unset
(
$this
->
param_pairs
[
$key
]
)
;
}
}
/**
* Checks if there is param pair with param name $key
*
*
@param
string
$key
*
@return
boolean
*/
public
function
param_exists
(
$key
)
{
return
array_key_exists
(
$key
,
$this
->
param_pairs
)
;
}
/**
* Returns all paramteres
*
*
@return
array
*/
protected
function
get_params
(
)
{
$params
=
''
;
foreach
(
$this
->
param_pairs
as
$key
=>
$value
)
{
if
(
$params
)
{
$params
.=
'&'
;
}
else
{
$params
.=
'?'
;
}
$params
.=
$key
.
'='
.
$value
;
}
return
$params
;
}
}