Input and Output variables - Require PHP 5.3.0

Features :
- Input sources (types) : GET, POST, COOKIE, FILES, SERVER, REQUEST, ENV, SESSION, local variables - Output format : HTML, URL, RAW, Base64, ISSET, SET - Format a input variable for an output format - Cached variables for performance - Exception or not if variable don't exist (configuration) - Charset can be specified - Types and format can be extended - Change values in superglobal arrays

Usage :
$Input = In::getInstance(); echo $Input->G->RAW->id; // display $_GET['id'] without change echo $Input->P->HTML->foo; // display $_POST['foo'] for a HTML usage (htmlentities) echo $Input->C->B64->login; // display $_COOKIE['login'] in base64 encoding echo $Input->F->RAW->fileName; // display $FILES['fileName']; without change echo $Input->G->ISSET->id; // display TRUE if $_GET['id'] exists, FALSE otherwise echo $Input->G->SET->id = 'foo'; // change value of $_GET['id'] et reset caches in instance echo $Input->HTML($localVariable); // display $localVariable for a HTML usage (htmlentities)

Extend type or format :
- Create a child class which extend In - Register type / format in register() method like predefined type/format in In - Implement convert() method for new formats in child class

Configuration :
In::setThrowException(FALSE); // Return NULL instead of throwing exception if a variable don't exists. In::setCharset('...'); // Set a charset for htmlentities

Warning to the behavior with In cache :
$Input = In::getInstance(); echo $Input->G->RAW->id; // display $_GET['id'] $_GET['id'] = 'new value'; echo $Input->G->RAW->id; // display the OLD value of $_GET['id'] because id is now in cache ! echo $Input->G->HTML->id; // display the NEW value of $_GET['id'] because id has been never asked for HTML format ! $_GET['machin'] = 'valeur'; echo $Input->G->RAW->machin; // display $_GET['machin'] because value has neven been asked, so it's not in cache. If we use $Input->G->SET->id = 'new value'; instead, this behavior vanish and all works as expected.

History
2.0 (25/11/2017)
The class became linked to SALT framework : Use of SALT Converters, better performance Use of SALT I18n translation Remove SQL format
1.8 (03/03/2017)
Remove array format feature : Allow this class to process array and scalar values on different ways is error prone. We can use RAW for retrieve unformated array values, and a loop for formatting each values.
1.7 (15/08/2016)
Translate in english
1.6 (31/07/2016)
Complete rewrite / simplification Removing magic_quotes support (deprecated in 5.3, removed in 5.4) Exception thrown for bad usage. setThrowException(FALSE) disable exceptions only for undefined variables. Add REQUEST, ENV, SESSION types Add SET format
1.5 (10/07/2016)
Add SERVER type Change URL implementation for using rawurlencode instead of urlencode
1.4 (12/03/2011)
Complete rewrite. Removing internal classes. New feature : register new type & format New format : URL & Base64
1.3 (15/02/2009)
Bug fix : magic_quotes_gpc
1.2
New feature : Charset for htmlentities HTML format : use of ENT_QUOTES
1.1 (29/12/2007)
New feature : format local variables
author Richaud Julien "Fladnag"
version 1.8
package salt\utils

 Methods

value formatted with base64_encode()

B64() : 
magic
method value formatted with base64_encode()

Returns

HTML value formatted with htmlentities(), ENT_QUOTES, and charset

HTML() : 
magic
method HTML value formatted with htmlentities(), ENT_QUOTES, and charset

Returns

value without change

RAW() : 
magic
method value without change

Returns

value formatted with rawurlencode()

URL() : 
magic
method value formatted with rawurlencode()

Returns

Throw an exception for calling an undefined format

__call(string $method, array<mixed,mixed> $args) 

Parameters

$method

string

the method/format

$args

array<mixed,mixed>

Exceptions

\Exception static::_SALT_EX_UNDEFINED_FORMAT

Return an instance property. If the property don't exists, it's will be dynamically created then returned<br/> So, the next access to this property will not call this method.

__get(string $var) : mixed

Parameters

$var

string

Property name

Exceptions

\Exception static::_SALT_EX_UNDEFINED_VARIABLE if property don't exists, or NULL if exceptions disabled

Returns

mixedvalue

Set a dynamic property in instance

__set(string $var, mixed $value) 

