WeekDay::fromNativeDateTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\Enum\Enum;
6
7
class WeekDay extends Enum
8
{
9
    const MONDAY    = 'Monday';
10
    const TUESDAY   = 'Tuesday';
11
    const WEDNESDAY = 'Wednesday';
12
    const THURSDAY  = 'Thursday';
13
    const FRIDAY    = 'Friday';
14
    const SATURDAY  = 'Saturday';
15
    const SUNDAY    = 'Sunday';
16
17
    /**
18
     * Returns the current week day.
19
     *
20
     * @return WeekDay
21
     */
22 1
    public static function now()
23
    {
24 1
        $now = new \DateTime('now');
25
26 1
        return static::fromNativeDateTime($now);
27
    }
28
29
    /**
30
     * Returns a WeekDay from a PHP native \DateTime
31
     *
32
     * @param  \DateTime $date
33
     * @return WeekDay
34
     */
35 2
    public static function fromNativeDateTime(\DateTime $date)
36
    {
37 2
        $weekDay = \strtoupper($date->format('l'));
38
39 2
        return static::getByName($weekDay);
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\Enum::getByName() has been deprecated.

This method has been deprecated.

Loading history...
40
    }
41
42
    /**
43
     * Returns a numeric representation of the WeekDay.
44
     * 1 for Monday to 7 for Sunday.
45
     *
46
     * @return int
47
     */
48 1
    public function getNumericValue()
49
    {
50 1
        return $this->getOrdinal() + 1;
51
    }
52
}
53