KeyUtil::validate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the webmozart/key-value-store package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore\Util;
13
14
use Webmozart\KeyValueStore\Api\InvalidKeyException;
15
16
/**
17
 * Utility methods for dealing with key-value store keys.
18
 *
19
 * @since  1.0
20
 *
21
 * @author Bernhard Schussek <[email protected]>
22
 */
23
final class KeyUtil
24
{
25
    /**
26
     * Validates that a key is valid.
27
     *
28
     * @param mixed $key The tested key.
29
     *
30
     * @throws InvalidKeyException If the key is invalid.
31
     */
32 856
    public static function validate($key)
33
    {
34 856
        if (!is_string($key) && !is_int($key)) {
35 114
            throw InvalidKeyException::forKey($key);
36
        }
37 746
    }
38
39
    /**
40
     * Validates that multiple keys are valid.
41
     *
42
     * @param array $keys The tested keys.
43
     *
44
     * @throws InvalidKeyException If a key is invalid.
45
     */
46 369
    public static function validateMultiple($keys)
47
    {
48 369
        foreach ($keys as $key) {
49 369
            if (!is_string($key) && !is_int($key)) {
50 369
                throw InvalidKeyException::forKey($key);
51
            }
52
        }
53 329
    }
54
55
    private function __construct()
56
    {
57
    }
58
}
59