Completed
Push — master ( d7f537...efd057 )
by Adam
02:00
created

Cell   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 100
rs 10
c 1
b 0
f 0
wmc 15
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getColumn() 0 4 1
A getData() 0 4 1
A setData() 0 4 1
A setValue() 0 4 1
A getValue() 0 4 2
A getUnescapedValue() 0 4 1
A setupValue() 0 8 4
A decorate() 0 9 3
1
<?php
2
3
namespace Boduch\Grid;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
7
class Cell implements CellInterface
8
{
9
    use AttributesTrait;
10
11
    /**
12
     * @var Column
13
     */
14
    protected $column;
15
16
    /**
17
     * @var mixed
18
     */
19
    protected $data;
20
21
    /**
22
     * @var mixed
23
     */
24
    protected $value;
25
26
    /**
27
     * @param Column $column
28
     * @param mixed $data       Raw row data (array or object)
29
     */
30
    public function __construct(Column $column, $data)
31
    {
32
        $this->attributes = new ParameterBag();
33
        $this->column = $column;
34
        $this->data = $data;
35
36
        $this->setupValue();
37
        $this->decorate();
38
    }
39
40
    /**
41
     * @return Column
42
     */
43
    public function getColumn()
44
    {
45
        return $this->column;
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51
    public function getData()
52
    {
53
        return $this->data;
54
    }
55
56
    /**
57
     * @param mixed $data
58
     */
59
    public function setData($data)
60
    {
61
        $this->data = $data;
62
    }
63
64
    /**
65
     * @param mixed $value
66
     */
67
    public function setValue($value)
68
    {
69
        $this->value = $value;
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getValue()
76
    {
77
        return $this->column->isAutoescape() ? htmlspecialchars($this->value) : $this->value;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getUnescapedValue()
84
    {
85
        return $this->value;
86
    }
87
88
    protected function setupValue()
89
    {
90
        if (is_array($this->data) || $this->data instanceof \ArrayAccess) {
91
            $this->value = array_get($this->data, $this->column->getName());
92
        } elseif (is_object($this->data)) {
93
            $this->value = object_get($this->data, $this->column->getName());
94
        }
95
    }
96
97
    protected function decorate()
98
    {
99
        foreach ($this->column->getDecorators() as $decorator) {
100
            // if decorator returns FALSE, we need to break the loop. next decorators WILL NOT be executed.
101
            if (false === $decorator->decorate($this)) {
102
                break;
103
            }
104
        }
105
    }
106
}
107