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

Builder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 0
loc 175
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 4 1
A expr() 0 4 1
A raw() 0 11 2
A table() 0 7 2
A from() 0 6 1
A select() 0 8 1
A getDialectStatement() 0 14 2
A fixTable() 0 11 4
A defaultSettings() 0 12 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;
16
17
use Phossa2\Query\Misc\Raw;
18
use Phossa2\Query\Dialect\Mysql;
19
use Phossa2\Query\Misc\Expression;
20
use Phossa2\Shared\Base\ObjectAbstract;
21
use Phossa2\Query\Traits\SettingsTrait;
22
use Phossa2\Query\Traits\PreviousTrait;
23
use Phossa2\Query\Traits\DialectAwareTrait;
24
use Phossa2\Query\Traits\ParameterAwareTrait;
25
use Phossa2\Query\Interfaces\BuilderInterface;
26
use Phossa2\Query\Interfaces\DialectInterface;
27
use Phossa2\Query\Interfaces\StatementInterface;
28
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface;
29
30
/**
31
 * Builder
32
 *
33
 * @package Phossa2\Query
34
 * @author  Hong Zhang <[email protected]>
35
 * @see     ObjectAbstract
36
 * @see     BuilderInterface
37
 * @see     StatementInterface
38
 * @see     SelectStatementInterface
39
 * @version 2.0.0
40
 * @since   2.0.0 added
41
 */
42
class Builder extends ObjectAbstract implements BuilderInterface
43
{
44
    use DialectAwareTrait, SettingsTrait, ParameterAwareTrait, PreviousTrait;
45
46
    /**
47
     * tables
48
     *
49
     * @var    array
50
     * @access protected
51
     */
52
    protected $tables = [];
53
54
    /**
55
     * Constructor
56
     *
57
     * ```php
58
     * // builder with default table `users` and Mysql dialect
59
     * $users = new Builder('users', new Mysql())
60
     *
61
     * // builder with defult tables:  `users` and `accounts` AS `a`
62
     * $builder = new Builder(['users', 'accounts' => 'a'])
63
     *
64
     * // change default settings
65
     * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]);
66
     * ```
67
     *
68
     * @param  string|array $table table[s] to build upon
69
     * @param  DialectInterface $dialect default dialect is `Mysql`
70
     * @param  array $settings builder settings
71
     * @access public
72
     */
73
    public function __construct(
74
        $table = '',
75
        DialectInterface $dialect = null,
76
        array $settings = []
77
    ) {
78
        $this
79
            ->initParameter()
80
            ->setSettings(array_replace($this->defaultSettings(), $settings))
81
            ->setDialect($dialect)
82
            ->table($table);
83
    }
84
85
    /**
86
     * Change table[s]
87
     *
88
     * @param  $table change to table[s]
89
     * @return $this
90
     * @access public
91
     */
92
    public function __invoke($table)
93
    {
94
        return $this->table($table);
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function expr()/*# : ExpressionInterface */
101
    {
102
        return new Expression($this);
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function raw(/*# string */ $string)/*# : RawInterface */
109
    {
110
        // values found
111
        if (func_num_args() > 1) {
112
            $values = func_get_arg(1);
113
            $string = $this->getParameter()
114
                ->replaceQuestionMark($string, $values);
115
        }
116
117
        return new Raw($string, $this);
118
    }
119
120
    /**
121
     * If has existing tables, return a new instance with provided table[s]
122
     *
123
     * {@inheritDoc}
124
     */
125
    public function table($table, /*# string */ $alias = '')
126
    {
127
        $tbl = $this->fixTable($table, $alias);
128
        $clone = [] === $this->tables ? $this : clone $this;
129
        $clone->tables = $tbl;
130
        return $clone;
131
    }
132
133
    /**
134
     * Append to existing tables
135
     *
136
     * {@inheritDoc}
137
     */
138
    public function from($table, /*# string */ $alias = '')
139
    {
140
        $tbl = $this->fixTable($table, $alias);
141
        $this->tables = array_merge($this->tables, $tbl);
142
        return $this;
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    public function select(
149
        $col = '',
150
        /*# string */ $alias = ''
151
    )/*# : SelectStatementInterface */ {
152
        /* @var SelectStatementInterface $select */
153
        $select = $this->getDialectStatement('select');
154
        return $select->table($this->tables)->col($col, $alias);
155
    }
156
157
    /**
158
     * Get the statement
159
     *
160
     * @param  string $name statement name, such as 'select'
161
     * @access protected
162
     */
163
    protected function getDialectStatement(
164
        /*# string */ $name
165
    )/*# : StatementInterface */ {
166
        /* @var StatementInterface $stmt */
167
        $stmt = $this->getDialect()->{$name}($this);
168
169
        // dealing with previous statement
170
        if ($this->hasPrevious()) {
171
            $stmt->setPrevious($this->getPrevious());
172
            $this->setPrevious();
173
        }
174
175
        return $stmt;
176
    }
177
178
    /**
179
     * Convert to [$table => alias] or [$table]
180
     *
181
     * @param  string|string[] $table
182
     * @param  string $alias
183
     * @return array
184
     * @access protected
185
     */
186
    protected function fixTable(
187
        $table,
188
        /*# string */ $alias = ''
189
    )/*# : array */ {
190
        if (empty($table)) {
191
            $table = [];
192
        } elseif (!is_array($table)) {
193
            $table = empty($alias) ? [$table] : [$table => $alias];
194
        }
195
        return $table;
196
    }
197
198
    /**
199
     * Builder default settings
200
     *
201
     * @return array
202
     * @access protected
203
     */
204
    protected function defaultSettings()/*# : array */
205
    {
206
        return [
207
            'autoQuote' => true,
208
            'positionedParam' => false,
209
            'namedParam' => false,
210
            'seperator' => ' ',
211
            'indent' => '',
212
            'escapeFunction' => null,
213
            'useNullAsDefault' => false,
214
        ];
215
    }
216
}
217