DateTime::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 1
nop 4
crap 2
1
<?php
2
3
namespace kalanis\kw_table\core\Table\Columns;
4
5
6
use DateTime as core;
7
use kalanis\kw_connect\core\Interfaces\IRow;
8
9
10
/**
11
 * Class DateTime
12
 * @package kalanis\kw_table\core\Table\Columns
13
 * Format date by datetime class
14
 */
15
class DateTime extends AColumn
16
{
17
    protected string $format = '';
18
    protected bool $timestamp = false;
19
    protected core $dateTime;
20
21 3
    public function __construct(string $sourceName, string $format = 'Y-m-d', bool $timestamp = false, ?core $dateTime = null)
22
    {
23 3
        $this->sourceName = $sourceName;
24 3
        $this->format = $format;
25 3
        $this->timestamp = $timestamp;
26 3
        $this->dateTime = $dateTime ?: new core();
27 3
    }
28
29 3
    public function getValue(IRow $source)
30
    {
31 3
        $result = parent::getValue($source);
32 3
        $isEmpty = empty($result);
33 3
        if ($isEmpty) {
34 1
            return 0;
35
        } else {
36 2
            $result = $this->timestamp ? $result : strtotime(strval($result));
37 2
            $this->dateTime->setTimestamp(intval($result));
38
39 2
            return $this->dateTime->format($this->format);
40
        }
41
    }
42
}
43