ArrayWhere::isAssocArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * The MIT License
4
 *
5
 * Copyright 2017 David Schoenbauer <[email protected]>.
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 <[email protected]>
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
    private $statement;
42
43 17
    public function __construct($whereData, $fieldOperator = 'and', $rowOperator = 'or', $setUseParanthesis = true)
44
    {
45 17
        $this->setFieldOperator($fieldOperator)
46 17
            ->setRowOperator($rowOperator)
47 17
            ->setUseParanthesis($setUseParanthesis)
48 17
            ->setWhereData($whereData);
49 17
    }
50
51 8
    public function getStatement()
52
    {
53 8
        $this->build($this->getWhereData());
54 8
        return $this->statement;
55
    }
56
57 8
    public function setStatement($statement)
58
    {
59 8
        $this->statement = $statement;
60 8
        return $this;
61
    }
62
63 8
    protected function build(array $data)
64
    {
65 8
        $this->saltSeed = 1;
66 8
        $this->setStatement($this->recursiveStatement($data));
67 8
    }
68
69 8
    protected function recursiveStatement(array $data)
70
    {
71 8
        $sqlStatements = [];
72 8
        if (!$this->isAssocArray($data)) {
73 1
            foreach ($data as $row) {
74 1
                $sqlStatements[] = $this->recursiveStatement($row);
75
            }
76
        } else {
77 8
            $sqlStatements[] = $this->buildRow($data, $this->saltSeed);
78 8
            $this->saltSeed++;
79
        }
80
81 8
        list($prefix, $keyPrefix, $keySuffix, $suffix) = $this->getParenthesis($sqlStatements, 1);
82
83 8
        return $prefix . implode($keySuffix . $this->getRowOperator() . $keyPrefix, $sqlStatements) . $suffix;
84
    }
85
86 8
    private function getParenthesis(array $qualifyingArray = [], $count = -1)
87
    {
88
        //prefix, keyPrefix, keySuffix, suffix
89 8
        $parenthesis = [null, null, null, null];
90 8
        if ($this->getUseParanthesis() && count($qualifyingArray) > $count) {
91 7
            $parenthesis = ["(", " (", ") ", ")"];
92
        }
93 8
        return $parenthesis;
94
    }
95
96 8
    public function buildRow(array $assocArray, $keySalt)
97
    {
98 8
        list($prefix, $keySuffix, $keyPrefix,  $suffix) = $this->getParenthesis();
99
100 8
        $glue = $keyPrefix . $this->getFieldOperator() . $keySuffix;
101 8
        return $prefix . implode($glue, array_map(function ($key, $value) use ($keySalt) {
102 8
                    $saltedKey = $key . "_" . $keySalt;
103 8
                    $this->addData($saltedKey, $value);
104 8
                    return sprintf('%s = :%s', $key, $saltedKey);
105 8
        }, array_keys($assocArray), array_values($assocArray))) . $suffix;
106
    }
107
108 8
    protected function isAssocArray(array $array)
109
    {
110 8
        return count(array_filter(array_keys($array), 'is_string')) > 0;
111
    }
112
113 10
    public function getFieldOperator()
114
    {
115 10
        return $this->fieldOperator;
116
    }
117
118 17
    public function setFieldOperator($logicalOperator)
119
    {
120 17
        $this->fieldOperator = $logicalOperator;
121 17
        return $this;
122
    }
123
124 10
    public function getRowOperator()
125
    {
126 10
        return $this->rowOperator;
127
    }
128
129 17
    public function setRowOperator($rowOperator)
130
    {
131 17
        $this->rowOperator = $rowOperator;
132 17
        return $this;
133
    }
134
135
    /**
136
     *
137
     * @return array
138
     */
139 8
    public function getData()
140
    {
141 8
        $this->build($this->getWhereData());
142 8
        return $this->data;
143
    }
144
145 8
    public function addData($key, $value)
146
    {
147 8
        $this->data[$key] = $value;
148 8
        return $this;
149
    }
150
151 11
    public function getUseParanthesis()
152
    {
153 11
        return $this->useParanthesis;
154
    }
155
156 17
    public function setUseParanthesis($useParanthesis = true)
157
    {
158 17
        $this->useParanthesis = $useParanthesis;
159 17
        return $this;
160
    }
161
162 10
    public function getWhereData()
163
    {
164 10
        return $this->whereData;
165
    }
166
167 17
    public function setWhereData($whereData)
168
    {
169 17
        $this->whereData = $whereData;
170 17
        return $this;
171
    }
172
}
173