Month::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 5
1
<?php
2
/**
3
 * Copyright 2016 Alexandru Guzinschi <[email protected]>.
4
 *
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
namespace Gentle\Embeddable\Date;
9
10
use Gentle\Embeddable\Embeddable;
11
12
/**
13
 * @author Alexandru Guzinschi <[email protected]>
14
 */
15
final class Month extends Embeddable
16
{
17
    /**
18
     * @param string|int $month
19
     *
20
     * @throws \InvalidArgumentException
21
     * @throws \OutOfRangeException
22
     */
23 49
    public function __construct($month)
24
    {
25 49
        if (is_int($month)) {
26 16
            $month = (string)$month;
27
        }
28
29 49
        if (!is_string($month) || !ctype_digit($month)) {
30 8
            throw new \InvalidArgumentException(sprintf('Expected string or integer and got %s', gettype($month)));
31
        }
32
33 41
        if (!in_array((int)$month, range(1, 12), true)) {
34 5
            throw new \OutOfRangeException('Expected a numeric representation of a month.');
35
        }
36
37 36
        $this->value = str_pad($month, 2, 0, STR_PAD_LEFT);
38 36
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 5
    public function equals(Embeddable $object)
44
    {
45 5
        return get_class($object) === 'Gentle\Embeddable\Date\Month' && $this->value === (string)$object;
46
    }
47
}
48