Completed
Push — master ( f15fe2...9a8393 )
by Pavel
07:23 queued 11s
created

RelationColumn   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 47
rs 10
c 2
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCellContent() 0 10 4
A getLabel() 0 3 2
A getLabelAttribute() 0 3 1
A setLabelAttribute() 0 3 1
A checkConfiguration() 0 5 3
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Columns;
5
6
7
8
class RelationColumn extends DataColumn
9
{
10
    protected $labelAttribute;
11
12
    protected function checkConfiguration()
13
    {
14
        parent::checkConfiguration();
15
        if (!is_string($this->labelAttribute) || empty($this->labelAttribute)) {
16
            $this->labelAttribute = 'id';
17
        }
18
    }
19
20
    /**
21
     * @return string
22
     */
23
    public function getLabel(): string
24
    {
25
        return !empty($this->label) ? $this->label : "{$this->attribute}.{$this->labelAttribute}";
26
    }
27
28
29
    public function getCellContent($entity)
30
    {
31
        $obj = $this->getCellValue($entity);
32
        if (is_object($obj)) {
33
            $result = $obj->{'get' . ucfirst($this->labelAttribute)}();
34
            return $this->format === 'html'
35
                ? $result
36
                : htmlspecialchars($result);
37
        }
38
        return is_string($obj) ? $obj : '';
39
    }
40
41
    /**
42
     * @return mixed
43
     */
44
    public function getLabelAttribute()
45
    {
46
        return $this->labelAttribute;
47
    }
48
49
    /**
50
     * @param string $labelAttribute
51
     */
52
    protected function setLabelAttribute(string $labelAttribute): void
53
    {
54
        $this->labelAttribute = $labelAttribute;
55
    }
56
}
57