Completed
Push — master ( 162df7...9060a0 )
by Hong
02:04
created

ClauseTrait::joinClause()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 11
nc 3
nop 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\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\RawInterface;
18
use Phossa2\Query\Interfaces\ClauseInterface;
19
use Phossa2\Query\Interfaces\TemplateInterface;
20
use Phossa2\Query\Interfaces\StatementInterface;
21
22
/**
23
 * ClauseTrait
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ClauseInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait ClauseTrait
32
{
33
    use QuoteTrait;
34
35
    /**
36
     * storage for clauses
37
     *
38
     * @var    array
39
     * @access protected
40
     */
41
    protected $clause = [];
42
43
    /**
44
     * Is $str a raw sql string ?
45
     *
46
     * @param  mixed $str
47
     * @param  bool $rawMode
48
     * @access protected
49
     */
50
    protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */
51
    {
52
        if ($rawMode) {
53
            return true;
54
        }
55
56
        if (is_string($str)) {
57
            return (bool) preg_match('/[^0-9a-zA-Z\$_.]/', $str);
58
        }
59
60
        return is_object($str) && $str instanceof RawInterface;
61
    }
62
63
    /**
64
     * Quote an alias if not an int
65
     *
66
     * @param  int|string $alias
67
     * @param  array $settings
68
     * @return string
69
     * @access protected
70
     */
71
    protected function quoteAlias($alias, array $settings)/*# : string */
72
    {
73
        $prefix = $settings['quotePrefix'];
74
        $suffix = $settings['quoteSuffix'];
75
        return is_int($alias) ?
76
            '' : (' AS ' . $this->quoteSpace($alias, $prefix, $suffix));
77
    }
78
79
    /**
80
     * Quote an item if it is a field/column
81
     *
82
     * @param  string|StatementInterface $item
83
     * @param  array $settings
84
     * @param  bool $rawMode
85
     * @access protected
86
     */
87
    protected function quoteItem(
88
        $item,
89
        array $settings,
90
        /*# bool */ $rawMode = false
91
    )/*# : string */ {
92
        // object
93
        if (is_object($item)) {
94
            return $this->quoteObject($item, $settings);
95
        }
96
97
        // string
98
        return $rawMode ? (string) $item :
99
            $this->quote($item, $settings['quotePrefix'], $settings['quoteSuffix']);
100
    }
101
102
    /**
103
     * Quote object
104
     *
105
     * @param  object $object
106
     * @param  array $settings
107
     * @return string
108
     * @access protected
109
     */
110
    protected function quoteObject($object, $settings)/*# : string */
111
    {
112
        if ($object instanceof StatementInterface) {
113
            $settings = array_merge(
114
                $settings,
115
                ['seperator' => ' ', 'indent' => '']
116
            );
117
            return '(' . $object->getStatement($settings) . ')';
118
        }
119
        if ($object instanceof TemplateInterface) {
120
            return $object->getOutput($settings);
121
        }
122
        return (string) $object;
123
    }
124
125
    /**
126
     * Return specific clause part
127
     *
128
     * @param  string $clauseName
129
     * @param  array
130
     * @access protected
131
     */
132
    protected function &getClause(/*# string */ $clauseName)/*# : array */
133
    {
134
        if (empty($clauseName)) {
135
            return $this->clause;
136
        } else {
137
            if (!isset($this->clause[$clauseName])) {
138
                $this->clause[$clauseName] = [];
139
            }
140
            return $this->clause[$clauseName];
141
        }
142
    }
143
144
    /**
145
     * Quote string even space found
146
     *
147
     * @param  string $str
148
     * @param  string $prefix
149
     * @param  string $suffix
150
     * @return string
151
     * @access protected
152
     */
153
    protected function quoteSpace(
154
        /*# string */ $str,
155
        /*# string */ $prefix,
156
        /*# string */ $suffix
157
    )/*# : string */ {
158
        return sprintf('%s%s%s', $prefix, $str, $suffix);
159
    }
160
161
    /**
162
     * Process value
163
     *
164
     * @param  mixed $value
165
     * @return string
166
     * @access protected
167
     */
168
    protected function processValue($value)/*# : string */
169
    {
170
        // @TODO processValue
171
        return (string) $value;
172
    }
173
174
    /**
175
     * Join each clause
176
     *
177
     * @param  string $prefix
178
     * @param  string $seperator
179
     * @param  array $clause
180
     * @param  array $settings
181
     * @return string
182
     * @access protected
183
     */
184
    protected function joinClause(
185
        /*# : string */ $prefix,
186
        /*# : string */ $seperator,
187
        array $clause,
188
        array $settings
189
    )/*# : string */ {
190
        if (empty($clause)) {
191
            return '';
192
        } else {
193
            $join = $settings['seperator'] . $settings['indent'];
194
            $pref = empty($prefix) ? '' : ($prefix . $join);
195
            return $settings['seperator'] . $pref . join($seperator . $join, $clause);
196
        }
197
    }
198
}
199