Completed
Pull Request — develop (#9)
by Daan van
24:12 queued 15:23
created

Assertion::nonEmptyString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
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
18
    protected static $exceptionClass = 'OpenConext\Value\Exception\InvalidArgumentException';
19
20
    /**
21
     * @param string $value
22
     * @param string $propertyPath
23
     * @return void
24
     */
25
    public static function nonEmptyString($value, $propertyPath)
26
    {
27
        if (!is_string($value) || trim($value) === '') {
28
            $message = 'Expected non-empty string for "%s", "%s" given';
29
30
            throw static::createException(
31
                $value,
32
                sprintf($message, $propertyPath, static::stringify($value)),
33
                static::INVALID_NON_EMPTY_STRING,
34
                $propertyPath
35
            );
36
        }
37
    }
38
39
    /**
40
     * @param $regularExpression
41
     * @param $propertyPath
42
     * @return void
43
     */
44
    public static function validRegularExpression($regularExpression, $propertyPath)
45
    {
46
        $pregMatchErrored = false;
47
        set_error_handler(
48
            function () use (&$pregMatchErrored) {
49
                $pregMatchErrored = true;
50
            }
51
        );
52
53
        preg_match($regularExpression, 'some test string');
54
55
        restore_error_handler();
56
57
        if ($pregMatchErrored || preg_last_error()) {
58
            throw static::createException(
59
                $regularExpression,
60
                sprintf('The pattern "%s" is not a valid regular expression', self::stringify($regularExpression)),
61
                static::INVALID_REGULAR_EXPRESSION,
62
                $propertyPath
63
            );
64
        }
65
    }
66
67
    /**
68
     * @param array       $requiredKeys
69
     * @param array       $value
70
     * @param null|string $message
71
     * @param null|string $propertyPath
72
     * @return void
73
     */
74
    public static function keysExist(array $value, array $requiredKeys, $message = null, $propertyPath = null)
75
    {
76
        foreach ($requiredKeys as $requiredKey) {
77
            self::keyExists($value, $requiredKey, $message, $propertyPath);
78
        }
79
    }
80
}
81