Completed
Push — develop ( 7ff4c1...b05481 )
by Sander
02:22
created

PosttypeBuilder::setBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sanderdekroon\Parlant\Builder;
4
5
use InvalidArgumentException;
6
use Sanderdekroon\Parlant\Container;
7
use Sanderdekroon\Parlant\Grammar\PosttypeGrammar;
8
use Sanderdekroon\Parlant\Compiler\PosttypeCompiler;
9
use Sanderdekroon\Parlant\Configurator\ParlantConfigurator;
10
use Sanderdekroon\Parlant\Configurator\ConfiguratorInterface;
11
12
class PosttypeBuilder implements BuilderInterface
13
{
14
    use BuildsQueries, QueriesMeta, QueriesTaxonomies;
15
16
17
    protected $grammar;
18
    
19
    protected $compiler;
20
    protected $bindings;
21
22
    protected $configuration;
23
24
25 32
    public function __construct(Container $container = null)
26
    {
27 32
        $this->grammar = new PosttypeGrammar; // Replace via DI
28 32
        $this->compiler = new PosttypeCompiler($this->getGrammar()); // Replace via DI
29 32
        $this->bindings = $container ?: new Container;
30 32
    }
31
32
33
    public function configure($configuration)
34
    {
35
        // If the supplied configuration is an instance of the ConfiguratorInterface
36
        // we can add it directly to the PosttypeBuilder. This way a developer
37
        // can supply their own configurator implementation.
38
        if ($configuration instanceof ConfiguratorInterface) {
39
            return $this->configuration = $configuration;
40
        }
41
42
        // If the developer wants to add additional configuration but has not
43
        // supplied an instance of the ConfiguratorInterface, we'll create
44
        // a new instance of our own implementation of the configurator.
45
        if (is_null($this->getConfiguration())) {
46
            $this->configuration = new ParlantConfigurator;
47
        }
48
49
        return $this->configuration->add($configuration);
50
    }
51
52
53 3
    public function setConfig($key, $value)
54
    {
55 3
        if ($this->requiresConfiguration()) {
56
            $this->applyDefaultConfiguration();
57
        }
58
59 3
        return $this->updateConfiguration($key, $value);
60
    }
61
62
63 3
    protected function updateConfiguration($key, $value)
64
    {
65 3
        $this->getConfiguration()->add($key, $value);
66
67 3
        return $this;
68
    }
69
70
    /**
71
     * Determine if the builder requires additional configuration.
72
     * @return bool
73
     */
74 32
    protected function requiresConfiguration()
75
    {
76 32
        return is_null($this->getConfiguration());
77
    }
78
79
    /**
80
     * Fill the configuration property with an instance of our ParlantConfigurator.
81
     * @return ParlantConfigurator
82
     */
83 32
    protected function applyDefaultConfiguration()
84
    {
85 32
        return $this->configuration = new ParlantConfigurator;
86
    }
87
88
    /**
89
     * Set the posttype the developer is querying.
90
     * @param  string $posttype
91
     * @return $this
92
     */
93 32
    public function type($posttype)
94
    {
95
        // Since this is the entry method (for now), we'll assume the developer allready
96
        // has supplied some form of configuration. If nothing is found, we'll create a
97
        // new instance of our ParlantConfigurator which sets some default settings.
98 32
        if ($this->requiresConfiguration()) {
99 32
            $this->applyDefaultConfiguration();
100
        }
101
102 32
        $this->setBinding('post_type', $posttype);
103 32
        return $this;
104
    }
105
106
    /**
107
     * Start a new Where clause. This simply adds the arguments to the outer array.
108
     * @param  string|array $column
109
     * @param  string|null $operator
110
     * @param  string|null $value
111
     * @return $this
112
     */
113 4
    public function where($column, $operator = null, $value = null)
114
    {
115 4
        $clause = new WhereClause($this->getGrammar());
116
117 4
        foreach ($clause->build($column, $operator, $value) as $where) {
118 3
            $this->appendBinding('wheres', $where);
119
        }
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * Limit the number of posts to return to the $number
126
     * @param  int $number
127
     * @return $this
128
     */
129 1
    public function limit($number)
130
    {
131 1
        $this->setBinding('limit', (int)$number);
132 1
        return $this;
133
    }
134
135
    /**
136
     * Set the post offset.
137
     * @param  int $number
138
     * @return $this
139
     */
140 1
    public function offset($number)
141
    {
142 1
        $this->setBinding('offset', (int)$number);
143 1
        return $this;
144
    }
145
146
    /**
147
     * Prepare the value and operator. If $useDefault is true, return the default operator (=)
148
     * Throws an exception if the operator is not supported with the current grammer.
149
     * @param  mixed        $value
150
     * @param  string       $operator
151
     * @param  boolean      $useDefault
152
     * @throws InvalidArgumentException
153
     * @return array
154
     * @todo  make deprecated
155
     */
156
    protected function prepareValueAndOperator($value, $operator, $useDefault = false, $termDefault = false)
157
    {
158
        if ($useDefault) {
159
            return [$operator, $termDefault ? 'IN' : '='];
160
        }
161
162
        if ($this->invalidOperator($operator) && !is_null($value)) {
163
            throw new InvalidArgumentException('Illegal operator and value combination.');
164
        }
165
166
        return [$value, $operator];
167
    }
168
169 32
    protected function getGrammar()
170
    {
171 32
        return $this->grammar;
172
    }
173
174 25
    protected function getCompiler()
175
    {
176 25
        return $this->compiler;
177
    }
178
179 32
    protected function getBindings()
180
    {
181 32
        return $this->bindings;
182
    }
183
184 32
    protected function getConfiguration()
185
    {
186 32
        return $this->configuration;
187
    }
188
189
    /**
190
     * Determine if an operator is invalid or unsupported
191
     * @param  string $operator
192
     * @return bool
193
     * @todo  make deprecated
194
     */
195
    private function invalidOperator($operator)
196
    {
197
        return !in_array($operator, $this->getGrammar()->getOperators());
198
    }
199
200
    /**
201
     * Set an query binding
202
     * @param  string    $key   @todo validate if the $key is valid with the grammar
203
     * @param  mixed     $data
204
     * @return bool
205
     */
206 32
    private function setBinding($key, $data)
207
    {
208 32
        return $this->getBindings()->bind($key, $data);
209
    }
210
211
    /**
212
     * Append an query binding
213
     * @param  string   $key  @todo validate if the $key is valid with the grammar
214
     * @param  mixed    $data
215
     * @return bool
216
     */
217 14
    private function appendBinding($key, $data)
218
    {
219 14
        return $this->getBindings()->append($key, $data);
220
    }
221
222
223 11
    private function getBinding($key)
0 ignored issues
show
Unused Code introduced by
The method getBinding() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
224
    {
225 11
        return $this->getBindings()->get($key);
226
    }
227
}
228