Skip to content

Commit 0c9331c

Browse files
committed
#28 Re code review
- notInArray
1 parent d893094 commit 0c9331c

File tree

6 files changed

+39
-82
lines changed

6 files changed

+39
-82
lines changed

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,22 @@ All array validators run previously:
221221

222222
#### inArray($range) `Check if value is in array $range`
223223

224-
* Antipode: **notInArray**
225-
226224
Arguments:
227225

228226
* `$range` MUST be array
229227

230228
```php
231229
// OK
232230
v::assert(['a'], 'var')->inArray(['a', 'b']);
233-
v::assert(5, 'var')->notInArray();
234231

235232
// EXCEPTION
236233
v::assert(['c'], 'var')->inArray(['a', 'b']);
237-
v::assert(['a'], 'var')->notInArray(['a', 'b']);
238234

239235
// EXCEPTION: var MUST be array
240236
v::assert('a', 'var')->inArray(['a', 'b']);
241-
v::assert('a', 'var')->notInArray(['a', 'b']);
242237

243238
// EXCEPTION: $range MUST be array
244239
v::assert(['a'], 'var')->inArray('a');
245-
v::assert(['a'], 'var')->notInArray('a');
246240
```
247241

248242
-- --

src/Variable.php

+39-30
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ public function length($length)
148148
throw new \InvalidArgumentException('Param $length must be more than 0');
149149
}
150150

151-
$this->string();
151+
if (!is_string($this->value)) {
152+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'string']);
153+
}
152154

153155
if (mb_strlen($this->value) !== $length) {
154156
throw $this->buildException(self::EXCEPTION_LENGTH_TEXT_POSITIVE, ['{{value}}' => $length]);
@@ -184,7 +186,9 @@ public function lengthBetween($from, $to)
184186
throw new \InvalidArgumentException('Param $from must be more than 0');
185187
}
186188

187-
$this->string();
189+
if (!is_string($this->value)) {
190+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'string']);
191+
}
188192

189193
$length = mb_strlen($this->value);
190194

@@ -216,7 +220,9 @@ public function lengthLess($length)
216220
throw new \InvalidArgumentException('Param $length must be more than 0');
217221
}
218222

219-
$this->string();
223+
if (!is_string($this->value)) {
224+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'string']);
225+
}
220226

221227
if (mb_strlen($this->value) > $length) {
222228
throw $this->buildException(self::EXCEPTION_LENGTH_TEXT_POSITIVE, ['{{value}}' => 'more than ' . $length]);
@@ -243,7 +249,9 @@ public function lengthMore($length)
243249
throw new \InvalidArgumentException('Param $length must be more than 0');
244250
}
245251

246-
$this->notEmpty()->string();
252+
if (!is_string($this->value)) {
253+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'string']);
254+
}
247255

248256
if (mb_strlen($this->value) < $length) {
249257
throw $this->buildException(self::EXCEPTION_LENGTH_TEXT_POSITIVE, ['{{value}}' => 'more than ' . $length]);
@@ -269,23 +277,6 @@ public function inArray(array $range)
269277
return $this;
270278
}
271279

272-
/**
273-
* Check if value is not in array (in_array strict)
274-
*
275-
* @param array $range
276-
*
277-
* @return Variable
278-
* @throws \InvalidArgumentException
279-
*/
280-
public function isNotInArray(array $range)
281-
{
282-
if (in_array($this->value, $range, true)) {
283-
throw $this->buildException(self::EXCEPTION_VALUE_IN_ARRAY_NEGATIVE, ['{{type}}' => 'array']);
284-
}
285-
286-
return $this;
287-
}
288-
289280
/**
290281
* Check if value is array
291282
*
@@ -339,7 +330,9 @@ public function between($from, $to)
339330
throw new \InvalidArgumentException('Param $from must be less than $to');
340331
}
341332

342-
$this->numeric()->notString();
333+
if (!is_int($this->value) && !is_float($this->value)) {
334+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
335+
}
343336

344337
if ($this->value < $from || $this->value > $to) {
345338
throw $this->buildException(
@@ -374,7 +367,9 @@ public function betweenStrict($from, $to)
374367
throw new \InvalidArgumentException('Param $from must be less than $to');
375368
}
376369

377-
$this->numeric()->notString();
370+
if (!is_int($this->value) && !is_float($this->value)) {
371+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
372+
}
378373

379374
if ($this->value <= $from || $this->value >= $to) {
380375
throw $this->buildException(
@@ -424,7 +419,9 @@ public function notBool()
424419
*/
425420
public function digit()
426421
{
427-
$this->string();
422+
if (!is_string($this->value)) {
423+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'string']);
424+
}
428425

429426
if (!ctype_digit($this->value)) {
430427
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'digit']);
@@ -538,7 +535,9 @@ public function less($number)
538535
throw new \InvalidArgumentException('Param $number must be int or float');
539536
}
540537

