Skip to content

Commit 63dcaa3

Browse files
committed
#28 Re code review
- notInArray - notBool - notInt - notFloat - notArray - notResource - notString
1 parent 0c9331c commit 63dcaa3

File tree

6 files changed

+51
-466
lines changed

6 files changed

+51
-466
lines changed

README.md

+24-36
Original file line numberDiff line numberDiff line change
@@ -100,62 +100,44 @@ v::assert([], 'var')->notEmpty();
100100

101101
#### isArray `Check if value is array`
102102

103-
* Antipode: **notArray**
104-
105103
```php
106104
// OK
107105
v::assert([], 'var')->isArray();
108-
v::assert('5', 'var')->notArray();
109106

110107
// EXCEPTION
111108
v::assert('5', 'var')->isArray();
112-
v::assert([], 'var')->notArray();
113109
```
114110

115111
#### bool `Check if value is bool`
116112

117-
* Antipode: **notBool**
118-
119113
```php
120114
// OK
121115
v::assert(false, 'var')->bool();
122-
v::assert('5', 'var')->notBool();
123116

124117
// EXCEPTION
125118
v::assert('5', 'var')->bool();
126-
v::assert(true, 'var')->notBool();
127119
```
128120

129121
#### float `Check if value is float`
130122

131-
* Antipode: **notFloat**
132-
133123
```php
134124
// OK
135125
v::assert(15.2, 'var')->float();
136-
v::assert('15.2', 'var')->notFloat();
137-
v::assert([], 'var')->notFloat();
138126

139127
// EXCEPTION
140128
v::assert('15.2', 'var')->float();
141129
v::assert([], 'var')->float();
142-
v::assert(15.2, 'var')->notFloat();
143130
```
144131

145132
#### int `Check if value is int`
146133

147-
* Antipode: **notInt**
148-
149134
```php
150135
// OK
151136
v::assert(15, 'var')->int();
152-
v::assert(15.2, 'var')->notInt();
153-
v::assert([], 'var')->notInt();
154137

155138
// EXCEPTION
156139
v::assert(15.2, 'var')->int();
157140
v::assert([], 'var')->int();
158-
v::assert(5, 'var')->notInt();
159141
```
160142

161143
#### numeric `Check if value is numeric`
@@ -184,30 +166,22 @@ v::assert(null, 'var')->notNull();
184166

185167
#### string `Check if value is string`
186168

187-
* Antipode: **notString**
188-
189169
```php
190170
// OK
191171
v::assert('5', 'var')->string();
192-
v::assert([], 'var')->notString();
193172

194173
// EXCEPTION
195174
v::assert([], 'var')->string();
196-
v::assert('-5', 'var')->notString();
197175
```
198176

199177
#### resource `Check if value is resource`
200178

201-
* Antipode: **notResource**
202-
203179
```php
204180
// OK
205181
v::assert(tmpfile(), 'var')->resource();
206-
v::assert(5, 'var')->notResource();
207182

208183
// EXCEPTION
209184
v::assert(5, 'var')->resource();
210-
v::assert(tmpfile(), 'var')->notResource();
211185
```
212186

213187
-- --
@@ -232,6 +206,8 @@ v::assert(['a'], 'var')->inArray(['a', 'b']);
232206
// EXCEPTION
233207
v::assert(['c'], 'var')->inArray(['a', 'b']);
234208

209+
// ----------
210+
235211
// EXCEPTION: var MUST be array
236212
v::assert('a', 'var')->inArray(['a', 'b']);
237213

@@ -258,6 +234,8 @@ v::assert('5', 'var')->digit();
258234
// EXCEPTION
259235
v::assert('c', 'var')->digit();
260236

