Passed
Push — master ( 9e5fe0...88d9d6 )
by Tom
04:35
created

Prefix::filter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Value;
6
7
use Ktomk\Pipelines\Utility\App;
8
use UnexpectedValueException;
9
10
/**
11
 * Pipelines Prefix
12
 *
13
 * @package Ktomk\Pipelines\Value
14
 */
15
abstract class Prefix
16
{
17
    const DEFAULT_PREFIX = App::UTILITY_NAME;
18
19
    /**
20
     * @param string $prefix
21
     *
22
     * @return string
23
     */
24 2
    public static function verify($prefix)
25
    {
26 2
        if (preg_match('~^[a-z]{3,}$~', $prefix)) {
27 2
            return $prefix;
28
        }
29
30 1
        throw new UnexpectedValueException(sprintf(
31 1
            'invalid prefix: "%s"; a prefix is only lower-case letters with a minimum length of three characters',
32
            $prefix
33
        ));
34
    }
35
36
    /**
37
     * filter a prefix
38
     *
39
     * always return a verified prefix (string)
40
     *
41
     * @param null|string $prefix [optional] if null, defaults to the default prefix
42
     *
43
     * @return string verified prefix
44
     */
45 1
    public static function filter($prefix = null)
46
    {
47 1
        $buffer = $prefix;
48
49 1
        if (null === $buffer) {
50 1
            $buffer = self::DEFAULT_PREFIX;
51
        }
52
53 1
        return self::verify($buffer);
54
    }
55
}
56