QueryParameterParserTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
getSession() 0 1 ?
A unorderParameters() 0 4 1
A orderParameters() 0 12 1
A getParametersType() 0 6 1
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\QueryManager;
11
12
/**
13
 * QueryParameterParserTrait
14
 *
15
 * Trait that makes query managers to parse, expand anc convert query
16
 * parameters.
17
 *
18
 * @package     Foundation
19
 * @copyright   2014 - 2017 Grégoire HUBERT
20
 * @author      Grégoire HUBERT
21
 * @license     X11 {@link http://opensource.org/licenses/mit-license.php}
22
 */
23
trait QueryParameterParserTrait
24
{
25
    abstract protected function getSession();
26
27
    /**
28
     * unorderParameters
29
     *
30
     * Transform an ordered parameters list with $1, $2 to $* parameters.
31
     *
32
     * @param  string $string
33
     * @return string
34
     */
35
    public function unorderParameters($string)
36
    {
37
        return preg_replace('/\$[0-9]+/', '$*', $string);
38
    }
39
40
    /**
41
     * orderParameters
42
     *
43
     * Transform an unordered parameters list $* to ordered $1, $2 parameters.
44
     *
45
     * @param  string $string
46
     * @return string
47
     */
48
    public function orderParameters($string)
49
    {
50
        return preg_replace_callback(
51
            '/\$\*/',
52
            function () {
53
                static $nb = 0;
54
55
                return sprintf("$%d", ++$nb);
56
            },
57
            $string
58
        );
59
    }
60
61
    /**
62
     * getParametersType
63
     *
64
     * Return an array of the type specified with the parameters if any. It is
65
     * possible to give the type when passing parameters like « SELECT … WHERE
66
     * field = $*::timestamptz ». In this case, PostgreSQL will assume the
67
     * given parameter is a timestamp. Pomm uses these type hints to convert
68
     * PHP representation to PostgreSQL data value.
69
     *
70
     * @param   mixed $string SQL query.
71
     * @return  array
72
     */
73
    public function getParametersType($string)
74
    {
75
        preg_match_all('/\$\*(?:::([\w\."]+(?:\[\])?))?/', $string, $matches);
76
77
        return str_replace('"', '', $matches[1]);
78
    }
79
}
80