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

QuoteTrait::quote()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 12
nc 2
nop 3
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  string $prefix
35
     * @param  string $suffix
36
     * @return string
37
     * @access protected
38
     */
39
    protected function quote(
40
        /*# string */ $str,
41
        /*# string */ $prefix,
42
        /*# string */ $suffix
43
    )/*# : string */ {
44
        // pattern
45
        $pattern = '/^[a-zA-Z\$][0-9a-zA-Z_.\$]*+$/';
46
        if (preg_match($pattern, $str)) {
47
            return preg_replace_callback(
48
                '/\b([a-zA-Z\$][0-9a-zA-Z_\$]*+)\b/',
49
                function($m) use ($prefix, $suffix) {
50
                    return sprintf('%s%s%s', $prefix, $m[1], $suffix);
51
                },
52
                $str
53
            );
54
        }
55
        return $str;
56
    }
57
}
58