1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ZfTable ( Module for Zend Framework 2) |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2013 Piotr Duda [email protected] |
6
|
|
|
* @license MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ZfTable; |
10
|
|
|
|
11
|
|
|
use ZfTable\AbstractElement; |
12
|
|
|
use ZfTable\Decorator\DecoratorFactory; |
13
|
|
|
|
14
|
|
|
class Cell extends AbstractElement |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Header object |
19
|
|
|
* @var Header |
20
|
|
|
*/ |
21
|
|
|
protected $header; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @param Header $header |
26
|
|
|
*/ |
27
|
|
|
public function __construct($header) |
28
|
|
|
{ |
29
|
|
|
$this->header = $header; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @param string $name type |
35
|
|
|
* @param array $options type |
36
|
|
|
* @return Decorator\AbstractDecorator |
37
|
|
|
*/ |
38
|
|
|
public function addDecorator($name, $options = array()) |
39
|
|
|
{ |
40
|
|
|
$decorator = DecoratorFactory::factoryCell($name, $options); |
41
|
|
|
$decorator->setCell($this); |
42
|
|
|
$this->attachDecorator($decorator); |
43
|
|
|
return $decorator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get header object |
48
|
|
|
* @return Header |
49
|
|
|
*/ |
50
|
|
|
public function getHeader() |
51
|
|
|
{ |
52
|
|
|
return $this->header; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set header object |
57
|
|
|
* |
58
|
|
|
* @param Header $header |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setHeader($header) |
62
|
|
|
{ |
63
|
|
|
$this->header = $header; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get actual row |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getActualRow() |
73
|
|
|
{ |
74
|
|
|
return $this->getTable()->getRow()->getActualRow(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Rendering single cell |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function render($type = 'html') |
83
|
|
|
{ |
84
|
|
|
$row = $this->getTable()->getRow()->getActualRow(); |
85
|
|
|
|
86
|
|
|
$value = ''; |
87
|
|
|
|
88
|
|
|
if (is_array($row) || $row instanceof \ArrayAccess) { |
89
|
|
|
$value = (isset($row[$this->getHeader()->getName()])) ? $row[$this->getHeader()->getName()] : ''; |
90
|
|
|
} elseif (is_object($row)) { |
91
|
|
|
$headerName = $this->getHeader()->getName(); |
92
|
|
|
$methodName = 'get' . ucfirst($headerName); |
93
|
|
|
if (method_exists($row, $methodName)) { |
94
|
|
|
$value = $row->$methodName(); |
95
|
|
|
} else { |
96
|
|
|
$value = (property_exists($row, $headerName)) ? $row->$headerName : ''; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($this->decorators as $decorator) { |
101
|
|
|
if ($decorator->validConditions()) { |
102
|
|
|
$value = $decorator->render($value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($type == 'html') { |
107
|
|
|
$ret = sprintf("<td %s>%s</td>", $this->getAttributes(), $value); |
108
|
|
|
$this->clearVar(); |
109
|
|
|
return $ret; |
110
|
|
|
|
111
|
|
|
} else { |
112
|
|
|
return $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|