TimeFormatTrait::setTimeFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * (c) Mantas Varatiejus <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace MVar\Apache2LogParser;
11
12
/**
13
 * This trait defines logic needed to convert time to different format.
14
 */
15
trait TimeFormatTrait
16
{
17
    /**
18
     * @var bool|string Custom time format
19
     */
20
    private $timeFormat;
21
22
    /**
23
     * Sets time format.
24
     *
25
     * Set format string supported by PHP's date() function or set TRUE to get
26
     * time as \DateTime object.
27
     *
28
     * @param bool|string $timeFormat
29
     */
30 2
    public function setTimeFormat($timeFormat)
31
    {
32 2
        $this->timeFormat = $timeFormat;
33 2
    }
34
35
    /**
36
     * Converts time to previously set format.
37
     *
38
     * @param string $time
39
     *
40
     * @return \DateTime|string
41
     */
42 6
    public function formatTime($time)
43
    {
44 6
        if ($this->timeFormat === null || $this->timeFormat === false) {
45 4
            return $time;
46
        }
47
48 2
        $dateTime = new \DateTime($time);
49
50 2
        if ($this->timeFormat === true) {
51 1
            return $dateTime;
52
        }
53
54 1
        return $dateTime->format($this->timeFormat);
55
    }
56
}
57