ValidNameChecker::asString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 4
nc 4
nop 1
crap 3
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