Passed
Push — master ( bbd9c6...a4fde9 )
by Andy
01:14
created

AbstractPeriod::getIntervalCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Palmtree\Chrono\Option;
4
5
abstract class AbstractPeriod
6
{
7
    abstract protected static function getIntervalCodes(): array;
8
9
    abstract protected static function getDateFormats(): array;
10
11 6
    public static function getIntervalCode(string $period): string
12
    {
13 6
        $codes = static::getIntervalCodes();
14
15 6
        if (!isset($codes[$period])) {
16 4
            $keys = implode("','", array_keys($codes));
17 4
            throw new \InvalidArgumentException("Period must be one of '$keys'. $period given");
18
        }
19
20 6
        return $codes[$period];
21
    }
22
23 16
    public static function getDateFormat(string $period): string
24
    {
25 16
        $formats = static::getDateFormats();
26
27 16
        if (!isset($formats[$period])) {
28 2
            $keys = implode("','", array_keys($formats));
29 2
            throw new \InvalidArgumentException("Period must be one of '$keys'. $period given");
30
        }
31
32 15
        return $formats[$period];
33
    }
34
}
35