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