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

Cell::getUnescapedValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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