Skip to content

Commit 0bf2175

Browse files
committed
Add Criteria::[and]iteratorType
1 parent 1227fd3 commit 0bf2175

File tree

2 files changed

+157
-92
lines changed

2 files changed

+157
-92
lines changed

src/Schema/Criteria.php

+36-92
Original file line numberDiff line numberDiff line change
@@ -118,196 +118,140 @@ public function getOffset() : int
118118
return $this->offset;
119119
}
120120

121-
public static function eqIterator() : self
121+
public static function iteratorType(int $iteratorType) : self
122122
{
123123
$self = new self();
124-
$self->iteratorType = IteratorTypes::EQ;
124+
$self->iteratorType = $iteratorType;
125125

126126
return $self;
127127
}
128128

129-
public function andEqIterator() : self
129+
public function andIteratorType(int $iteratorType) : self
130130
{
131131
$new = clone $this;
132-
$new->iteratorType = IteratorTypes::EQ;
132+
$new->iteratorType = $iteratorType;
133133

134134
return $new;
135135
}
136136

137-
public static function reqIterator() : self
137+
public static function eqIterator() : self
138138
{
139-
$self = new self();
140-
$self->iteratorType = IteratorTypes::REQ;
139+
return self::iteratorType(IteratorTypes::EQ);
140+
}
141141

142-
return $self;
142+
public function andEqIterator() : self
143+
{
144+
return $this->andIteratorType(IteratorTypes::EQ);
143145
}
144146

145-
public function andReqIterator() : self
147+
public static function reqIterator() : self
146148
{
147-
$new = clone $this;
148-
$new->iteratorType = IteratorTypes::REQ;
149+
return self::iteratorType(IteratorTypes::REQ);
150+
}
149151

150-
return $new;
152+
public function andReqIterator() : self
153+
{
154+
return $this->andIteratorType(IteratorTypes::REQ);
151155
}
152156

153157
public static function allIterator() : self
154158
{
155-
$self = new self();
156-
$self->iteratorType = IteratorTypes::ALL;
157-
158-
return $self;
159+
return self::iteratorType(IteratorTypes::ALL);
159160
}
160161

161162
public function andAllIterator() : self
162163
{
163-
$new = clone $this;
164-
$new->iteratorType = IteratorTypes::ALL;
165-
166-
return $new;
164+
return $this->andIteratorType(IteratorTypes::ALL);
167165
}
168166

169167
public static function ltIterator() : self
170168
{
171-
$self = new self();
172-
$self->iteratorType = IteratorTypes::LT;
173-
174-
return $self;
169+
return self::iteratorType(IteratorTypes::LT);
175170
}
176171

177172
public function andLtIterator() : self
178173
{
179-
$new = clone $this;
180-
$new->iteratorType = IteratorTypes::LT;
181-
182-
return $new;
174+
return $this->andIteratorType(IteratorTypes::LT);
183175
}
184176

185177
public static function leIterator() : self
186178
{
187-
$self = new self();
188-
$self->iteratorType = IteratorTypes::LE;
189-
190-
return $self;
179+
return self::iteratorType(IteratorTypes::LE);
191180
}
192181

193182
public function andLeIterator() : self
194183
{
195-
$new = clone $this;
196-
$new->iteratorType = IteratorTypes::LE;
197-
198-
return $new;
184+
return $this->andIteratorType(IteratorTypes::LE);
199185
}
200186

201187
public static function geIterator() : self
202188
{
203-
$self = new self();
204-
$self->iteratorType = IteratorTypes::GE;
205-
206-
return $self;
189+
return self::iteratorType(IteratorTypes::GE);
207190
}
208191

209192
public function andGeIterator() : self
210193
{
211-
$new = clone $this;
212-
$new->iteratorType = IteratorTypes::GE;
213-
214-
return $new;
194+
return $this->andIteratorType(IteratorTypes::GE);
215195
}
216196

217197
public static function gtIterator() : self
218198
{
219-
$self = new self();
220-
$self->iteratorType = IteratorTypes::GT;
221-
222-
return $self;
199+
return self::iteratorType(IteratorTypes::GT);
223200
}
224201

225202
public function andGtIterator() : self
226203
{
227-
$new = clone $this;
228-
$new->iteratorType = IteratorTypes::GT;
229-
230-
return $new;
204+
return $this->andIteratorType(IteratorTypes::GT);
231205
}
232206

233207
public static function bitsAllSetIterator() : self
234208
{
235-
$self = new self();
236-
$self->iteratorType = IteratorTypes::BITS_ALL_SET;
237-
238-
return $self;
209+
return self::iteratorType(IteratorTypes::BITS_ALL_SET);
239210
}
240211

241212
public function andBitsAllSetIterator() : self
242213
{
243-
$new = clone $this;
244-
$new->iteratorType = IteratorTypes::BITS_ALL_SET;
245-
246-
return $new;
214+
return $this->andIteratorType(IteratorTypes::BITS_ALL_SET);
247215
}
248216

249217
public static function bitsAnySetIterator() : self
250218
{
251-
$self = new self();
252-
$self->iteratorType = IteratorTypes::BITS_ANY_SET;
253-
254-
return $self;
219+
return self::iteratorType(IteratorTypes::BITS_ANY_SET);
255220
}
256221

257222
public function andBitsAnySetIterator() : self
258223
{
259-
$new = clone $this;
260-
$new->iteratorType = IteratorTypes::BITS_ANY_SET;
261-
262-
return $new;
224+
return $this->andIteratorType(IteratorTypes::BITS_ANY_SET);
263225
}
264226

