SyntaxFormatter::quoteIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Graze\DataDb\Formatter;
4
5
use BadMethodCallException;
6
use InvalidArgumentException;
7
8
class SyntaxFormatter implements SyntaxFormatterInterface
9
{
10
    const DEFAULT_QUOTE_CHAR = '"';
11
12
    /**
13
     * @var string
14
     */
15
    protected $identifierQuote = self::DEFAULT_QUOTE_CHAR;
16
17
    /**
18
     * @param string $char
19
     *
20
     * @return static
21
     */
22
    public function setIdentifierQuote($char)
23
    {
24
        $this->identifierQuote = $char;
25
        return $this;
26
    }
27
28
    /**
29
     * @param string $identifier
30
     *
31
     * @return string
32
     */
33
    protected function quoteIdentifier($identifier)
34
    {
35
        return sprintf(
36
            '%s%s%s',
37
            $this->identifierQuote,
38
            str_replace($this->identifierQuote, $this->identifierQuote . $this->identifierQuote, $identifier),
39
            $this->identifierQuote
40
        );
41
    }
42
43
    /**
44
     * @param string $syntax
45
     * @param array  $params
46
     *
47
     * @return mixed
48
     */
49
    public function format($syntax, array $params = [])
50
    {
51
        return preg_replace_callback('/(?<!\{)\{(\w+)(?:\:(\w+))?(\|q)?\}(?!\})/i', function ($match) use ($params) {
52
            $key = $match[1];
53
            $result = $match[0];
54
            if (isset($params[$key])) {
55
                if (is_object($params[$key])) {
56 View Code Duplication
                    if (isset($match[2])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                        $result = $this->returnMethodCall($params[$key], $match[2]);
58
                    } else {
59
                        throw new BadMethodCallException("No method supplied in syntax to call for object: {$key}");
60
                    }
61 View Code Duplication
                } elseif (is_array($params[$key])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                    if (isset($match[2])) {
63
                        $result = $this->returnArrayKey($params[$key], $match[2]);
64
                    } else {
65
                        throw new InvalidArgumentException("No key supplied in syntax to call for array: {$key}");
66
                    }
67
                } elseif (is_scalar($params[$key])) {
68
                    $result = $params[$key];
69
                }
70
            }
71
            $doQuote = (isset($match[3]) && ($match[3] === '|q'));
72
            if ($doQuote) {
73
                return $this->quoteIdentifier($result);
74
            } else {
75
                return $result;
76
            }
77
        }, $syntax);
78
    }
79
80
    /**
81
     * @param object $object
82
     * @param string $method
83
     *
84
     * @return string
85
     */
86
    private function returnMethodCall($object, $method)
87
    {
88
        $method = 'get' . ucfirst($method);
89
        $result = call_user_func([$object, $method]);
90
        if ($result) {
91
            return $result;
92
        } else {
93
            throw new BadMethodCallException("The result of {$method} on object is not valid");
94
        }
95
    }
96
97
    /**
98
     * @param array  $array
99
     * @param string $key
100
     *
101
     * @return string mixed
102
     */
103
    private function returnArrayKey(array $array, $key)
104
    {
105
        if (!array_key_exists($key, $array)) {
106
            throw new InvalidArgumentException("Missing $key for array");
107
        }
108
109
        $result = $array[$key];
110
        if ($result) {
111
            return $result;
112
        } else {
113
            throw new InvalidArgumentException("The returned value for $key on array is not valid");
114
        }
115
    }
116
}
117