Completed
Push — master ( a7b8c4...d685b1 )
by Jared
02:06
created

UnionStatement::addQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
        // build each query and concatenate
50
        $queries = [];
51
        $this->values = [];
52
        foreach ($this->queries as $row) {
53
            list($query, $type) = $row;
54
55
            $prefix = 'UNION ';
56
            if ($type) {
57
                $prefix .= $type.' ';
58
            }
59
60
            $queries[] = $prefix.$query->build();
61
62
            $this->values = array_merge($this->values, $query->getValues());
63
        }
64
65
        return implode(' ', $queries);
66
    }
67
}
68