Completed
Push — master ( a6f36a...985feb )
by Pavel
02:40
created

ColumnDateTime   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnValue() 0 22 3
A setFormat() 0 6 1
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Column;
10
11
use DateTime;
12
use Ublaboo\DataGrid\Row;
13
use Ublaboo\DataGrid\Utils\DateTimeHelper;
14
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
15
16
class ColumnDateTime extends Column
17
{
18
	/**
19
	 * @var string
20
	 */
21
	protected $align = 'right';
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $format = 'j. n. Y';
27
28
29
	/**
30
	 * Format row item value as DateTime
31
	 * @param  Row $row
32
	 * @return string
33
	 */
34
	public function getColumnValue(Row $row)
35
	{
36
		$value = parent::getColumnValue($row);
37
38
		if (!($value instanceof DateTime)) {
39
			/**
40
			 * Try to convert string to DateTime object
41
			 */
42
			try {
43
				$date = DateTimeHelper::tryConvertToDateTime($value);
44
45
				return $date->format($this->format);
46
			} catch (DataGridDateTimeHelperException $e) {
47
				/**
48
				 * Otherwise just return raw string
49
				 */
50
				return $value;
51
			}
52
		}
53
54
		return $value->format($this->format);
55
	}
56
57
58
	/**
59
	 * Set DateTime format
60
	 * @param string $format
61
	 * @return static
62
	 */
63
	public function setFormat($format)
64
	{
65
		$this->format = $format;
66
67
		return $this;
68
	}
69
70
}
71