Completed
Push — master ( 4b2726...162df7 )
by Hong
03:18
created

ClauseTrait::isRaw()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 4
nop 2
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
        if (is_object($item)) {
93
            if ($item instanceof StatementInterface) {
94
                $settings = array_merge(
95
                    $settings,
96
                    ['seperator' => ' ', 'indent' => '']
97
                );
98
                return '(' . $item->getStatement($settings) . ')';
99
            }
100
            if ($item instanceof TemplateInterface) {
101
                return $item->getOutput($settings);
102
            }
103
        }
104
        return $rawMode ? (string) $item :
105
            $this->quote($item, $settings['quotePrefix'], $settings['quoteSuffix']);
106
    }
107
108
    /**
109
     * Return specific clause part
110
     *
111
     * @param  string $clauseName
112
     * @param  array
113
     * @access protected
114
     */
115
    protected function &getClause(/*# string */ $clauseName)/*# : array */
116
    {
117
        if (empty($clauseName)) {
118
            return $this->clause;
119
        } else {
120
            if (!isset($this->clause[$clauseName])) {
121
                $this->clause[$clauseName] = [];
122
            }
123
            return $this->clause[$clauseName];
124
        }
125
    }
126
127
    /**
128
     * Quote string even space found
129
     *
130
     * @param  string $str
131
     * @param  string $prefix
132
     * @param  string $suffix
133
     * @return string
134
     * @access protected
135
     */
136
    protected function quoteSpace(
137
        /*# string */ $str,
138
        /*# string */ $prefix,
139
        /*# string */ $suffix
140
    )/*# : string */ {
141
        return sprintf('%s%s%s', $prefix, $str, $suffix);
142
    }
143
144
    /**
145
     * Process value
146
     *
147
     * @param  mixed $value
148
     * @return string
149
     * @access protected
150
     */
151
    protected function processValue($value)/*# : string */
152
    {
153
        // @TODO processValue
154
        return (string) $value;
155
    }
156
157
    /**
158
     * Join each clause
159
     *
160
     * @param  string $prefix
161
     * @param  string $seperator
162
     * @param  array $clause
163
     * @param  array $settings
164
     * @return string
165
     * @access protected
166
     */
167
    protected function joinClause(
168
        /*# : string */ $prefix,
169
        /*# : string */ $seperator,
170
        array $clause,
171
        array $settings
172
    )/*# : string */ {
173
        if (empty($clause)) {
174
            return '';
175
        } else {
176
            $join = $settings['seperator'] . $settings['indent'];
177
            $pref = empty($prefix) ? '' : ($prefix . $join);
178
            return $settings['seperator'] . $pref . join($seperator . $join, $clause);
179
        }
180
    }
181
}
182