Completed
Push — master ( 3152a4...36e9e8 )
by Hong
02:25
created

StatementAbstract::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
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;
34
35
    /**
36
     * value bindings
37
     *
38
     * @var    array
39
     * @access protected
40
     */
41
    protected $bindings;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param  BuilderInterface $builder
47
     * @access public
48
     */
49
    public function __construct(BuilderInterface $builder)
50
    {
51
        $this->setBuilder($builder);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function getStatement(array $settings = [])/*# : string */
58
    {
59
        // combine settings
60
        $settings = $this->combineSettings($settings);
61
62
        // build sql
63
        $sql = $this->buildSql($settings);
64
65
        // replace with ?, :name or real values
66
        return $this->bindValues($sql, $settings);
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function getBindings()/*# : array */
73
    {
74
        return $this->bindings;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function __toString()/*# : string */
81
    {
82
        return $this->getStatement([
83
            'positionedParam' => false,
84
            'namedParam' => false,
85
        ]);
86
    }
87
88
    /**
89
     * Combine builder/statement and provided settings
90
     *
91
     * @param  array $settings statement specific settings
92
     * @return array
93
     * @access protected
94
     */
95
    protected function combineSettings(array $settings)/*# : array */
96
    {
97
        return array_replace(
98
            $this->getBuilder()->getSettings(), // builder settings
99
            $this->getBuilder()->getDialect()->dialectSettings(), // dialect
100
            $settings,  // user settings
101
            $this->getSettings() // statement settings
102
        );
103
    }
104
105
    /**
106
     * Build SQL statement clauses
107
     *
108
     * @param  array $settings
109
     * @param  string
110
     * @access protected
111
     */
112
    protected function buildSql(array $settings)/*# : string */
113
    {
114
        $result = $this->getType();
115
        $settings['join'] = $settings['seperator'] . $settings['indent'];
116
        foreach ($this->getConfigs() as $clause) {
117
            $method = 'build' . ucfirst(strtolower($clause));
118
            $result .= $this->{$method}($settings);
119
        }
120
        return $result;
121
    }
122
123
    /**
124
     * Replace placeholders in the sql with '?', ':named' or real value
125
     *
126
     * @param  string $sql
127
     * @param  array $settings
128
     * @return string
129
     * @access protected
130
     */
131
    protected function bindValues(/*# string */ $sql, array $settings)
132
    {
133
        // init bindings
134
        $this->bindings = [];
135
136
        // bind values
137
        return $this->getBuilder()->getParameter()
138
            ->bindValues($sql, $this->bindings, $settings);
139
    }
140
141
    /**
142
     * Get the statement type, such as 'SELECT'
143
     *
144
     * @return string
145
     * @access protected
146
     */
147
    protected function getType()/*# : string */
148
    {
149
        return '';
150
    }
151
152
    /**
153
     * Get clause configurations
154
     *
155
     * @return array
156
     * @access protected
157
     */
158
    protected function getConfigs()/*# : array */
159
    {
160
        return [];
161
    }
162
}
163