237+
// ----------
238+
261239
// EXCEPTION: var MUST be string
262240
v::assert(5, 'var')->digit();
263241
```
@@ -275,6 +253,8 @@ v::assert('a', 'var')->match('/a/');
275253
// EXCEPTION
276254
v::assert('b', 'var')->match('/a/');
277255

256+
// ----------
257+
278258
// EXCEPTION: pattern MUST be not empty
279259
v::assert('a', 'var')->match('');
280260

@@ -294,6 +274,8 @@ v::assert('aa', 'var')->glob('a*');
294274
// EXCEPTION
295275
v::assert('bb', 'var')->glob('a*');
296276

277+
// ----------
278+
297279
// EXCEPTION: pattern MUST be not empty
298280
v::assert('a', 'var')->glob('');
299281

@@ -317,6 +299,8 @@ v::assert('aa', 'var')->length(2);
317299
// EXCEPTION
318300
v::assert('bb', 'var')->length(5);
319301

302+
// ----------
303+
320304
// EXCEPTION: length MUST be int
321305
v::assert('a', 'var')->length(null);
322306

@@ -397,10 +381,7 @@ v::assert(5, 'var')->lengthBetween(1);
397381
Number validators (int or float)
398382
--------------------------------
399383

400-
All number validators run previously:
401-
402-
* **numeric**
403-
* **notString**
384+
**All number MUST be int or float**
404385

405386
#### positive `Check if value is positive (not 0)`
406387

@@ -417,6 +398,8 @@ v::assert(10, 'var')->positive();
417398
v::assert(0, 'var')->negative();
418399
v::assert(0, 'var')->negative();
419400

401+
// ----------
402+
420403
// EXCEPTION: var MUST be int or float
421404
v::assert('A', 'var')->positive();
422405
v::assert([], 'var')->negative();
@@ -441,6 +424,8 @@ v::assert(10, 'var')->more(5);
441424
v::assert(10, 'var')->less(5);
442425
v::assert(1, 'var')->more(2);
443426

427+
// ----------
428+
444429
// EXCEPTION: length MUST be int or float
445430
v::assert(1, 'var')->less(null);
446431
v::assert(1, 'var')->more(null);
@@ -503,7 +488,7 @@ v::assert('a', 'var')->toBool()->get();
503488

504489
#### toFloat `Converts any type (except array) to float`
505490

506-
Run previously: **notArray**
491+
**Value MUST NOT be array**
507492

508493
```php
509494
// RETURN 0.0
@@ -512,13 +497,15 @@ v::assert('a', 'var')->toFloat()->get();
512497
// RETURN -15.2
513498
v::assert('-15.2', 'var')->toFloat()->get();
514499

500+
// ----------
501+
515502
// EXCEPTION: var MUST NOT be array
516503
v::assert([], 'var')->toFloat()->get();
517504
```
518505

519506
#### toInt `Converts any type (except array) to int`
520507

521-
Run previously: **notArray**
508+
**Value MUST NOT be array**
522509

523510
```php
524511
// RETURN 0
@@ -527,13 +514,15 @@ v::assert('a', 'var')->toInt()->get();
527514
// RETURN -15
528515
v::assert('-15.2', 'var')->toInt()->get();
529516

517+
// ----------
518+
530519
// EXCEPTION: var MUST NOT be array
531520
v::assert([], 'var')->toInt()->get();
532521
```
533522

534523
#### toString `Converts any type (except array) to string`
535524

536-
Run previously: **notArray**
525+
**Value MUST NOT be array**
537526

538527
```php
539528
// RETURN ''
@@ -542,6 +531,8 @@ v::assert(false, 'var')->toString()->get();
542531
// RETURN '-15'
543532
v::assert(-15, 'var')->toString()->get();
544533

