Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2.  
  3. class Where
  4. {
  5.     public $operation = 'AND';
  6.     public $condition = '';
  7.     public $childs    = [];
  8.     public $params    = [];
  9.  
  10.     static private $DBH = NULL;
  11.  
  12.     /**
  13.      * Where constructor.
  14.      * If $condition is string, all next parameters
  15.      * @param  $condition
  16.      */
  17.     public function __construct($condition)
  18.     {
  19.         //get DBH for escaping string
  20.         if(empty(self::$DBH) && empty((self::$DBH = DBHandler::getHandler())))
  21.             return NULL;
  22.  
  23.         if(is_string($condition))
  24.         {
  25.             $this->condition = $condition;
  26.             $this->params    = array_slice(func_get_args(),1);
  27.         }
  28.         elseif(is_array($condition))
  29.         {
  30.             if(array_key_exists('condition',$condition) &&
  31.                 array_key_exists('values',$condition))
  32.             {
  33.                 $this->condition = $condition['condition'];
  34.                 $this->params    = $condition['values'];
  35.             }
  36.             foreach ($condition as $k => $c)
  37.                 if(is_string($k))
  38.                 {
  39.                     if ($k == 'operation')
  40.                         $this->operation = $c;
  41.                     else
  42.                         $this->childs[] = new self($k, $c);
  43.                 }
  44.                 elseif(is_array($c))
  45.                      $this->childs[] = new self($c);
  46.         }
  47.         else
  48.         {
  49.             foreach(func_get_args() as $a)
  50.                 $this->childs[] = new self($a);
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * Getter-setter for operation
  56.      * @param null|string $o Operation
  57.      * @return string|Where
  58.      */
  59.     public function operation($o = NULL)
  60.     {
  61.         if(empty($o))
  62.             return $this->operation;
  63.         else
  64.         {
  65.             $this->operation = $o;
  66.             return $this;
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Get finally condition from
  72.      * @return string
  73.      */
  74.     public function get_condition()
  75.     {
  76.         return $this->update_condition(!empty($this->condition));
  77.     }
  78.  
  79.     /**
  80.      * Regenerate where-condition string with all child conditions
  81.      * @param bool $inchilds
  82.      * @return string
  83.      */
  84.     private function update_condition($inchilds=false)
  85.     {
  86.         if($inchilds)
  87.         {
  88.             return '('.implode(' '.$this->operation.' ',array_map(function(/** @var $x Where*/$x){return $x->get_condition();},$this->childs)).')';
  89.         }
  90.         else
  91.         {
  92.             $c = explode('?',$this->condition);
  93.             $r = ''; $c1 = count($c); $c2 = count($this->params);
  94.             for($i = 0, $j = 0; $i < $c1; $i++)
  95.             {
  96.                 $r .= $c[$i];
  97.                 if($j < $c2 && $i < $c1-1)
  98.                 {
  99.                     $r .= self::$DBH->_escape($this->params[$j]);
  100.                     $j++;
  101.                     if($j == $c2)
  102.                         $j = 0;
  103.                 }
  104.             }
  105.             return $r;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement