Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TCriteria.php

Documentation is available at TCriteria.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Database\TExpression;
  5.  
  6. /**
  7.  * Provides an interface for filtering criteria definition
  8.  *
  9.  * @version    7.4
  10.  * @package    database
  11.  * @author     Pablo Dall'Oglio
  12.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  13.  * @license    http://www.adianti.com.br/framework-license
  14.  */
  15. class TCriteria extends TExpression
  16. {
  17.     private $expressions;  // store the list of expressions
  18.     private $operators;    // store the list of operators
  19.     private $properties;   // criteria properties
  20.     private $caseInsensitive;
  21.  
  22.     /**
  23.      * Constructor Method
  24.      * @author Pablo Dall'Oglio
  25.      */
  26.     public function __construct()
  27.     {
  28.         $this->expressions array();
  29.         $this->operators   array();
  30.         
  31.         $this->properties['order']     '';
  32.         $this->properties['offset']    0;
  33.         $this->properties['direction''';
  34.         $this->properties['group']     '';
  35.  
  36.         $this->caseInsensitive FALSE;
  37.     }
  38.  
  39.     /**
  40.      * create criteria from array of filters
  41.      */
  42.     public static function create($simple_filters$properties null)
  43.     {
  44.         $criteria new TCriteria;
  45.         if ($simple_filters)
  46.         {
  47.             foreach ($simple_filters as $left_operand => $right_operand)
  48.             {
  49.                 $criteria->add(new TFilter($left_operand'='$right_operand));
  50.             }
  51.         }
  52.         
  53.         if ($properties)
  54.         {
  55.             foreach ($properties as $property => $value)
  56.             {
  57.                 if (!empty($value))
  58.                 {
  59.                     $criteria->setProperty($property$value);
  60.                 }
  61.             }
  62.         }
  63.         
  64.         return $criteria;
  65.     }
  66.     
  67.     /**
  68.      * When clonning criteria
  69.      */
  70.     function __clone()
  71.     {
  72.         $newExpressions array();
  73.         foreach ($this->expressions as $key => $value)
  74.         {
  75.             $newExpressions[$keyclone $value;
  76.         }
  77.         $this->expressions $newExpressions;
  78.     }
  79.     
  80.     /**
  81.      * Adds a new Expression to the Criteria
  82.      *
  83.      * @param   $expression  TExpression object
  84.      * @param   $operator    Logic Operator Constant
  85.      * @author               Pablo Dall'Oglio
  86.      */
  87.     public function add(TExpression $expression$operator self::AND_OPERATOR)
  88.     {
  89.         // the first time, we don't need a logic operator to concatenate
  90.         if (empty($this->expressions))
  91.         {
  92.             $operator NULL;
  93.         }
  94.         
  95.         // aggregates the expression to the list of expressions
  96.         $this->expressions[$expression;
  97.         $this->operators[]   $operator;
  98.     }
  99.     
  100.     /**
  101.      * Return if criteria is empty
  102.      */
  103.     public function isEmpty()
  104.     {
  105.         return count($this->expressions== 0;
  106.     }
  107.     
  108.     /**
  109.      * Return the prepared vars
  110.      */
  111.     public function getPreparedVars()
  112.     {
  113.         $preparedVars array();
  114.         if (is_array($this->expressions))
  115.         {
  116.             if (count($this->expressions0)
  117.             {
  118.                 foreach ($this->expressions as $expression)
  119.                 {
  120.                     $preparedVars array_merge($preparedVars(array) $expression->getPreparedVars());
  121.                 }
  122.                 return $preparedVars;
  123.             }
  124.         }
  125.     }
  126.     
  127.     /**
  128.      * Returns the final expression
  129.      * 
  130.      * @param   $prepared Return a prepared expression
  131.      * @return  string containing the resulting expression
  132.      * @author  Pablo Dall'Oglio
  133.      */
  134.     public function dump$prepared FALSE)
  135.     {
  136.         // concatenates the list of expressions
  137.         if (is_array($this->expressions))
  138.         {
  139.             if (count($this->expressions0)
  140.             {
  141.                 $result '';
  142.                 foreach ($this->expressions as $i=> $expression)
  143.                 {
  144.                     $operator $this->operators[$i];
  145.                     $expression->setCaseInsensitive($this->caseInsensitive);
  146.  
  147.                     // concatenates the operator with its respective expression
  148.                     $result .=  $operator$expression->dump$prepared ' ';
  149.                 }
  150.                 $result trim($result);
  151.                 if ($result)
  152.                 {
  153.                     return "({$result})";
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     
  159.     /**
  160.      * Define a Criteria property
  161.      * 
  162.      * @param $property Name of the property (limit, offset, order, direction)
  163.      * @param $value    Value for the property
  164.      * @author          Pablo Dall'Oglio
  165.      */
  166.     public function setProperty($property$value)
  167.     {
  168.         if (isset($value))
  169.         {
  170.             $this->properties[$property$value;
  171.         }
  172.         else
  173.         {
  174.             $this->properties[$propertyNULL;
  175.         }
  176.         
  177.     }
  178.     
  179.     /**
  180.      * reset criteria properties
  181.      */
  182.     public function resetProperties()
  183.     {
  184.         $this->properties['limit']  NULL;
  185.         $this->properties['order']  NULL;
  186.         $this->properties['offset'NULL;
  187.         $this->properties['group']  NULL;
  188.     }
  189.     
  190.     /**
  191.      * Set properties form array
  192.      * @param $properties array of properties
  193.      */
  194.     public function setProperties($properties)
  195.     {
  196.         if (isset($properties['order']AND $properties['order'])
  197.         {
  198.             $this->properties['order'addslashes($properties['order']);
  199.         }
  200.         
  201.         if (isset($properties['offset']AND $properties['offset'])
  202.         {
  203.             $this->properties['offset'= (int) $properties['offset'];
  204.         }
  205.         
  206.         if (isset($properties['direction']AND $properties['direction'])
  207.         {
  208.             $this->properties['direction'$properties['direction'];
  209.         }
  210.     }
  211.     
  212.     /**
  213.      * Return a Criteria property
  214.      * 
  215.      * @param $property Name of the property (LIMIT, OFFSET, ORDER)
  216.      * @return          String containing the property value
  217.      * @author          Pablo Dall'Oglio
  218.      */
  219.     public function getProperty($property)
  220.     {
  221.         if (isset($this->properties[$property]))
  222.         {
  223.             return $this->properties[$property];
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Force case insensitive searches
  229.      */
  230.     public function setCaseInsensitive(bool $valuevoid
  231.     {
  232.         $this->caseInsensitive $value;
  233.     }
  234.  
  235.     /**
  236.      * Return if case insensitive is turned on
  237.      */
  238.     public function getCaseInsensitive(bool
  239.     {
  240.         return $this->caseInsensitive;
  241.     }
  242. }