Completed
Branch master (3b97d6)
by Pavel
09:49
created

RelationColumn::getHeadContent()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Columns;
5
6
7
use Pfilsx\DataGrid\DataGridException;
8
9
class RelationColumn extends DataColumn
10
{
11
    protected $labelAttribute;
12
13
    protected function checkConfiguration()
14
    {
15
        parent::checkConfiguration();
16
        if (!is_string($this->labelAttribute) || empty($this->labelAttribute)) {
17
            throw new DataGridException('labelAttribute property must be set for RelationColumn');
18
        }
19
    }
20
21
    public function getHeadContent()
22
    {
23
        return !empty($this->label)
24
            ? ucfirst($this->label)
25
            : (!empty($this->attribute)
26
                ? ucfirst($this->attribute) . '.' . ucfirst($this->labelAttribute)
27
                : ucfirst($this->labelAttribute)
28
            );
29
    }
30
31
    public function getCellContent($entity)
32
    {
33
        $obj = $this->getCellValue($entity);
34
        if (is_object($obj)) {
35
            $result = $obj->{'get' . ucfirst($this->labelAttribute)}();
36
            return $this->format === 'html'
37
                ? $result
38
                : htmlspecialchars($result);
39
        }
40
        return is_string($obj) ? $obj : '';
41
    }
42
43
    /**
44
     * @param string $labelAttribute
45
     */
46
    protected function setLabelAttribute(string $labelAttribute): void
47
    {
48
        $this->labelAttribute = $labelAttribute;
49
    }
50
}
51