DateTime   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
c 0
b 0
f 0
dl 0
loc 25
rs 10
ccs 14
cts 14
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getValue() 0 11 3
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