If the call is from self::setCache(), it's an internal call and we set the value without check.
If the call is from ->$type->SET-> , it's an external call and we set the value in $type array and we doing a cache reset
If the call is from another format ->$type->$format-> we throw an exception.

Parameters

$var

string

Property name to set

$value

mixed

value

Exceptions

\Exception self::_SALT_EX_BAD_SETTER

Return charset

getCharset() : string
Static

Returns

stringcharset

Retrieve an In singleton

getInstance() : static
Static

Returns

staticinstance of class (1 singleton by child class)

Check if we throw an exception if variable undefined

haveToThrowException() : boolean
Static

Returns

booleanreturn TRUE if we have to thrown an exception if variable undefined.

Set the charset for format functions (like HTML with htmlentities)

setCharset(string $charset) 
Static
see

Parameters

$charset

string

Valid charset

Configure In instances for throw exception if a variable is undefined

setThrowException(boolean $throwException = TRUE
Static

Parameters

$throwException

boolean

FALSE to not thrown exception if variable undefined.

Register types and formats

register() 

The method to override to register new type/format
Don't forget to call parent::register()

Register a new format

registerFormat(string $name, \salt\Converter $converter) 
Static

Parameters

$name

string

Name of format

$converter

\salt\Converter

Converter class

Register a new type

registerType(string $name, string $source) 
Static

Parameters

$name

string

Name of type

$source

string

Name of superglobal array, without $ (for example "_GET" for "$_GET")

New instance of In

__construct(\salt\In $parent = NULL, string $name = NULL

If $parent is provided, the return object depends on parent type.
If $parent is an _SALT_INSTANCE_BASE instance, will construct an _SALT_INSTANCE_TYPE instance
If $parent is an _SALT_INSTANCE_TYPE instance, will construct an _SALT_INSTANCE_FORMAT instance

Parameters

$parent

\salt\In

Parent instance if we are in a delegation chain

$name

string

Name of type or format

Reset caches for variable on current instance (of type _SALT_INSTANCE_TYPE)

invalidCache(string $var) 

Parameters

$var

string

Name of variable to remove from caches

Change a dynamic property of the instance

setCache(string $var, mixed $value) 

Parameters

$var

string

Name of property

$value

mixed

New value

Throw an exception

throwException(string $message, integer $type, boolean $force = FALSE
Static

Parameters

$message

string

Exception message

$type

integer

code, see self::_SALTEX*

$force

boolean

(optional, FALSE) TRUE if we have to throw an internal exception and ignore configuration

Exceptions

\Exception self::_SALT_EX_*

 Properties

 

Charset to use in format functions if needed

$_saltCharset : string

Default

NULL
Static
 

In singleton list : 1 singleton for each child class as className=>Instance

$_saltInstances : array<mixed,\salt\In>

Default

array()
Static
 

Type and format registered as TYPE=>array(name => mixed)

$_saltRegistry : array<mixed,mixed[]>

Default

array(self::_SALT_INSTANCE_TYPE => array(), self::_SALT_INSTANCE_FORMAT => array())
Static
 

TRUE if we have to throw an exception if variable undefined

$_saltThrowException : boolean

Default

TRUE
Static
 

differentiate internal call of exteral __set call

$_saltCacheSetter : boolean

Default

FALSE
 

format or type name, depends on instance type

$_saltName : string

Default

NULL
 

parent instance

$_saltParent : \salt\In

Default

NULL
 

instance type

$_saltType : integer

Default

self::_SALT_INSTANCE_BASE

 Constants

 

Exception code for trying to set a value without SET format

_SALT_EX_BAD_SETTER = 4 
 

Exception code for undefined format

_SALT_EX_UNDEFINED_FORMAT = 2 
 

Exception code for undefined type

_SALT_EX_UNDEFINED_TYPE = 3 
 

Exception code for undefined variable

_SALT_EX_UNDEFINED_VARIABLE = 1 
 

In type : instance retrieved with getInstance()

_SALT_INSTANCE_BASE = -1 
 

In type : instance retrieved with ->TYPE->FORMAT, linked to a superglobal array AND a format

_SALT_INSTANCE_FORMAT = 1 
 

In type : instance retrieved with ->TYPE, linked to a superglobal array

_SALT_INSTANCE_TYPE = 0