[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file context.class.php
Documentation is available at
context.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Context
*
*
@package
tangra_lib
*
@subpackage
core
*/
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_var_already_exists.class.php'
)
;
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_var_not_exists.class.php'
)
;
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_not_an_array.class.php'
)
;
/**
* Container for variables.
*
*
@package
tangra_lib
*
@subpackage
core
*
@see
Web_Context, Vars_Manager
*/
class
Context
extends
Tangra_Class
{
/**
* Storage for the values
*
*
@var
array
simple associative array
*/
private
$vars
=
array
(
)
;
/**
* Constructor
*
*
@param
array
$vars
Simple associative array used to initialize private $vars.
*
@throws
TE_Not_An_Array
*/
function
__construct
(
&
$vars
=
NULL
)
{
if
(
$vars
)
{
if
(
is_array
(
$vars
))
{
$this
->
vars
=
$vars
;
}
else
{
throw
new
TE_Not_An_Array
(
$vars
)
;
}
}
}
/**
* Adds new variable.
*
*
@param
string
$name
*
@param
any
$value
*/
public
function
add_var
(
$name
,
&
$value
)
{
if
(
!
array_key_exists
(
$name
,
$this
->
vars
))
{
$this
->
_set_var
(
$name
,
$value
)
;
}
else
{
throw
new
TE_Var_Already_Exists
(
$name
)
;
}
}
/**
* Sets variable.
* Throws exception if variable with name provided with $name does not exists.
*
*
@param
string
$name
*
@param
any
$value
*
@throws
TE_Var_Not_Exists
*/
public
function
set_var
(
$name
,
$value
)
{
if
(
array_key_exists
(
$name
,
$this
->
vars
))
{
$this
->
_set_var
(
$name
,
$value
)
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$name
)
;
}
}
/**
* Gets variable as reference. Use this function if you intend to change the value.
*
*
@param
string
$name
*
@return
any
*
@throws
TE_Var_Not_Exists
*/
public
function
&
get_var
(
$name
)
{
if
(
array_key_exists
(
$name
,
$this
->
vars
))
{
return
$this
->
vars
[
$name
]
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$name
)
;
}
}
/**
* Query variable. Similar to get_var() but returns copy of the variable, not reference. Use this function instead of get_var() if you are not intending to change the value.
*
*
@param
string
$name
*
@return
any
*/
public
function
query_var
(
$name
)
{
if
(
array_key_exists
(
$name
,
$this
->
vars
))
{
return
$this
->
vars
[
$name
]
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$name
)
;
}
}
/**
* Checks if var exists (it is already set)
*
*
@param
string
$key
*
@return
boolean
*/
public
function
vars_exists
(
$key
)
{
$ret
=
false
;
if
(
array_key_exists
(
$key
,
$this
->
vars
))
{
$ret
=
true
;
}
return
$ret
;
}
/**
* Actual set of the variable
*
*
@param
string
$name
*
@param
any
$value
*
@access
private
*/
private
function
_set_var
(
$name
,
&
$value
)
{
$this
->
vars
[
$name
]
=
&
$value
;
}
}