WhereTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 64
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getWhereStatement() 0 7 2
A hasWhere() 0 4 1
A getWhere() 0 4 1
A setWhere() 0 5 1
A getWhereData() 0 8 2
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\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 <[email protected]>
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|null returns a full WHERE statement will return null if not statement provided
42
     * @since v1.0.0
43
     */
44 24
    public function getWhereStatement()
45
    {
46 24
        if ($this->hasWhere()) {
47 11
            return sprintf("WHERE %s", $this->getWhere()->getStatement());
48
        }
49 13
        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 29
    protected function hasWhere()
58
    {
59 29
        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 15
    public function getWhere()
68
    {
69 15
        return $this->where;
70
    }
71
72
    /**
73
     * adds a where statement to a given statement
74
     * @param null|WhereStatementInterface $where where statement to be used added
75
     * @return CommandInterface bubbling
76
     * @since v1.0.0
77
     */
78 50
    public function setWhere(WhereStatementInterface $where = null)
79
    {
80 50
        $this->where = $where;
81 50
        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 21
    public function getWhereData()
90
    {
91 21
        $whereData = [];
92 21
        if ($this->hasWhere()) {
93 11
            $whereData = $this->getWhere()->getData();
94
        }
95 21
        return $whereData;
96
    }
97
}
98