UnionStatement::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace JAQB\Statement;
13
14
use JAQB\Query\SelectQuery;
15
16
class UnionStatement extends Statement
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $queries = [];
22
23
    /**
24
     * Adds a query to the statement.
25
     *
26
     * @param string $type
27
     *
28
     * @return self
29
     */
30
    public function addQuery(SelectQuery $query, $type = '')
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
            if ($type) {
58
                $type .= ' ';
59
            }
60
61
            $queries[] = 'UNION '.$type.$query->build();
62
63
            $this->values = array_merge(
64
                $this->values,
65
                $query->getValues()
66
            );
67
        }
68
69
        return implode(' ', $queries);
70
    }
71
}
72