Completed
Push — master ( 77f155...38d909 )
by Hong
02:53
created

Builder::getDialectStatement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Builder::fixTable() 0 11 4
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\StatementInterface;
27
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface;
28
29
/**
30
 * Builder
31
 *
32
 * @package Phossa2\Query
33
 * @author  Hong Zhang <[email protected]>
34
 * @see     ObjectAbstract
35
 * @see     BuilderInterface
36
 * @see     StatementInterface
37
 * @see     SelectStatementInterface
38
 * @version 2.0.0
39
 * @since   2.0.0 added
40
 */
41
class Builder extends ObjectAbstract implements BuilderInterface
42
{
43
    use DialectAwareTrait, SettingsTrait, ParameterAwareTrait;
44
45
    /**
46
     * tables
47
     *
48
     * @var    array
49
     * @access protected
50
     */
51
    protected $tables = [];
52
53
    /**
54
     * Constructor
55
     *
56
     * ```php
57
     * // builder with default table `users` and Mysql dialect
58
     * $users = new Builder('users', new Mysql())
59
     *
60
     * // builder with defult tables:  `users` and `accounts` AS `a`
61
     * $builder = new Builder(['users', 'accounts' => 'a'])
62
     *
63
     * // change default settings
64
     * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]);
65
     * ```
66
     *
67
     * @param  string|array $table table[s] to build upon
68
     * @param  DialectInterface $dialect default dialect is `Mysql`
69
     * @param  array $settings builder settings
70
     * @access public
71
     */
72
    public function __construct(
73
        $table = '',
74
        DialectInterface $dialect = null,
75
        array $settings = []
76
    ) {
77
        $this
78
            ->initParameter()
79
            ->setSettings(array_replace($this->defaultSettings(), $settings))
80
            ->setDialect($dialect)
81
            ->table($table);
82
    }
83
84
    /**
85
     * Change table[s]
86
     *
87
     * @param  $table change to table[s]
88
     * @return $this
89
     * @access public
90
     */
91
    public function __invoke($table)
92
    {
93
        return $this->table($table);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function expr()/*# : ExpressionInterface */
100
    {
101
        return new Expression($this);
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function raw(/*# string */ $string)/*# : OutputInterface */
108
    {
109
        // values found
110
        if (func_num_args() > 1) {
111
            $values = func_get_arg(1);
112
            $string = $this->getParameter()
113
                ->replaceQuestionMark($string, $values);
114
        }
115
        return new Raw($string);
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
            'autoQuote' => true,
185
            'positionedParam' => false,
186
            'namedParam' => false,
187
            'seperator' => ' ',
188
            'indent' => '',
189
            'escapeFunction' => null,
190
            'useNullAsDefault' => false,
191
        ];
192
    }
193
}
194