PHP Classes
elePHPant
Icontem

File: phptricksORM/Operations/parseWhere.php

Recommend this page to a friend!
  Classes of mohammad anzawi  >  PHP PDO database class  >  phptricksORM/Operations/parseWhere.php  >  Download  
File: phptricksORM/Operations/parseWhere.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP PDO database class
Access databases using PDO
Author: By
Last change:
Date: 2 years ago
Size: 1,032 bytes
 

 

Contents

Class file image Download
<?php

namespace PHPtricks\Orm\Operations;

trait
parseWhere {
    private
$__whereTypes = ['AND', 'OR'];
   
/**
     * How to use
     * $con = [
     * [
     * 'sex', '=', 'female'
     * ],
     * 'AND' => [
     * 'position', '=', 'manager'
     * ]
     * ];
     * $db->table('table_name')->parseWhere($con)->select();
     */
   
public function parseWhere(array $cons, $type = "AND")
    {

       
$this->_query .= " {$type} (";

        foreach (
$cons as $con => $st)
        {
            if(
is_array($st))
            {
                if(!
is_numeric($st[2]))
                   
$st[2] = "'$st[2]'";
                else
                   
$st[2] = "`$st[2]`";

                if (
strtolower($con) === 'none' || $con === 0)
                {
                   
$this->_query .= " `{$st[0]}` $st[1] $st[2] ";
                }
                else
                {
                    if (
$this->con($con))
                    {
                       
$this->_query .= " {$con} `{$st[0]}` $st[1] $st[2] ";
                    }
                }
            }
            else
            {
               
$this->_query .= " `{$cons[0]}` $cons[1] $cons[2] ";
                break;
            }
           
        }

       
$this->_query .= ')';

        return
$this;
    }

    private function
con($con)
    {
        return
in_array(strtoupper($con), $this->__whereTypes);
    }
}