Completed
Push — feature/more-metadata-values ( 04e0cc...df50c8 )
by Daan van
05:57 queued 03:45
created

Assertion::isCallable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
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 12
rs 9.4285
cc 3
eloc 8
nc 4
nop 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
    /**
83
     * @param mixed       $value
84
     * @param null|string $message
85
     * @param string      $propertyPath
86
     */
87
    public static function isCallable($value, $propertyPath, $message = null)
88
    {
89
        $message = $message ?: 'Expected a callable for "%s", got a "%s"';
90
        if (!is_callable($value)) {
91
            throw static::createException(
92
                $value,
93
                sprintf($message, $propertyPath, static::stringify($value)),
94
                static::INVALID_CALLABLE,
95
                $propertyPath
96
            );
97
        }
98
    }
99
}
100