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\Command;
26:
27: use DSchoenbauer\Sql\Where\WhereStatementInterface;
28:
29: /**
30: * Common functionality all items using a where statement use.
31: *
32: * @author David Schoenbauer <dschoenbauer@gmail.com>
33: */
34: trait WhereTrait
35: {
36:
37: private $where;
38:
39: /**
40: * Adds the prefix WHERE to what the where object has provided as a where statement
41: * @return string returns a full WHERE statement will return null if not statement provided
42: * @since v1.0.0
43: */
44: public function getWhereStatement()
45: {
46: if ($this->hasWhere()) {
47: return sprintf("WHERE %s", $this->getWhere()->getStatement());
48: }
49: return null;
50: }
51:
52: /**
53: * checks to see if a where statement has been provided
54: * @return bool checks to see if a where statement has been set
55: * @since v1.0.0
56: */
57: protected function hasWhere()
58: {
59: return $this->where instanceof WhereStatementInterface;
60: }
61:
62: /**
63: * returns where statement given to object
64: * @return WhereStatementInterface provides stored where statement object
65: * @since v1.0.0
66: */
67: public function getWhere()
68: {
69: return $this->where;
70: }
71:
72: /**
73: * adds a where statement to a given statement
74: * @param WhereStatementInterface $where where statement to be used added
75: * @return inherit bubbling
76: * @since v1.0.0
77: */
78: public function setWhere(WhereStatementInterface $where = null)
79: {
80: $this->where = $where;
81: return $this;
82: }
83:
84: /**
85: * returns an array that can be used in a prepared statement
86: * @return array an array of data specific to the data of the where statement
87: * @since v1.0.0
88: */
89: public function getWhereData()
90: {
91: $whereData = [];
92: if ($this->hasWhere()) {
93: $whereData = $this->getWhere()->getData();
94: }
95: return $whereData;
96: }
97: }
98: