Overview

Namespaces

  • DSchoenbauer
    • Sql
      • Command
      • Exception
      • Where

Classes

  • DSchoenbauer\Sql\Command\Create
  • DSchoenbauer\Sql\Command\Delete
  • DSchoenbauer\Sql\Command\Select
  • DSchoenbauer\Sql\Command\Update
  • DSchoenbauer\Sql\Exception\EmptyDatasetException
  • DSchoenbauer\Sql\Exception\ExecutionErrorException
  • DSchoenbauer\Sql\Exception\MethodNotValidException
  • DSchoenbauer\Sql\Query
  • DSchoenbauer\Sql\Where\ArrayWhere

Interfaces

  • DSchoenbauer\Sql\Command\CommandInterface
  • DSchoenbauer\Sql\Exception\SqlExceptionInterface
  • DSchoenbauer\Sql\Where\WhereStatementInterface

Traits

  • DSchoenbauer\Sql\Command\WhereTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3:  * The MIT License
  4:  *
  5:  * Copyright 2017 David Schoenbauer <dschoenbauer@gmail.com>.
  6:  *
  7:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8:  * of this software and associated documentation files (the "Software"), to deal
  9:  * in the Software without restriction, including without limitation the rights
 10:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11:  * copies of the Software, and to permit persons to whom the Software is
 12:  * furnished to do so, subject to the following conditions:
 13:  *
 14:  * The above copyright notice and this permission notice shall be included in
 15:  * all copies or substantial portions of the Software.
 16:  *
 17:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23:  * THE SOFTWARE.
 24:  */
 25: namespace DSchoenbauer\Sql\Where;
 26: 
 27: /**
 28:  * Description of Where
 29:  *
 30:  * @author David Schoenbauer <dschoenbauer@gmail.com>
 31:  */
 32: class ArrayWhere implements WhereStatementInterface
 33: {
 34: 
 35:     private $data = [];
 36:     private $whereData = [];
 37:     private $fieldOperator = 'and';
 38:     private $rowOperator = 'or';
 39:     private $useParanthesis;
 40:     private $saltSeed = 1;
 41: 
 42:     public function __construct($whereData, $fieldOperator = 'and', $rowOperator = 'or', $setUseParanthesis = true)
 43:     {
 44:         $this->setWhereData($whereData)
 45:             ->setFieldOperator($fieldOperator)
 46:             ->setRowOperator($rowOperator)
 47:             ->setUseParanthesis($setUseParanthesis);
 48:     }
 49: 
 50:     public function getStatement()
 51:     {
 52:         return $this->recursiveStatement($this->getWhereData());
 53:     }
 54: 
 55:     protected function recursiveStatement(array $data)
 56:     {
 57:         $sql = [];
 58:         if (!$this->isAssocArray($data)) {
 59:             foreach ($data as $row) {
 60:                 $sql[] = $this->recursiveStatement($row);
 61:             }
 62:         } else {
 63:             $sql[] = $this->buildRow($data, $this->saltSeed++);
 64:         }
 65:         return "(" . implode(") " . $this->getRowOperator() . " (", $sql) . ")";
 66:     }
 67: 
 68:     public function buildRow(array $assocArray, $keySalt)
 69:     {
 70:         $prefix = $keySuffix = ($this->getUseParanthesis() ? "(" : "");
 71:         $suffix = $keyPrefix = ($this->getUseParanthesis() ? ")" : "");
 72: 
 73:         $glue = $keyPrefix . ' ' . $this->getFieldOperator() . ' ' . $keySuffix;
 74:         return $prefix . implode($glue, array_map(function ($key, $value) use ($keySalt) {
 75:                     $saltedKey = $key . "-" . $keySalt;
 76:                     $this->addData($saltedKey, $value);
 77:                     return sprintf('%s = :%s', $key, $saltedKey);
 78:         }, array_keys($assocArray), array_values($assocArray))) . $suffix;
 79:     }
 80: 
 81:     protected function isAssocArray(array $array)
 82:     {
 83:         return count(array_filter(array_keys($array), 'is_string')) > 0;
 84:     }
 85: 
 86:     public function getFieldOperator()
 87:     {
 88:         return $this->fieldOperator;
 89:     }
 90: 
 91:     public function setFieldOperator($logicalOperator)
 92:     {
 93:         $this->fieldOperator = $logicalOperator;
 94:         return $this;
 95:     }
 96: 
 97:     public function getRowOperator()
 98:     {
 99:         return $this->rowOperator;
100:     }
101: 
102:     public function setRowOperator($rowOperator)
103:     {
104:         $this->rowOperator = $rowOperator;
105:         return $this;
106:     }
107: 
108:     /**
109:      *
110:      * @return array
111:      */
112:     public function getData()
113:     {
114:         return $this->data;
115:     }
116: 
117:     public function addData($key, $value)
118:     {
119:         $this->data[$key] = $value;
120:         return $this;
121:     }
122: 
123:     public function getUseParanthesis()
124:     {
125:         return $this->useParanthesis;
126:     }
127: 
128:     public function setUseParanthesis($useParanthesis = true)
129:     {
130:         $this->useParanthesis = $useParanthesis;
131:         return $this;
132:     }
133: 
134:     public function getWhereData()
135:     {
136:         return $this->whereData;
137:     }
138: 
139:     public function setWhereData($whereData)
140:     {
141:         $this->whereData = $whereData;
142:         return $this;
143:     }
144: }
145: 
API documentation generated by ApiGen