Completed
Push — master ( 468379...ad9d92 )
by Petr
23:49 queued 21:04
created

Date::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Grido (http://grido.bugyik.cz)
5
 *
6
 * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE.md that was distributed with this source code.
10
 */
11
12
namespace Grido\Components\Columns;
13
14
/**
15
 * Date column.
16
 *
17
 * @package     Grido
18
 * @subpackage  Components\Columns
19
 * @author      Petr Bugyík
20
 *
21
 * @property string $dateFormat
22
 */
23
class Date extends Editable
24 1
{
25
    const FORMAT_TEXT = 'd M Y';
26
    const FORMAT_DATE = 'd.m.Y';
27
    const FORMAT_DATETIME = 'd.m.Y H:i:s';
28
29
    /** @var string */
30
    protected $dateFormat = self::FORMAT_DATE;
31
32
    /**
33
     * @param \Grido\Grid $grid
34
     * @param string $name
35
     * @param string $label
36
     * @param string $dateFormat
37
     */
38
    public function __construct($grid, $name, $label, $dateFormat = NULL)
39
    {
40 1
        parent::__construct($grid, $name, $label);
41
42 1
        if ($dateFormat !== NULL) {
43 1
            $this->dateFormat = $dateFormat;
44 1
        }
45 1
    }
46
47
    /**
48
     * @param string $format
49
     * @return Date
50
     */
51
    public function setDateFormat($format)
52
    {
53 1
        $this->dateFormat = $format;
54 1
        return $this;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getDateFormat()
61
    {
62 1
        return $this->dateFormat;
63
    }
64
65
    /**
66
     * @param mixed $value
67
     * @return mixed
68
     */
69
    protected function formatValue($value)
70
    {
71 1
        if ($value === NULL || is_bool($value)) {
72 1
            return $this->applyReplacement($value);
73 1
        } elseif (is_scalar($value)) {
74 1
            $value = \Latte\Runtime\Filters::escapeHtml($value);
75 1
            $replaced = $this->applyReplacement($value);
76 1
            if ($value !== $replaced && is_scalar($replaced)) {
77 1
                return $replaced;
78
            }
79 1
        }
80
81
        return $value instanceof \DateTime
82 1
            ? $value->format($this->dateFormat)
83 1
            : date($this->dateFormat, is_numeric($value) ? $value : strtotime($value)); //@todo notice for "01.01.1970"
84
    }
85
86
    /**
87
     * @param mixed $row
88
     * @return string
89
     * @internal
90
     */
91
    public function renderExport($row)
92
    {
93 1
        if (is_callable($this->customRenderExport)) {
94 1
            return call_user_func_array($this->customRenderExport, [$row]);
95
        }
96
97 1
        $value = $this->getValue($row);
98 1
        return $this->formatValue($value);
99
    }
100
}
101