Completed
Push — master ( 226980...c1625e )
by Hong
02:50
created

UnionTrait::setPrevious()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\BuilderInterface;
18
use Phossa2\Query\Interfaces\Clause\UnionInterface;
19
20
/**
21
 * UnionTrait
22
 *
23
 * Implementation of UnionInterface
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     UnionInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait UnionTrait
32
{
33
    /**
34
     * 0 NO, 1 YES, 2 UNION ALL
35
     * @var    int
36
     * @access protected
37
     */
38
    protected $is_union = UnionInterface::UNION_NOT;
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function union()/*# : SelectStatementInterface */
44
    {
45
        $this->is_union = UnionInterface::UNION_YES;
46
        return $this->getBuilder()->select()->table('')->setPrevious($this);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function unionAll()/*# : SelectStatementInterface */
53
    {
54
        $this->is_union = UnionInterface::UNION_ALL;
55
        return $this->getBuilder()->select()->table('')->setPrevious($this);
56
    }
57
58
    /**
59
     * Override `getStatement()` in StatementAbstract
60
     *
61
     * {@inheritDoc}
62
     */
63
    public function getStatement(array $settings = [])/*# : string */
64
    {
65
        // combine settings
66
        $settings = $this->combineSettings($settings);
67
68
        // statements
69
        $sql = [];
70
71
        // build previous statement
72
        if ($this->hasPrevious()) {
73
            $sql[] = $this->getPrevious()->getStatement($settings);
74
        }
75
76
        // build current sql
77
        $sql[] = $this->buildSql($settings);
78
79
        // replace with ?, :name or real values
80
        return $this->bindValues(join($settings['seperator'], $sql), $settings);
81
    }
82
83
    /**
84
     * Build UNION/UNION ALL
85
     *
86
     * @param  string $prefix
87
     * @param  array $settings
88
     * @return string
89
     * @access protected
90
     */
91
    protected function buildUnion(
92
        /*# string */ $prefix,
93
        array $settings
94
    )/*# : string */ {
95
        switch ($this->is_union) {
96
            case UnionInterface::UNION_YES :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
97
                return $settings['seperator'] . 'UNION';
98
            case UnionInterface::UNION_ALL :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
99
                return $settings['seperator'] . 'UNION ALL';
100
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
101
                return $prefix;
102
        }
103
    }
104
105
    abstract protected function hasPrevious()/*# : bool */;
106
    abstract protected function getPrevious()/*# : StatementInterface */;
107
    /**
108
     * Return the builder
109
     *
110
     * @return BuilderInterface
111
     * @access public
112
     */
113
    abstract public function getBuilder()/*# : BuilderInterface */;
114
    abstract protected function combineSettings(array $settings)/*# : array */;
115
    abstract protected function buildSql(array $settings)/*# : string */;
116
    abstract protected function bindValues(/*# string */ $sql, array $settings)/*# : string */;
117
}
118