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

ColumnDateTime::getColumnValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 9.2
cc 3
eloc 9
nc 4
nop 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