Completed
Push — master ( 78fbbf...ba1e76 )
by Iqbal
02:26
created

DefaultParser::transformComparison()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 8.8571
cc 5
eloc 9
nc 5
nop 2
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 . $this->quote . ' ' .
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)) {
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