534+
// ----------
535+
545536
// EXCEPTION: var MUST NOT be array
546537
v::assert([], 'var')->toString()->get();
547538
```
@@ -559,6 +550,3 @@ make test
559550
```sh
560551
make benchmark
561552
```
562-
563-
[yii1-request]: https://github.com/yiisoft/yii/blob/master/framework/web/CHttpRequest.php
564-
[symfony-request]: https://github.com/symfony/HttpFoundation/blob/master/Request.php

src/Variable.php

+9-93
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,6 @@ public function isArray()
292292
return $this;
293293
}
294294

295-
/**
296-
* Check if value is not an array
297-
*
298-
* @return Variable
299-
* @throws \Exception
300-
*/
301-
public function notArray()
302-
{
303-
if (is_array($this->value)) {
304-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'array']);
305-
}
306-
307-
return $this;
308-
}
309-
310295
/**
311296
* Soft check that $from <= value <= $to
312297
*
@@ -396,21 +381,6 @@ public function bool()
396381
return $this;
397382
}
398383

399-
/**
400-
* Check if value is not boolean (is_bool)
401-
*
402-
* @return Variable
403-
* @throws \InvalidArgumentException
404-
*/
405-
public function notBool()
406-
{
407-
if (is_bool($this->value)) {
408-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'bool']);
409-
}
410-
411-
return $this;
412-
}
413-
414384
/**
415385
* Check if value is digit (ctype_digit)
416386
*
@@ -475,21 +445,6 @@ public function float()
475445
return $this;
476446
}
477447

478-
/**
479-
* Check if value is not float (is_float)
480-
*
481-
* @return Variable
482-
* @throws \InvalidArgumentException
483-
*/
484-
public function notFloat()
485-
{
486-
if (is_float($this->value)) {
487-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'float']);
488-
}
489-
490-
return $this;
491-
}
492-
493448
/**
494449
* Check if value is integer (is_int)
495450
*
@@ -505,21 +460,6 @@ public function int()
505460
return $this;
506461
}
507462

508-
/**
509-
* Check if value is not integer (is_int)
510-
*
511-
* @return Variable
512-
* @throws \InvalidArgumentException
513-
*/
514-
public function notInt()
515-
{
516-
if (is_int($this->value)) {
517-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'int']);
518-
}
519-
520-
return $this;
521-
}
522-
523463
/**
524464
* Soft check that value <= $max
525465
*
@@ -776,21 +716,6 @@ public function resource()
776716
return $this;
777717
}
778718

779-
/**
780-
* Check if value is not resource (is_resource)
781-
*
782-
* @return Variable
783-
* @throws \InvalidArgumentException
784-
*/
785-
public function notResource()
786-
{
787-
if (is_resource($this->value)) {
788-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'resource']);
789-
}
790-
791-
return $this;
792-
}
793-
794719
/**
795720
* Check if value is string (is_string)
796721
*
@@ -806,21 +731,6 @@ public function string()
806731
return $this;
807732
}
808733

809-
/**
810-
* Check if value is not string (is_string)
811-
*
812-
* @return Variable
813-
* @throws \InvalidArgumentException
814-
*/
815-
public function notString()
816-
{
817-
if (is_string($this->value)) {
818-
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'string']);
819-
}
820-
821-
return $this;
822-
}
823-
824734
/**
825735
* Cast value to bool
826736
*
@@ -841,7 +751,9 @@ public function toBool()
841751
*/
842752
public function toFloat()
843753
{
844-
$this->notArray();
754+
if (is_array($this->value)) {
755+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'array']);
756+
}
845757

846758
$this->value = (float) $this->value;
847759

@@ -856,7 +768,9 @@ public function toFloat()
856768
*/
857769
public function toInt()
858770
{
859-
$this->notArray();
771+
if (is_array($this->value)) {
772+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'array']);
773+
}
860774

861775
$this->value = (int) $this->value;
862776

@@ -871,7 +785,9 @@ public function toInt()
871785
*/
872786
public function toString()
873787
{
874-
$this->notArray();
788+
if (is_array($this->value)) {
789+
throw $this->buildException(self::EXCEPTION_TYPE_TEXT_NEGATIVE, ['{{type}}' => 'array']);
790+
}
875791

876792
$this->value = (string) $this->value;
877793

0 commit comments

Comments
 (0)