Completed
Branch master (9b1797)
by Mantas
11:40
created

TimeFormatTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setTimeFormat() 0 4 1
A formatTime() 0 14 4
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
    public function setTimeFormat($timeFormat)
31
    {
32
        $this->timeFormat = $timeFormat;
33
    }
34
35
    /**
36
     * Converts time to previously set format.
37
     *
38
     * @param string $time
39
     *
40
     * @return \DateTime|string
41
     */
42
    public function formatTime($time)
43
    {
44
        if ($this->timeFormat === null || $this->timeFormat === false) {
45
            return $time;
46
        }
47
48
        $dateTime = new \DateTime($time);
49
50
        if ($this->timeFormat === true) {
51
            return $dateTime;
52
        }
53
54
        return $dateTime->format($this->timeFormat);
55
    }
56
}
57