Year::equals()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 Year extends Embeddable
16
{
17
    /**
18
     * @param string|int $year
19
     *
20
     * @throws \InvalidArgumentException
21
     * @throws \LengthException If received value is not a full numeric representation of a year.
22
     */
23 53
    public function __construct($year)
24
    {
25 53
        if (is_int($year)) {
26 17
            $year = (string)$year;
27
        }
28
29 53
        if (!is_string($year) || !ctype_digit($year)) {
30 9
            throw new \InvalidArgumentException(sprintf('Expected string or integer and got %s', gettype($year)));
31
        }
32
33 44
        if (4 !== mb_strlen($year)) {
34 6
            throw new \LengthException('Expected a full numeric representation of a year.');
35
        }
36
37 38
        $this->value = $year;
38 38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 6
    public function equals(Embeddable $object)
44
    {
45 6
        return get_class($object) === 'Gentle\Embeddable\Date\Year' && $this->value === (string)$object;
46
    }
47
}
48