[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file tple_exports.class.php
Documentation is available at
tple_exports.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class TPLE_Exports
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_var_already_exists.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_not_exists.class.php'
)
;
/**
* Container for variables that will be used by template engine
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
class
Tple_Exports
extends
Tangra_Class
{
/**
* Array of variables and their values
*
*
@var
unknown_type
*
@internal
*/
private
$pairs
=
array
(
)
;
/**
* Constructor
*
*/
function
__construct
(
)
{
$this
->
pairs
=
array
(
)
;
}
/**
* Adds new variable and value
*
* Will throw TE_Var_Already_Exists if variable with same name already exists
*
*
@param
string
$var_name
Variable name
*
@param
mixed
$value
Variable value
*
@throws
TE_Var_Already_Exists
*/
public
function
add_pair
(
$var_name
,
$value
)
{
if
(
!
array_key_exists
(
$var_name
,
$this
->
pairs
))
{
$this
->
pairs
[
$var_name
]
=
$value
;
}
else
{
throw
new
TE_Var_Already_Exists
(
$var_name
)
;
}
}
/**
* Merges variables of current TPLE_Exports with those provided by <var>$exports</var>
*
*
@param
Tple_Exports
$exports
*/
public
function
merge
(
Tple_Exports
$exports
)
{
$new_exports
=
$exports
->
get_pairs
(
)
;
foreach
(
$new_exports
as
$key
=>
$value
)
{
$this
->
add_pair
(
$key
,
$value
)
;
}
}
/**
* Returns all variable-value pairs
*
*
@return
array
*/
public
function
&
get_pairs
(
)
{
return
$this
->
pairs
;
}
/**
* Changes value for pair
*
* Please note that if you need this function it is indicator that there is something
* wrong with your code - it is not normal to add variable and later to need to change its value.
* Design your code in a way that exports (add_pair()) are added just before parsing the template
*
*
@param
string
$var_name
Variable name
*
@param
mixed
$value
Variable value
*/
public
function
update_pair_value
(
$var_name
,
$value
)
{
if
(
array_key_exists
(
$var_name
,
$this
->
pairs
))
{
$this
->
pairs
[
$var_name
]
=
$value
;
}
else
{
throw
new
TE_Var_Already_Exists
(
$var_name
)
;
}
}
/**
* Gets value of pair
*
* If $forgiving is set to false will throw exception TE_Key_Not_Exists if such key does not exist.
* If $forgiving is set to true will return NULL if such key does not exist.
*
*
@param
string
$key
*
@param
boolean
$forgiving
*
@return
mixed
*
@throws
TE_Key_Not_Exists
*/
public
function
get_pair_value
(
$key
,
$forgiving
=
true
)
{
if
(
$this
->
key_exists
(
$key
))
{
$ret
=
$this
->
pairs
[
$key
]
;
}
else
{
if
(
$forgiving
)
{
$ret
=
NULL
;
}
else
{
throw
TE_Key_Not_Exists
(
'Key not exists: '
.
$key
)
;
}
}
return
$ret
;
}
/**
* Checks if there is already added pair with key $key
*
*
@param
string
$key
*
@return
boolean
*/
public
function
key_exists
(
$key
)
{
if
(
array_key_exists
(
$key
,
$this
->
pairs
))
{
$ret
=
true
;
}
else
{
$ret
=
false
;
}
return
$ret
;
}
}