Passed
Push — master ( 8bcaf5...500a63 )
by Glynn
03:09 queued 42s
created

Binding   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 30
c 1
b 0
f 0
dl 0
loc 165
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A asString() 0 3 1
A isRaw() 0 3 1
A hasTypeDefined() 0 3 1
A asRaw() 0 3 1
A asInt() 0 3 1
A getType() 0 3 1
A verifyType() 0 5 3
A asFloat() 0 3 1
A __construct() 0 7 2
A asJSON() 0 3 1
A getValue() 0 5 2
A asBool() 0 3 1
1
<?php
2
3
namespace Pixie;
4
5
use Pixie\Exception;
6
use Pixie\QueryBuilder\Raw;
7
8
class Binding
9
{
10
    public const STRING = '%s';
11
    public const BOOL = '%d';
12
    public const INT = '%d';
13
    public const FLOAT = '%f';
14
    public const JSON = '%s';
15
    public const RAW = ':RAW';
16
17
    /**
18
     * Holds the value to bind with
19
     *
20
     * @var mixed
21
     */
22
    protected $value;
23
24
    /**
25
     * Denotes the type
26
     *
27
     * @var string|null
28
     */
29
    protected $type;
30
31
    /**
32
     * Denotes if the field is a RAW value
33
     *
34
     * @var bool
35
     */
36
    protected $isRaw = false;
37
38
    /**
39
     * @param mixed $value
40
     * @param string|null $type
41
     */
42
    public function __construct($value, ?string $type = null)
43
    {
44
        $this->verifyType($type);
45
        $this->value = $value;
46
        $this->type = $type;
47
        if (self::RAW === $type) {
48
            $this->isRaw = true;
49
        }
50
    }
51
52
    /**
53
     * Creates a binding for a String
54
     *
55
     * @param mixed $value
56
     * @return self
57
     */
58
    public static function asString($value): self
59
    {
60
        return new Binding($value, self::STRING);
61
    }
62
63
    /**
64
     * Creates a binding for a Float
65
     *
66
     * @param mixed $value
67
     * @return self
68
     */
69
    public static function asFloat($value): self
70
    {
71
        return new Binding($value, self::FLOAT);
72
    }
73
74
    /**
75
     * Creates a binding for a Int
76
     *
77
     * @param mixed $value
78
     * @return self
79
     */
80
    public static function asInt($value): self
81
    {
82
        return new Binding($value, self::INT);
83
    }
84
85
    /**
86
     * Creates a binding for a Bool
87
     *
88
     * @param mixed $value
89
     * @return self
90
     */
91
    public static function asBool($value): self
92
    {
93
        return new Binding($value, self::BOOL);
94
    }
95
96
    /**
97
     * Creates a binding for a JSON
98
     *
99
     * @param mixed $value
100
     * @return self
101
     */
102
    public static function asJSON($value): self
103
    {
104
        return new Binding($value, self::JSON);
105
    }
106
107
    /**
108
     * Creates a binding for a Raw
109
     *
110
     * @param mixed $value
111
     * @return self
112
     */
113
    public static function asRaw($value): self
114
    {
115
        return new Binding($value, self::RAW);
116
    }
117
118
    /**
119
     * Verifies that the passed type is allowed
120
     *
121
     * @param string|null $type
122
     * @return void
123
     * @throws Exception if not a valid type.
124
     */
125
    protected function verifyType(?string $type): void
126
    {
127
        $validTypes = [self::STRING, self::BOOL, self::FLOAT, self::INT, self::JSON, self::RAW];
128
        if (null !== $type && !in_array($type, $validTypes, true)) {
129
            throw new Exception("{$type} is not a valid type to use for Bindings.", 1);
130
        }
131
    }
132
133
    /**
134
     * Checks if we have a type that will bind.
135
     *
136
     * @return bool
137
     */
138
    public function hasTypeDefined(): bool
139
    {
140
        return !\in_array($this->type, [null, self::RAW], true);
141
    }
142
143
    /**
144
     * Returns the bindings values
145
     *
146
     * @return string|int|float|bool|Raw|null
147
     */
148
    public function getValue()
149
    {
150
        return ! $this->hasTypeDefined()
151
            ? new Raw($this->value)
152
            : $this->value;
153
    }
154
155
    /**
156
     * Gets the types format Conversion Specifier
157
     *
158
     * @return string|null
159
     */
160
    public function getType(): ?string
161
    {
162
        return $this->type;
163
    }
164
165
    /**
166
     * Get denotes if the field is a RAW value
167
     *
168
     * @return bool
169
     */
170
    public function isRaw(): bool
171
    {
172
        return $this->isRaw;
173
    }
174
}
175