Completed
Push — master ( d685b1...8967da )
by Jared
02:15
created

UnionStatement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addQuery() 0 6 1
A getQueries() 0 4 1
B build() 0 24 3
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Statement;
12
13
use JAQB\Query\SelectQuery;
14
15
class UnionStatement extends Statement
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $queries = [];
21
22
    /**
23
     * Adds a query to the statement.
24
     *
25
     * @param SelectQuery  $query
26
     * @param string|false $type
27
     *
28
     * @return self
29
     */
30
    public function addQuery(SelectQuery $query, $type = false)
31
    {
32
        $this->queries[] = [$query, $type];
33
34
        return $this;
35
    }
36
37
    /**
38
     * Gets the queries for this statement.
39
     *
40
     * @return array
41
     */
42
    public function getQueries()
43
    {
44
        return $this->queries;
45
    }
46
47
    public function build()
48
    {
49
        // reset the parameterized values
50
        $this->values = [];
51
52
        // build each select query and concatenate
53
        $queries = [];
54
        foreach ($this->queries as $row) {
55
            list($query, $type) = $row;
56
57
            $prefix = 'UNION ';
58
            if ($type) {
59
                $prefix .= $type.' ';
60
            }
61
62
            $queries[] = $prefix.$query->build();
63
64
            $this->values = array_merge(
65
                $this->values,
66
                $query->getValues());
67
        }
68
69
        return implode(' ', $queries);
70
    }
71
}
72