Completed
Push — master ( f96952...73a571 )
by Pavel
03:59 queued 11s
created

DataColumn::getEmptyValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Columns;
5
6
7
use Pfilsx\DataGrid\DataGridException;
8
9
class DataColumn extends AbstractColumn
10
{
11
    protected $attribute;
12
    protected $format = 'raw';
13
    protected $sort = true;
14
    protected $emptyValue = '';
15
16
    /**
17
     * @param mixed $attribute
18
     */
19
    protected function setAttribute($attribute): void
20
    {
21
        $this->attribute = $attribute;
22
    }
23
24
    /**
25
     * @return null|string
26
     */
27
    public function getAttribute()
28
    {
29
        return $this->attribute;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getLabel(): string
36
    {
37
        return $this->label === false
0 ignored issues
show
introduced by
The condition $this->label === false is always false.
Loading history...
38
            ? ''
39
            : (!empty($this->label) ? $this->label : $this->attribute);
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getFormat(): string
46
    {
47
        return $this->format;
48
    }
49
50
    /**
51
     * @param string $format
52
     */
53
    protected function setFormat(string $format): void
54
    {
55
        $this->format = strtolower($format);
56
    }
57
58
    public function getEmptyValue()
59
    {
60
        return $this->emptyValue;
61
    }
62
63
    protected function setEmptyValue($value): void
64
    {
65
        $this->emptyValue = $value;
66
    }
67
68
    protected function checkConfiguration()
69
    {
70
        if ((!is_string($this->attribute) || empty($this->attribute)) && $this->value === null) {
71
            throw new DataGridException('attribute or value property must be set for ' . static::class);
72
        }
73
    }
74
75
    public function hasFilter()
76
    {
77
        return parent::hasFilter() && !empty($this->attribute);
78
    }
79
80
    public function getHeadContent()
81
    {
82
        $label = $this->getLabel();
83
        if (($translator = $this->container->getTranslator()) !== null) {
84
            $label = $translator->trans($label, [], $this->translationDomain);
85
        }
86
        return ucfirst($label);
87
    }
88
89
    public function getFilterContent()
90
    {
91
        if ($this->hasFilter()) {
92
            return $this->filter->render($this->attribute, $this->filterValue);
93
        }
94
        return '';
95
    }
96
97
    public function getCellContent($entity)
98
    {
99
        $result = (string)$this->getCellValue($entity);
100
        return empty($result)
101
            ? $this->emptyValue
102
            : ($this->format === 'html' ? $result : htmlspecialchars($result));
103
    }
104
105
    protected function getCellValue($entity)
106
    {
107
        if (is_callable($this->value)) {
108
            return call_user_func_array($this->value, [$entity]);
109
        } elseif ($this->value !== null) {
110
            return $this->value;
111
        } else {
112
            return $entity->{$this->attribute};
113
        }
114
    }
115
}
116