Completed
Push — master ( f56a01...af237a )
by Hong
02:39
created

StatementAbstract::setPrevous()   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;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Query\Interfaces\BuilderInterface;
19
use Phossa2\Query\Interfaces\StatementInterface;
20
21
/**
22
 * StatementAbstract
23
 *
24
 * @package Phossa2\Query
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ObjectAbstract
27
 * @see     StatementInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface
32
{
33
    use SettingsTrait, BuilderAwareTrait, PreviousTrait;
34
35
    /**
36
     * value bindings
37
     *
38
     * @var    array
39
     * @access protected
40
     */
41
    protected $bindings;
42
43
    /**
44
     * Statement type, 'SELECT' etc.
45
     *
46
     * @var    string
47
     * @access protected
48
     */
49
    protected $type = '';
50
51
    /**
52
     * clause configs,[name => prefix]
53
     *
54
     * @var    array
55
     * @access protected
56
     */
57
    protected $configs = [];
58
59
    /**
60
     * Constructor
61
     *
62
     * @param  BuilderInterface $builder
63
     * @access public
64
     */
65
    public function __construct(BuilderInterface $builder)
66
    {
67
        $this->setBuilder($builder);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function getStatement(array $settings = [])/*# : string */
74
    {
75
        // combine settings
76
        $settings = $this->combineSettings($settings);
77
78
        // statements
79
        $sql = [];
80
81
        // build previous statement
82
        if ($this->hasPrevious()) {
83
            $sql[] = $this->getPrevious()->getStatement($settings);
84
        }
85
86
        // build current sql
87
        $sql[] = $this->buildSql($settings);
88
89
        // replace with ?, :name or real values
90
        return $this->bindValues(join($settings['seperator'], $sql), $settings);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function getBindings()/*# : array */
97
    {
98
        return $this->bindings;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function __toString()/*# : string */
105
    {
106
        return $this->getStatement([
107
            'positionedParam' => false,
108
            'namedParam' => false,
109
        ]);
110
    }
111
112
    /**
113
     * Combine builder/statement and provided settings
114
     *
115
     * @param  array $settings statement specific settings
116
     * @return array
117
     * @access protected
118
     */
119
    protected function combineSettings(array $settings)/*# : array */
120
    {
121
        return array_replace(
122
            $this->getBuilder()->getSettings(), // builder settings
123
            $this->getBuilder()->getDialect()->dialectSettings(), // dialect
124
            $settings,  // user settings
125
            $this->getSettings() // statement settings
126
        );
127
    }
128
129
    /**
130
     * Build SQL statement clauses
131
     *
132
     * @param  array $settings
133
     * @param  string
134
     * @access protected
135
     */
136
    protected function buildSql(array $settings)/*# : string */
137
    {
138
        $result = $this->type;
139
        $settings['join'] = $settings['seperator'] . $settings['indent'];
140
        foreach ($this->configs as $clause => $prefix) {
141
            $method = 'build' . ucfirst(strtolower($clause));
142
            $result .= $this->{$method}($prefix, $settings);
143
        }
144
        return $result;
145
    }
146
147
    /**
148
     * Replace placeholders in the sql with '?', ':named' or real value
149
     *
150
     * @param  string $sql
151
     * @param  array $settings
152
     * @return string
153
     * @access protected
154
     */
155
    protected function bindValues(/*# string */ $sql, array $settings)
156
    {
157
        // init bindings
158
        $this->bindings = [];
159
160
        // bind values
161
        return $this->getBuilder()->getParameter()
162
            ->bindValues($sql, $this->bindings, $settings);
163
    }
164
165
    /**
166
     * Set previous statement
167
     *
168
     * @param  StatementInterface $stmt
169
     * @return $this
170
     * @access protected
171
     */
172
    protected function setPrevous(StatementInterface $stmt)
173
    {
174
        $this->previous = $stmt;
175
        return $this;
176
    }
177
}
178