Completed
Pull Request — master (#148)
by
unknown
04:22
created

TableCell::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Nayjest\Grids\Components;
4
5
use Nayjest\Grids\FieldConfig;
6
7
/**
8
 * Class TableCell
9
 * 
10
 * The component for rendering TD html tag inside grid.
11
 *
12
 * @package Nayjest\Grids\Components
13
 */
14
class TableCell extends HtmlTag
15
{
16
    protected $tag_name = 'td';
17
18
    /** @var  FieldConfig */
19
    protected $column;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param FieldConfig $column
25
     */
26
    public function __construct(FieldConfig $column) {
27
28
        $this->setColumn($column);
29
    }
30
31
    public function getAttributes()
32
    {
33
        if (empty($this->attributes['class'])) {
34
            $this->attributes['class'] = 'column-' . $this->getColumn()->getName();
35
        }
36
        if ($this->getColumn()->getCellHtmlAttributes()) {
37
            foreach ($this->getColumn()->getCellHtmlAttributes() as $attribute=>$value) {
38
                $this->attributes[$attribute] .= (empty($this->attributes[$attribute]) ? '' : ' ') . $value;
39
            }
40
        }
41
        if (empty($this->attributes['data-label'])) {
42
            $this->attributes['data-label'] = $this->getColumn()->getLabel();
43
        }
44
        if ($this->column->isHidden()) {
45
            $this->attributes['style'] = 'display:none;';
46
        }
47
        return $this->attributes;
48
    }
49
50
    /**
51
     * Returns component name.
52
     * By default it's column_{$column_name}
53
     *
54
     * @return string|null
55
     */
56
    public function getName()
57
    {
58
        return $this->name ? : 'column_' . $this->column->getName();
59
    }
60
61
    /**
62
     * Returns associated column.
63
     *
64
     * @return FieldConfig $column
65
     */
66
    public function getColumn()
67
    {
68
        return $this->column;
69
    }
70
71
    /**
72
     * @param FieldConfig $column
73
     * @return $this
74
     */
75
    public function setColumn(FieldConfig $column)
76
    {
77
        $this->column = $column;
78
        return $this;
79
    }
80
}
81
82