-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidator.php
65 lines (58 loc) · 1.7 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Validator.php
*
* @category Class
* @package Shipper
* @author Yusuf Ardi <[email protected]>
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0-only License
*/
namespace Shipper;
use InvalidArgumentException;
/**
* Class Validator
*
* @category Class
* @package Shipper
* @author Yusuf Ardi <[email protected]>
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0-only License
*/
class Validator
{
/**
* Check if required parameters are exist.
*
* @param array $parameters
* @param array $requiredParameters
* @return self
* @throws \InvalidArgumentException
*/
public static function requireValidation(array $parameters, array $requiredParameters) : self
{
foreach ($requiredParameters as $requiredParameter) {
if (! isset($parameters[$requiredParameter])) {
throw new InvalidArgumentException("'{$requiredParameter}' is required.");
}
}
return new self;
}
/**
* Check if parameters have correct variable type.
*
* @param array $parameters
* @param array $typeParameters
* @return void
* @throws \InvalidArgumentException
*/
public static function typeValidation(array $parameters, array $typeParameters) : void
{
foreach ($typeParameters as $key => $typeParameter) {
if (isset($parameters[$key])) {
$acceptedTypes = explode('|', $typeParameter);
if (! in_array(gettype($parameters[$key]), $acceptedTypes)) {
throw new InvalidArgumentException("'{$key}' type must be {$typeParameter}.");
}
}
}
}
}