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

Builder::defaultSettings()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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\DialectAwareTrait;
23
use Phossa2\Query\Traits\ParameterAwareTrait;
24
use Phossa2\Query\Interfaces\BuilderInterface;
25
use Phossa2\Query\Interfaces\DialectInterface;
26
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface;
27
28
/**
29
 * Builder
30
 *
31
 * @package Phossa2\Query
32
 * @author  Hong Zhang <[email protected]>
33
 * @see     ObjectAbstract
34
 * @see     BuilderInterface
35
 * @see     SelectStatementInterface
36
 * @version 2.0.0
37
 * @since   2.0.0 added
38
 */
39
class Builder extends ObjectAbstract implements BuilderInterface
40
{
41
    use DialectAwareTrait, SettingsTrait, ParameterAwareTrait;
42
43
    /**
44
     * tables
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $tables = [];
50
51
    /**
52
     * Constructor
53
     *
54
     * ```php
55
     * // builder with default table `users` and Mysql dialect
56
     * $users = new Builder('users', new Mysql())
57
     *
58
     * // builder with defult tables:  `users` and `accounts` AS `a`
59
     * $builder = new Builder(['users', 'accounts' => 'a'])
60
     *
61
     * // change default settings
62
     * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]);
63
     * ```
64
     *
65
     * @param  string|array $table table[s] to build upon
66
     * @param  DialectInterface $dialect default dialect is `Mysql`
67
     * @param  array $settings builder settings
68
     * @access public
69
     */
70
    public function __construct(
71
        $table = '',
72
        DialectInterface $dialect = null,
73
        array $settings = []
74
    ) {
75
        $this
76
            ->initParameter()
77
            ->setSettings(array_replace($this->defaultSettings(), $settings))
78
            ->setDialect($dialect)
79
            ->table($table);
80
    }
81
82
    /**
83
     * Change table[s]
84
     *
85
     * @param  $table change to table[s]
86
     * @return $this
87
     * @access public
88
     */
89
    public function __invoke($table)
90
    {
91
        return $this->table($table);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function expr()/*# : ExpressionInterface */
98
    {
99
        return new Expression($this);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function raw(/*# string */ $string)/*# : RawInterface */
106
    {
107
        // values found
108
        if (func_num_args() > 1) {
109
            $values = func_get_args();
110
            array_shift($values);
111
            $string = $this->getParameter()
112
                ->replaceQuestionMark($string, $values);
113
        }
114
115
        return new Raw($string, $this);
116
    }
117
118
    /**
119
     * If has existing tables, return a new instance with provided table[s]
120
     *
121
     * {@inheritDoc}
122
     */
123
    public function table($table, /*# string */ $alias = '')
124
    {
125
        $tbl = $this->fixTable($table, $alias);
126
        $clone = [] === $this->tables ? $this : clone $this;
127
        $clone->tables = $tbl;
128
        return $clone;
129
    }
130
131
    /**
132
     * Append to existing tables
133
     *
134
     * {@inheritDoc}
135
     */
136
    public function from($table, /*# string */ $alias = '')
137
    {
138
        $tbl = $this->fixTable($table, $alias);
139
        $this->tables = array_merge($this->tables, $tbl);
140
        return $this;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function select(
147
        $col = '',
148
        /*# string */ $alias = ''
149
    )/*# : SelectStatementInterface */ {
150
        /* @var SelectStatementInterface $select */
151
        $select = $this->getDialect()->select($this);
152
        return $select->table($this->tables)->col($col, $alias);
153
    }
154
155
    /**
156
     * Convert to [$table => alias] or [$table]
157
     *
158
     * @param  string|string[] $table
159
     * @param  string $alias
160
     * @return array
161
     * @access protected
162
     */
163
    protected function fixTable(
164
        $table,
165
        /*# string */ $alias = ''
166
    )/*# : array */ {
167
        if (empty($table)) {
168
            $table = [];
169
        } elseif (!is_array($table)) {
170
            $table = empty($alias) ? [$table] : [$table => $alias];
171
        }
172
        return $table;
173
    }
174
175
    /**
176
     * Builder default settings
177
     *
178
     * @return array
179
     * @access protected
180
     */
181
    protected function defaultSettings()/*# : array */
182
    {
183
        return [
184
            // auto quote db identifier
185
            'autoQuote' => true,
186
187
            // default NOT using '?'
188
            'positionedParam' => false,
189
190
            // conside value ':name' as named parameter
191
            'namedParam' => false,
192
193
            // clause seperator
194
            'seperator' => ' ',
195
196
            // subline indention
197
            'indent' => '',
198
199
            // escape/quote function
200
            'escapeFunction' => null,
201
202
            // INSERT NULL instead of DEFAULT
203
            'useNullAsDefault' => false,
204
        ];
205
    }
206
}
207