Assertion   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A keysExist() 0 6 2
A nonEmptyString() 0 13 3
A validRegularExpression() 0 22 3
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