Source for file TFilter.php
Documentation is available at TFilter.php
namespace
Adianti \
Database ;
* Provides an interface to define filters to be used inside a criteria
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
private $caseInsensitive ;
* @param $variable = variable
* @param $operator = operator (>, <, =, BETWEEN)
* @param $value = value to be compared
* @param $value2 = second value to be compared (between)
public function __construct ( $variable , $operator , $value , $value2 =
NULL )
$this -> variable =
$variable ;
$this -> operator =
$operator ;
$this -> preparedVars =
array ( ) ;
// transform the value according to its type
$this -> caseInsensitive =
FALSE ;
* Transform the value according to its PHP type
* before send it to the database
* @param $value Value to be transformed
* @param $prepared If the value will be prepared
* @return Transformed Value
private function transform ( $value , $prepared =
FALSE )
// if the value is an array
// if the value is an integer
$preparedVar =
':par_' .
$this -> getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$x ;
// if the value is an string, add quotes
$preparedVar =
':par_' .
$this -> getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$x ;
$foo [ ] =
( $x ) ?
'TRUE' :
'FALSE' ;
// convert the array into a string, splitted by ","
$result =
'(' .
implode ( ',' , $foo ) .
')' ;
// if the value is a subselect (must not be escaped as string)
$value =
str_replace ( [ '#' , '--' , '/*' ] , [ '' , '' , '' ] , $value ) ;
// if the value must not be escaped (NOESC in front)
else if ( substr ( (string)
$value , 0 , 6 ) ==
'NOESC:' )
$value =
str_replace ( [ '#' , '--' , '/*' ] , [ '' , '' , '' ] , $value ) ;
// if the value is a string
$preparedVar =
':par_' .
$this -> getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$value ;
// if the value is a boolean
// the result is 'TRUE' of 'FALSE'
$result =
$value ?
'TRUE' :
'FALSE' ;
// if the value is a TSqlStatement object
// the result is the return of the getInstruction()
$result =
'(' .
$value -> getInstruction ( ) .
')' ;
$preparedVar =
':par_' .
$this -> getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$value ;
* Return the prepared vars
return $this -> preparedVars ;
* Return the filter as a string expression
* @return A string containing the filter
public function dump ( $prepared =
FALSE )
$this -> preparedVars =
array ( ) ;
$value =
$this -> transform ( $this -> value , $prepared ) ;
$value2 =
$this -> transform ( $this -> value2 , $prepared ) ;
// concatenated the expression
return " {
$this -> variable } {
$this -> operator } {
$value } AND {
$value2 } " ;
$variable = $this -> variable ;
$operator =
$this -> operator ;
if ( $this -> caseInsensitive &&
stristr ( strtolower ( $operator ) , 'like' ) !==
FALSE )
$variable = " UPPER( {
$variable } )
" ;
$value = " UPPER( {
$value } )
" ;
// concatenated the expression
return " {
$variable } {
$operator } {
$value } " ;
* Returns a random parameter
private function getRandomParameter ( )
return mt_rand ( 1000000000 , 1999999999 ) ;
* Force case insensitive searches
public function setCaseInsensitive ( bool $value ) : void
$this -> caseInsensitive =
$value ;
* Return if case insensitive is turned ON
public function getCaseInsensitive ( ) : bool
return $this -> caseInsensitive ;