QuoteTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 34
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A quote() 0 20 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
/**
18
 * QuoteTrait
19
 *
20
 * @package Phossa2\Query
21
 * @author  Hong Zhang <[email protected]>
22
 * @version 2.0.0
23
 * @since   2.0.0 added
24
 */
25
trait QuoteTrait
26
{
27
    /**
28
     * Quote string base on settings only if '[a-zA-Z_.\$]' found
29
     *
30
     * - username to `username`
31
     * - u.username to `u`.`username`
32
     *
33
     * @param  string $str
34
     * @param  array $settings
35
     * @return string
36
     * @access protected
37
     */
38
    protected function quote(
39
        /*# string */ $str,
40
        array $settings
41
    )/*# : string */ {
42
        // pattern
43
        $pattern = '/^[a-zA-Z\$][0-9a-zA-Z_.\$]*+$/';
44
        $prefix = $settings['quotePrefix'];
45
        $suffix = $settings['quoteSuffix'];
46
47
        if (preg_match($pattern, $str)) {
48
            return preg_replace_callback(
49
                '/\b([a-zA-Z\$][0-9a-zA-Z_\$]*+)\b/',
50
                function ($m) use ($prefix, $suffix) {
51
                    return sprintf('%s%s%s', $prefix, $m[1], $suffix);
52
                },
53
                $str
54
            );
55
        }
56
        return $str;
57
    }
58
}
59