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

RelationColumn   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeadContent() 0 7 3
A getCellContent() 0 10 4
A setLabelAttribute() 0 3 1
A checkConfiguration() 0 5 3
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