GetSetDateTrait::setDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JhFlexiTime\Controller;
4
5
use Zend\Validator\Date as DateValidator;
6
use JhFlexiTime\DateTime\DateTime;
7
8
/**
9
 * Class GetSetDateTrait
10
 * @author Aydin Hassan <[email protected]>
11
 */
12
trait GetSetDateTrait
13
{
14
    /**
15
     * @var DateTime
16
     */
17
    protected $date;
18
19
    /**
20
     * @param string $month
21
     * @param string $year
22
     * @return DateTime
23
     */
24
    public function getDate($month = null, $year = null)
25
    {
26
27
        if (!$this->date) {
28
            $validator  = new DateValidator(['format' => 'M Y']);
29
            if ($validator->isValid(sprintf("%s %s", $month, $year))) {
30
                $period = new DateTime(sprintf('last day of %s %s 23:59:59', $month, $year));
31
            } else {
32
                $period = new DateTime;
33
            }
34
            $this->date = $period;
35
        }
36
37
        return $this->date;
38
    }
39
40
    /**
41
     * @param DateTime $date
42
     */
43
    public function setDate(DateTime $date)
44
    {
45
        $this->date = $date;
46
    }
47
}
48