DefaultParser::setQuote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel\Storage\Pdo\Parser;
12
13
use Borobudur\Cqrs\Collection;
14
use Borobudur\Cqrs\ReadModel\Storage\Pdo\PdoFinder;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/18/15
19
 */
20
class DefaultParser implements ParserInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $quote;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setQuote($quote)
31
    {
32
        $this->quote = $quote;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function comparison($field, $comparison, $value)
39
    {
40
        return
41
            $this->quote($field) . ' ' .
42
            $this->transformComparison($comparison, $value) . ' ' .
43
            $this->normalizeValue($value);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function normalizeGroupValue($value)
50
    {
51
        return sprintf('(%s)', $this->normalizeCollectionValue($value));
52
    }
53
54
    /**
55
     * @param string $comparison
56
     * @param mixed  $value
57
     *
58
     * @return string
59
     */
60
    protected function transformComparison($comparison, $value)
61
    {
62
        if (null === $value) {
63
            switch ($comparison) {
64
                case "=":
65
                    return "IS";
66
                case "!=":
67
                case "<>":
68
                    return "IS NOT";
69
            }
70
        }
71
72
        return $comparison;
73
    }
74
75
    /**
76
     * Normalize value.
77
     *
78
     * @param mixed $value
79
     *
80
     * @return array|float|int|string
81
     */
82
    protected function normalizeValue($value)
83
    {
84
        if (null === $value) {
85
            $value = "NULL";
86
        } elseif (is_array($value)) {
87
            foreach ($value as $index => $val) {
88
                $value[$index] = $this->normalizeValue($val);
89
            }
90
        } elseif (is_string($value) && !preg_match('/^\(/', $value)) {
91
            $value = sprintf('\'%s\'', addslashes($value));
92
        } elseif (is_int($value)) {
93
            $value = (int) $value;
94
        } elseif (is_float($value)) {
95
            $value = (float) $value;
96
        } elseif (is_bool($value)) {
97
            $value = $value === true ? 'true' : 'false';
98
        }
99
100
        return $value;
101
    }
102
103
    /**
104
     * Normalize collection value.
105
     *
106
     * @param mixed $value
107
     *
108
     * @return array|string
109
     */
110
    protected function normalizeCollectionValue($value)
111
    {
112
        if ($value instanceof PdoFinder) {
113
            return (string) $value;
114
        }
115
116
        if ($value instanceof Collection) {
117
            $value = $value->toArray();
118
        }
119
120
        if (is_array($value)) {
121
            return implode(', ', $this->normalizeValue($value));
122
        }
123
124
        return $value;
125
    }
126
127
    /**
128
     * Quoting
129
     *
130
     * @param string $field
131
     *
132
     * @return string
133
     */
134 View Code Duplication
    protected function quote($field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
135
    {
136
        $parts = explode('.', $field);
137
        $quotes = array();
138
139
        foreach ($parts as $part) {
140
            $quotes[] = $this->quote . $part . $this->quote;
141
        }
142
143
        return implode('.', $quotes);
144
    }
145
}
146