541-
$this->numeric()->notString();
538+
if (!is_int($this->value) && !is_float($this->value)) {
539+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
540+
}
542541

543542
if ($this->value > $number) {
544543
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'less than ' . $number]);
@@ -561,7 +560,9 @@ public function more($number)
561560
throw new \InvalidArgumentException('Param $number must be int or float');
562561
}
563562

564-
$this->numeric()->notString();
563+
if (!is_int($this->value) && !is_float($this->value)) {
564+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
565+
}
565566

566567
if ($this->value < $number) {
567568
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'more than ' . $number]);
@@ -584,7 +585,9 @@ public function lessStrict($number)
584585
throw new \InvalidArgumentException('Param $number must be int or float');
585586
}
586587

587-
$this->numeric()->notString();
588+
if (!is_int($this->value) && !is_float($this->value)) {
589+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
590+
}
588591

589592
if ($this->value >= $number) {
590593
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'less than ' . $number]);
@@ -607,7 +610,9 @@ public function moreStrict($number)
607610
throw new \InvalidArgumentException('Param $number must be int or float');
608611
}
609612

610-
$this->numeric()->notString();
613+
if (!is_int($this->value) && !is_float($this->value)) {
614+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
615+
}
611616

612617
if ($this->value <= $number) {
613618
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'more than ' . $number]);
@@ -681,7 +686,9 @@ public function glob($pattern)
681686
*/
682687
public function negative()
683688
{
684-
$this->numeric()->notString();
689+
if (!is_int($this->value) && !is_float($this->value)) {
690+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
691+
}
685692

686693
if ($this->value >= 0) {
687694
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'negative']);
@@ -698,7 +705,9 @@ public function negative()
698705
*/
699706
public function positive()
700707
{
701-
$this->numeric()->notString();
708+
if (!is_int($this->value) && !is_float($this->value)) {
709+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_POSITIVE, ['{{type}}' => 'int or float']);
710+
}
702711

703712
if ($this->value <= 0) {
704713
throw $this->buildException(self::EXCEPTION_VALUE_TEXT_POSITIVE, ['{{value}}' => 'positive']);

tests/_data/fixtures/variable.php

-2
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,6 @@
679679
'lengthMore' => 1,
680680
'lengthLess' => 1,
681681
'inArray' => 0,
682-
'isNotInArray' => 1
683682
]
684683
],
685684
// [1, 2] -> 5
@@ -692,7 +691,6 @@
692691
'lengthMore' => 1,
693692
'lengthLess' => 1,
694693
'inArray' => 1,
695-
'isNotInArray' => 0
696694
]
697695
],
698696
// 'abc' -> length(4)

tests/commands/RespectBenchmarkCommand.php

-15
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,6 @@ public function notFloat($var)
317317
$this->stop(__FUNCTION__, self::TYPE_RESPECT);
318318
}
319319

320-
/**
321-
* @param string $var
322-
* @param array $array
323-
*
324-
* @throws \InvalidArgumentException
325-
*/
326-
public function isNotInArray($var, $array)
327-
{
328-
$this->start();
329-
for ($i = 0; $i < self::COUNT_TEST; $i++) {
330-
respect::not(respect::in($array))->assert($var);
331-
}
332-
$this->stop(__FUNCTION__, self::TYPE_RESPECT);
333-
}
334-
335320
/**
336321
* @param string $var
337322
*

tests/commands/VariableBenchmarkCommand.php

-21
Original file line numberDiff line numberDiff line change
@@ -811,27 +811,6 @@ public function notFloat($var)
811811
$this->stop(__FUNCTION__, self::TYPE_NATIVE);
812812
}
813813

814-
/**
815-
* @param string $var
816-
* @param array $array
817-
*
818-
* @throws \InvalidArgumentException
819-
*/
820-
public function isNotInArray($var, $array)
821-
{
822-
$this->start();
823-
for ($i = 0; $i < self::COUNT_TEST; $i++) {
824-
if (!is_array($array)) {
825-
throw new \InvalidArgumentException('argument must be an array');
826-
}
827-
828-
if (in_array($var, $array)) {
829-
throw new \InvalidArgumentException('var must be in not array');
830-
}
831-
}
832-
$this->stop(__FUNCTION__, self::TYPE_NATIVE);
833-
}
834-
835814
/**
836815
* @param string $var
837816
*

tests/unit/VariableCest.php

-8
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,6 @@ public function notFloat(\UnitTester $I)
496496
$this->check($I, __FUNCTION__);
497497
}
498498

499-
/**
500-
* @param \UnitTester $I
501-
*/
502-
public function isNotInArray(\UnitTester $I)
503-
{
504-
$this->check($I, __FUNCTION__);
505-
}
506-
507499
/**
508500
* @param \UnitTester $I
509501
*/

0 commit comments

Comments
 (0)