265227
public static function bitsAllNotSetIterator() : self
266228
{
267-
$self = new self();
268-
$self->iteratorType = IteratorTypes::BITS_ALL_NOT_SET;
269-
270-
return $self;
229+
return self::iteratorType(IteratorTypes::BITS_ALL_NOT_SET);
271230
}
272231

273232
public function andBitsAllNotSetIterator() : self
274233
{
275-
$new = clone $this;
276-
$new->iteratorType = IteratorTypes::BITS_ALL_NOT_SET;
277-
278-
return $new;
234+
return $this->andIteratorType(IteratorTypes::BITS_ALL_NOT_SET);
279235
}
280236

281237
public static function overlapsIterator() : self
282238
{
283-
$self = new self();
284-
$self->iteratorType = IteratorTypes::OVERLAPS;
285-
286-
return $self;
239+
return self::iteratorType(IteratorTypes::OVERLAPS);
287240
}
288241

289242
public function andOverlapsIterator() : self
290243
{
291-
$new = clone $this;
292-
$new->iteratorType = IteratorTypes::OVERLAPS;
293-
294-
return $new;
244+
return $this->andIteratorType(IteratorTypes::OVERLAPS);
295245
}
296246

297247
public static function neighborIterator() : self
298248
{
299-
$self = new self();
300-
$self->iteratorType = IteratorTypes::NEIGHBOR;
301-
302-
return $self;
249+
return self::iteratorType(IteratorTypes::NEIGHBOR);
303250
}
304251

305252
public function andNeighborIterator() : self
306253
{
307-
$new = clone $this;
308-
$new->iteratorType = IteratorTypes::NEIGHBOR;
309-
310-
return $new;
254+
return $this->andIteratorType(IteratorTypes::NEIGHBOR);
311255
}
312256

313257
public function getIteratorType() : int

tests/Unit/Schema/CriteriaTest.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Tarantool Client package.
5+
*
6+
* (c) Eugene Leonovich <gen.work@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\Client\Tests\Unit\Schema;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Tarantool\Client\Schema\Criteria;
18+
use Tarantool\Client\Schema\IteratorTypes;
19+
20+
final class CriteriaTest extends TestCase
21+
{
22+
public function testIndexId() : void
23+
{
24+
self::assertSame(1, Criteria::index(1)->getIndex());
25+
}
26+
27+
public function testAndIndexId() : void
28+
{
29+
self::assertSame(2, Criteria::index(1)->andIndex(2)->getIndex());
30+
}
31+
32+
public function testIndexName() : void
33+
{
34+
self::assertSame('foo', Criteria::index('foo')->getIndex());
35+
}
36+
37+
public function testAndIndexName() : void
38+
{
39+
self::assertSame('bar', Criteria::index('foo')->andIndex('bar')->getIndex());
40+
}
41+
42+
public function testKey() : void
43+
{
44+
self::assertSame([1], Criteria::key([1])->getKey());
45+
}
46+
47+
public function testAndKey() : void
48+
{
49+
self::assertSame([2], Criteria::key([1])->andKey([2])->getKey());
50+
}
51+
52+
public function testLimit() : void
53+
{
54+
self::assertSame(1, Criteria::limit(1)->getLimit());
55+
}
56+
57+
public function testAndLimit() : void
58+
{
59+
self::assertSame(2, Criteria::limit(1)->andLimit(2)->getLimit());
60+
}
61+
62+
public function testOffset() : void
63+
{
64+
self::assertSame(1, Criteria::offset(1)->getOffset());
65+
}
66+
67+
public function testAndOffset() : void
68+
{
69+
self::assertSame(2, Criteria::offset(1)->andOffset(2)->getOffset());
70+
}
71+
72+
public function testIteratorType() : void
73+
{
74+
self::assertSame(IteratorTypes::ALL, Criteria::iteratorType(IteratorTypes::ALL)->getIteratorType());
75+
}
76+
77+
public function testAndIteratorType() : void
78+
{
79+
self::assertSame(IteratorTypes::GE, Criteria::iteratorType(IteratorTypes::ALL)->andIteratorType(IteratorTypes::GE)->getIteratorType());
80+
}
81+
82+
/**
83+
* @dataProvider provideIteratorTypeNames
84+
*/
85+
public function testIteratorTypeByName(string $name) : void
86+
{
87+
$method = str_replace('_', '', $name).'iterator';
88+
89+
self::assertSame(constant(IteratorTypes::class.'::'.$name), [Criteria::class, $method]()->getIteratorType());
90+
}
91+
92+
/**
93+
* @dataProvider provideIteratorTypeNames
94+
*/
95+
public function testAndIteratorTypeByName(string $name) : void
96+
{
97+
// make sure we don't assign the same iterator twice
98+
$method = 'EQ' === $name ? 'allIterator' : 'eqIterator';
99+
$andMethod = 'and'.str_replace('_', '', $name).'iterator';
100+
101+
self::assertSame(constant(IteratorTypes::class.'::'.$name), [Criteria::class, $method]()->$andMethod()->getIteratorType());
102+
}
103+
104+
public function provideIteratorTypeNames() : iterable
105+
{
106+
return [
107+
['EQ'],
108+
['REQ'],
109+
['ALL'],
110+
['LT'],
111+
['LE'],
112+
['GE'],
113+
['GT'],
114+
['BITS_ALL_SET'],
115+
['BITS_ANY_SET'],
116+
['BITS_ALL_NOT_SET'],
117+
['OVERLAPS'],
118+
['NEIGHBOR'],
119+
];
120+
}
121+
}

0 commit comments

Comments
 (0)