Completed
Push — develop ( 2e317e...f1c66e )
by Alexandru
12:35
created

Gender::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 4
nop 1
1
<?php
2
/**
3
 * Copyright 2017 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 Vimishor\Cnp;
9
10
use Gentle\Embeddable\Embeddable;
11
12
/**
13
 * @author Alexandru Guzinschi <[email protected]>
14
 */
15
final class Gender extends Embeddable
16
{
17
    /**
18
     * @param string|int $gender Odd for male, even for female.
19
     *
20
     * @throws \InvalidArgumentException
21
     */
22
    public function __construct($gender)
23
    {
24
        $gender = is_string($gender) ? (int)$gender : $gender;
25
26
        if (!is_int($gender) || !in_array($gender, range(1, 8), false)) {
27
            throw new \InvalidArgumentException('Unknown gender');
28
        }
29
30
        $this->value = (string)$gender;
31
    }
32
33
    /**
34
     * @access public
35
     * @return bool
36
     */
37
    public function isMale()
38
    {
39
        return (int)$this->value % 2 !== 0;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function equals(Embeddable $object)
46
    {
47
        return get_class($object) === 'Vimishor\Cnp\Gender' && $this->value === (string)$object;
48
    }
49
}
50