Month::fromString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace BestServedCold\PhalueObjects\DateTime\Unit;
4
5
use BestServedCold\PhalueObjects\Contract\DateTime as DateTimeInterface;
6
use BestServedCold\PhalueObjects\DateTime\DateTimeTrait;
7
use BestServedCold\PhalueObjects\Mathematical\Integer;
8
9
/**
10
 * Class Month
11
 *
12
 * @package   BestServedCold\PhalueObjects\DateTime\Unit
13
 * @author    Adam Lewis <[email protected]>
14
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
15
 * @license   http://http://opensource.org/licenses/GPL-3.0 GPL License
16
 * @link      http://bestservedcold.com
17
 * @since     0.0.1-alpha
18
 * @version   0.0.2-alpha
19
 */
20
final class Month extends Integer implements DateTimeInterface
21
{
22
    use DateTimeTrait;
23
24
    /**
25
     * @param integer $value
26
     */
27 29
    public function __construct($value)
28
    {
29 29
        parent::__construct($value);
30 29
    }
31
32
    /**
33
     * @return static
34
     */
35 13
    public static function now()
36
    {
37 13
        return new static(self::getNowDateTimeFormat('n'));
38
    }
39
40
    /**
41
     * @return string
42
     */
43 24
    public function __toString()
44
    {
45 24
        return str_pad($this->getValue(), 2, '0', STR_PAD_LEFT);
46
    }
47
48
    /**
49
     * From String.
50
     *
51
     * @param  $string
52
     * @return static
53
     */
54 1
    public static function fromString($string)
55
    {
56 1
        return ctype_digit($string)
57 1
            ? new static((int) $string)
58 1
            : self::fromNative(new \DateTime($string));
59
    }
60
61
    /**
62
     * From Native
63
     *
64
     * @param \DateTime $native
65
     * @return DateTimeInterface
66
     */
67 2
    public static function fromNative(\DateTime $native)
68
    {
69 2
        return new static((int) $native->format('n'));
70
    }
71
72
    /**
73
     * @return int
74
     */
75 1
    public function getMaximum()
76
    {
77 1
        return 12;
78
    }
79
80
    /**
81
     * @return int
82
     */
83 1
    public function getMinimum()
84
    {
85 1
        return 1;
86
    }
87
88
}
89