|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenConext\Value\Assert; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion as BaseAssertion; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @method static void nullOrNonEmptyString($value, $message = null, $propertyPath = null) |
|
9
|
|
|
* @method static void allNonEmptyString($value, $message = null, $propertyPath = null) |
|
10
|
|
|
* @method static void allValidRegularExpression($value, $message = null, $propertyPath = null) |
|
11
|
|
|
* @method static void allKeysExist($value, $data, $message = null, $propertyPath = null) |
|
12
|
|
|
*/ |
|
13
|
|
|
class Assertion extends BaseAssertion |
|
14
|
|
|
{ |
|
15
|
|
|
const INVALID_NON_EMPTY_STRING = 501; |
|
16
|
|
|
const INVALID_REGULAR_EXPRESSION = 502; |
|
17
|
|
|
const INVALID_CALLABLE = 503; |
|
18
|
|
|
|
|
19
|
|
|
protected static $exceptionClass = 'OpenConext\Value\Exception\InvalidArgumentException'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $value |
|
23
|
|
|
* @param string $propertyPath |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function nonEmptyString($value, $propertyPath) |
|
27
|
|
|
{ |
|
28
|
|
|
if (!is_string($value) || trim($value) === '') { |
|
29
|
|
|
$message = 'Expected non-empty string for "%s", "%s" given'; |
|
30
|
|
|
|
|
31
|
|
|
throw static::createException( |
|
32
|
|
|
$value, |
|
33
|
|
|
sprintf($message, $propertyPath, static::stringify($value)), |
|
34
|
|
|
static::INVALID_NON_EMPTY_STRING, |
|
35
|
|
|
$propertyPath |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $regularExpression |
|
42
|
|
|
* @param $propertyPath |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function validRegularExpression($regularExpression, $propertyPath) |
|
46
|
|
|
{ |
|
47
|
|
|
$pregMatchErrored = false; |
|
48
|
|
|
set_error_handler( |
|
49
|
|
|
function () use (&$pregMatchErrored) { |
|
50
|
|
|
$pregMatchErrored = true; |
|
51
|
|
|
} |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
preg_match($regularExpression, 'some test string'); |
|
55
|
|
|
|
|
56
|
|
|
restore_error_handler(); |
|
57
|
|
|
|
|
58
|
|
|
if ($pregMatchErrored || preg_last_error()) { |
|
59
|
|
|
throw static::createException( |
|
60
|
|
|
$regularExpression, |
|
61
|
|
|
sprintf('The pattern "%s" is not a valid regular expression', self::stringify($regularExpression)), |
|
62
|
|
|
static::INVALID_REGULAR_EXPRESSION, |
|
63
|
|
|
$propertyPath |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $requiredKeys |
|
70
|
|
|
* @param array $value |
|
71
|
|
|
* @param null|string $message |
|
72
|
|
|
* @param null|string $propertyPath |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function keysExist(array $value, array $requiredKeys, $message = null, $propertyPath = null) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($requiredKeys as $requiredKey) { |
|
78
|
|
|
self::keyExists($value, $requiredKey, $message, $propertyPath); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|