ValidNameChecker::ensureValidName()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 8
nc 2
nop 1
crap 4
1
<?php
2
3
4
namespace Beanie\Tube;
5
6
7
use Beanie\Exception\InvalidNameException;
8
9
class ValidNameChecker
10
{
11
    const VALID_NAME_REGEX = '/^[A-Za-z0-9+\/;.$_()][A-Za-z0-9+\/;.$_()\-]*$/';
12
13
    /**
14
     * @param string $name
15
     * @return bool
16
     * @throws InvalidNameException
17
     */
18 29
    public function ensureValidName($name)
19
    {
20
        if (!(
21 29
            is_string($name) &&
22 29
            strlen($name) <= 200 &&
23 25
            preg_match(self::VALID_NAME_REGEX, $name)
24 29
        )) {
25 8
            throw new InvalidNameException(sprintf(
26 8
                'Invalid name: \'%s\'', $this->asString($name)
27 8
            ));
28
        }
29
30 21
        return true;
31
    }
32
33
    /**
34
     * @param mixed $value
35
     * @return string
36
     */
37 8
    private function asString($value)
38
    {
39 8
        return (is_object($value) && !method_exists($value, '__toString'))
40 8
            ? sprintf('{object of type %s', get_class($value))
41 8
            : $value;
42
    }
43
}
44