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: New Version (5.0.0)
Date: 7 months ago
Size: 1,812 bytes
 

Contents

Class file image Download
<?php
/**
 * *
 * * please don't remove this comment block
 * *
 * * @author phptricks Team - Mohammad Anzawi
 * * @author_uri https://phptricks.org
 * * @uri https://github.com/anzawi/php-database-class
 * * @version 5.0.0
 * * @licence MIT -> https://opensource.org/licenses/MIT
 * * @package PHPtricks\Orm
 *
 */

namespace PHPtricks\Orm\Operations;

/**
 * Trait parseWhere
 *
 * @package PHPtricks\Orm\Operations
 */
trait parseWhere
{

   
/**
     * @var string[]
     */
   
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;
    }

   
/**
     * @param $con
     *
     * @return bool
     */
   
private function con($con)
    {
        return
in_array(strtoupper($con), $this->__whereTypes);
    }

}


For more information send a message to info at phpclasses dot org.