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

UnionStatement::build()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 3
nop 0
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