Skip to content

Commit 69a8ae5

Browse files
committed
[#2] add hasKey assert
+ hasKey assert + count assert + isSame assert + notSame assert + ArgumentCheckerTrait
1 parent a5b91c2 commit 69a8ae5

File tree

64 files changed

+2148
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2148
-50
lines changed

README.md

+98-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ php-assert
1515

1616
## Yet another validator, WHY??
1717

18-
## It's very fast, but ugly inside
18+
## It's very fast and designed to be used in any method
1919

2020
There are many other cool asserts, but for their usability you must pay by time & memory of execution.
2121
This assert gives you very simple and fast API.
@@ -202,10 +202,52 @@ Assert::assert('c', 'var')->inArray(['a', 'b']);
202202
Assert::assert(['a'], 'var')->inArray('a');
203203
```
204204

205+
#### isSame($anotherValue) `Check if value is same as $anotherValue`
206+
207+
* Antipode: **notSame**
208+
209+
Arguments:
210+
211+
* `$anotherValue` MUST be not object
212+
213+
```php
214+
// OK
215+
Assert::assert('a', 'var')->isSame('a');
216+
217+
// EXCEPTION
218+
Assert::assert('a', 'var')->isSame('b');
219+
220+
// ----------
221+
222+
// EXCEPTION: $anotherValue MUST be not object
223+
Assert::assert('a', 'var')->isSame(new \stdClass());
224+
```
225+
226+
#### notSame($anotherValue) `Check if value is not same as $anotherValue`
227+
228+
* Antipode: **isSame**
229+
230+
Arguments:
231+
232+
* `$anotherValue` MUST be not object
233+
234+
```php
235+
// OK
236+
Assert::assert('a', 'var')->notSame('b');
237+
238+
// EXCEPTION
239+
Assert::assert('a', 'var')->notSame('a');
240+
241+
// ----------
242+
243+
// EXCEPTION: $anotherValue MUST be not object
244+
Assert::assert('a', 'var')->notSame(new \stdClass());
245+
```
246+
205247
-- --
206248

207249
String asserts
208-
-----------------
250+
--------------
209251

210252
All string asserts run previously:
211253

@@ -456,6 +498,60 @@ Assert::assert('A', 'var')->between(1);
456498

457499
-- --
458500

501+
Array asserts
502+
--------------
503+
504+
#### hasKey($key) `Check if array key exists`
505+
506+
Arguments:
507+
508+
* `$key` MUST be string or int
509+
510+
```php
511+
// OK
512+
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey('a');
513+
514+
// EXCEPTION
515+
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey('e');
516+
517+
// ----------
518+
519+
// EXCEPTION: data MUST be an array
520+
Assert::assert('notArray', 'data')->hasKey(1);
521+
522+
// EXCEPTION: key MUST be string or int
523+
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey(null);
524+
525+
```
526+
527+
#### count($count) `Check if array elements count is same as $count`
528+
529+
Arguments:
530+
531+
* `$count` MUST be int and greater than 0
532+
533+
```php
534+
// OK
535+
Assert::assert(['a', 'b', 'c'], 'data')->count(3);
536+
537+
// EXCEPTION
538+
Assert::assert(['a', 'c', 'd'], 'data')->count(1);
539+
540+
// ----------
541+
542+
// EXCEPTION: data MUST be an array
543+
Assert::assert('notArray', 'data')->hasKey(1);
544+
545+
// EXCEPTION: count MUST be int
546+
Assert::assert(['a', 'c', 'd'], 'data')->count(null);
547+
548+
// EXCEPTION: count MUST be greater than 0
549+
Assert::assert(['a', 'c', 'd'], 'data')->count(-5);
550+
551+
```
552+
553+
-- --
554+
459555
LOOP ASSERTS API
460556
================
461557

src/Assert.php

+103-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace KoKoKo\assert;
99

10+
use KoKoKo\assert\exceptions\ArrayKeyNotExistsException;
11+
use KoKoKo\assert\exceptions\InvalidArrayCountException;
1012
use KoKoKo\assert\exceptions\InvalidArrayException;
1113
use KoKoKo\assert\exceptions\InvalidBoolException;
1214
use KoKoKo\assert\exceptions\InvalidDigitException;
@@ -15,14 +17,17 @@
1517
use KoKoKo\assert\exceptions\InvalidIntException;
1618
use KoKoKo\assert\exceptions\InvalidIntOrFloatException;
1719
use KoKoKo\assert\exceptions\InvalidIntOrFloatOrStringException;
20+
use KoKoKo\assert\exceptions\InvalidIntOrStringException;
1821
use KoKoKo\assert\exceptions\InvalidNotArrayException;
1922
use KoKoKo\assert\exceptions\InvalidNotEmptyException;
2023
use KoKoKo\assert\exceptions\InvalidNotNullException;
2124
use KoKoKo\assert\exceptions\InvalidNotObjectException;
25+
use KoKoKo\assert\exceptions\InvalidNotSameValueException;
2226
use KoKoKo\assert\exceptions\InvalidNullException;
2327
use KoKoKo\assert\exceptions\InvalidNumericException;
24-
use KoKoKo\assert\exceptions\InvalidRegexpPatternException;
28+
use KoKoKo\assert\exceptions\InvalidRegExpPatternException;
2529
use KoKoKo\assert\exceptions\InvalidResourceException;
30+
use KoKoKo\assert\exceptions\InvalidSameValueException;
2631
use KoKoKo\assert\exceptions\InvalidStringException;
2732
use KoKoKo\assert\exceptions\InvalidStringLengthException;
2833
use KoKoKo\assert\exceptions\LengthNotBetweenException;
@@ -305,6 +310,61 @@ public function isArray()
305310
return $this;
306311
}
307312

313+
/**
314+
* Check if array key exists
315+
*
316+
* @param string|int $key
317+
* @return $this
318+
* @throws ArrayKeyNotExistsException
319+
* @throws InvalidArrayException
320+
* @throws InvalidIntOrStringException
321+
*/
322+
public function hasKey($key)
323+
{
324+
if (!is_string($key) && !is_int($key)) {
325+
throw new InvalidIntOrStringException('key', $key);
326+
}
327+
328+
if (!is_array($this->value)) {
329+
throw new InvalidArrayException($this->name, $this->value);
330+
}
331+
332+
if (!array_key_exists($key, $this->value)) {
333+
throw new ArrayKeyNotExistsException($this->name, $key);
334+
}
335+
336+
return $this;
337+
}
338+
339+
/**
340+
* Check if array elements count is same as $count
341+
*
342+
* @param int $count
343+
* @return $this
344+
* @throws InvalidArrayCountException
345+
* @throws InvalidArrayException
346+
* @throws InvalidIntException
347+
* @throws NumberNotGreaterException
348+
*/
349+
public function count($count)
350+
{
351+
if (!is_int($count)) {
352+
throw new InvalidIntException('count', $count);
353+
} elseif ($count < 0) {
354+
throw new NumberNotGreaterException('count', $count, 0);
355+
}
356+
357+
if (!is_array($this->value)) {
358+
throw new InvalidArrayException($this->name, $this->value);
359+
}
360+
361+
if (count($this->value) !== $count) {
362+
throw new InvalidArrayCountException($this->name, $this->value, $count);
363+
}
364+
365+
return $this;
366+
}
367+
308368
/**
309369
* Soft check that $from <= value <= $to
310370
*
@@ -646,6 +706,48 @@ public function positive()
646706
return $this;
647707
}
648708

709+
/**
710+
* Check if value is same as $anotherValue
711+
*
712+
* @param int|float|bool|string|resource|array|null $anotherValue
713+
* @return $this
714+
* @throws InvalidNotObjectException
715+
* @throws InvalidSameValueException
716+
*/
717+
public function isSame($anotherValue)
718+
{
719+
if (is_object($anotherValue)) {
720+
throw new InvalidNotObjectException('anotherValue');
721+
}
722+
723+
if ($this->value !== $anotherValue) {
724+
throw new InvalidSameValueException($this->name, $this->value, $anotherValue);
725+
}
726+
727+
return $this;
728+
}
729+
730+
/**
731+
* Check if value is not same as $anotherValue
732+
*
733+
* @param int|float|bool|string|resource|array|null $anotherValue
734+
* @return $this
735+
* @throws InvalidNotObjectException
736+
* @throws InvalidNotSameValueException
737+
*/
738+
public function notSame($anotherValue)
739+
{
740+
if (is_object($anotherValue)) {
741+
throw new InvalidNotObjectException('anotherValue');
742+
}
743+
744+
if ($this->value === $anotherValue) {
745+
throw new InvalidNotSameValueException($this->name, $this->value);
746+
}
747+
748+
return $this;
749+
}
750+
649751
/**
650752
* Check if value is null
651753
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @link https://github.com/ko-ko-ko/php-assert
4+
* @copyright Copyright (c) 2015 Roman Levishchenko <[email protected]>
5+
* @license https://raw.github.com/ko-ko-ko/php-assert/master/LICENSE
6+
*/
7+
8+
namespace KoKoKo\assert\exceptions;
9+
10+
class ArrayKeyNotExistsException extends ArgumentException
11+
{
12+
/**
13+
* @param string $variableName
14+
* @param string $key
15+
* @throws InvalidIntOrStringException
16+
* @throws InvalidNotEmptyException
17+
* @throws InvalidStringException
18+
*/
19+
public function __construct($variableName, $key)
20+
{
21+
if (!is_string($variableName)) {
22+
throw new InvalidStringException('variableName', $variableName);
23+
} elseif (!is_int($key) && !is_string($key)) {
24+
throw new InvalidIntOrStringException('key', $key);
25+
}
26+
27+
parent::__construct(
28+
sprintf(
29+
'Variable "$%s" must contain key: "%s"',
30+
$variableName,
31+
$key
32+
)
33+
);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @link https://github.com/ko-ko-ko/php-assert
4+
* @copyright Copyright (c) 2015 Roman Levishchenko <[email protected]>
5+
* @license https://raw.github.com/ko-ko-ko/php-assert/master/LICENSE
6+
*/
7+
8+
namespace KoKoKo\assert\exceptions;
9+
10+
class InvalidArrayCountException extends ArgumentException
11+
{
12+
/**
13+
* @param string $variableName
14+
* @param array $variableValue
15+
* @param int $count
16+
* @throws InvalidArrayException
17+
* @throws InvalidIntException
18+
* @throws InvalidNotEmptyException
19+
* @throws InvalidStringException
20+
* @throws NumberNotGreaterException
21+
*/
22+
public function __construct($variableName, $variableValue, $count)
23+
{
24+
if (!is_string($variableName)) {
25+
throw new InvalidStringException('variableName', $variableName);
26+
} elseif (!is_array($variableValue)) {
27+
throw new InvalidArrayException('variableValue', $variableValue);
28+
} elseif (!is_int($count)) {
29+
throw new InvalidIntException('count', $count);
30+
} elseif ($count < 0) {
31+
throw new NumberNotGreaterException('count', $count, 0);
32+
}
33+
34+
parent::__construct(
35+
sprintf(
36+
'Variable "$%s" must contain: "%d" elements, actual count: "%d"',
37+
$variableName,
38+
$count,
39+
count($variableValue)
40+
)
41+
);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @link https://github.com/ko-ko-ko/php-assert
4+
* @copyright Copyright (c) 2015 Roman Levishchenko <[email protected]>
5+
* @license https://raw.github.com/ko-ko-ko/php-assert/master/LICENSE
6+
*/
7+
8+
namespace KoKoKo\assert\exceptions;
9+
10+
class InvalidIntOrStringException extends ArgumentException
11+
{
12+
/**
13+
* @param string $variableName
14+
* @param int|float|bool|resource|array $variableValue
15+
* @throws InvalidNotIntAndNotStringException
16+
* @throws InvalidNotEmptyException
17+
* @throws InvalidStringException
18+
*/
19+
public function __construct($variableName, $variableValue)
20+
{
21+
if (!is_string($variableName)) {
22+
throw new InvalidStringException('variableName', $variableName);
23+
} elseif (is_int($variableValue) || is_string($variableValue)) {
24+
throw new InvalidNotIntAndNotStringException($variableName);
25+
}
26+
27+
parent::__construct(
28+
sprintf(
29+
'Variable "$%s" must be: "int" or "string", actual type: "%s"',
30+
$variableName,
31+
gettype($variableValue)
32+
)
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)