1 | <?php |
||
13 | class Column { |
||
14 | |||
15 | /** @var string */ |
||
16 | private $_key; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $_name; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $_mutated = false; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $_table; |
||
26 | |||
27 | /** @var Relation */ |
||
28 | private $_relation; |
||
29 | |||
30 | /** @var ColumnFormatter */ |
||
31 | private $_formatter; |
||
32 | 49 | ||
33 | /** |
||
34 | 49 | * Column constructor. |
|
35 | 49 | * @param string $name |
|
36 | 49 | * @param ColumnFormatter|null $columnFormatter |
|
37 | * @param Model|null $sourceModel |
||
38 | */ |
||
39 | public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null) |
||
40 | { |
||
41 | 49 | $this->_setName($name); |
|
42 | $this->_formatter = $columnFormatter; |
||
43 | 49 | if ($sourceModel !== null) |
|
44 | 49 | { |
|
45 | $this->_table = $sourceModel->getTable(); |
||
46 | 47 | $this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true); |
|
47 | } |
||
48 | 47 | } |
|
49 | |||
50 | /** |
||
51 | 5 | * @param string $name |
|
52 | */ |
||
53 | 5 | private function _setName(string $name) : void |
|
54 | 5 | { |
|
55 | 5 | $posDivider = \mb_strpos($name, '.'); |
|
56 | 5 | if ($posDivider === false) |
|
57 | { |
||
58 | $this->_name = $this->_key = $name; |
||
59 | |||
60 | return; |
||
61 | 34 | } |
|
62 | |||
63 | 34 | $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1)); |
|
64 | |||
65 | $this->_relation = new Relation($name); |
||
66 | $aggregate = $this->_relation->aggregate; |
||
67 | $this->_key = ($aggregate !== 'first' ? ($aggregate . '_') : '') . $this->_relation->name; |
||
68 | } |
||
69 | 5 | ||
70 | /** |
||
71 | 5 | * @return string |
|
72 | */ |
||
73 | public function getName() : string |
||
74 | { |
||
75 | return $this->_name; |
||
76 | } |
||
77 | 4 | ||
78 | /** |
||
79 | 4 | * @return bool |
|
80 | */ |
||
81 | public function isMutated() : bool |
||
82 | { |
||
83 | return $this->_mutated; |
||
84 | } |
||
85 | 3 | ||
86 | /** |
||
87 | 3 | * @return null|Relation |
|
88 | */ |
||
89 | public function getRelation() : ? Relation |
||
90 | { |
||
91 | return $this->_relation; |
||
92 | } |
||
93 | |||
94 | 2 | /** |
|
95 | * @param ColumnFormatter $columnFormatter |
||
96 | 2 | * @return Column |
|
97 | */ |
||
98 | 2 | public function setFormatter(ColumnFormatter $columnFormatter) : Column |
|
99 | { |
||
100 | $this->_formatter = $columnFormatter; |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get the formatted column value. |
||
107 | 34 | * |
|
108 | * @param Model $rowModel |
||
109 | 34 | * @return string|null |
|
110 | */ |
||
111 | 34 | public function getFormattedValue(Model $rowModel) : ? string |
|
112 | { |
||
113 | $value = $this->getValue($rowModel); |
||
114 | |||
115 | return $this->_formatter !== null ? $this->_formatter->format($value) : $value; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the value of this column for the given row. |
||
120 | 34 | * |
|
121 | * @param Model $rowModel |
||
122 | 34 | * @return string|null |
|
123 | */ |
||
124 | 3 | public function getValue(Model $rowModel) : ? string |
|
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getKey() : string |
||
133 | { |
||
134 | return $this->_key; |
||
135 | } |
||
136 | 3 | ||
137 | /** |
||
138 | 3 | * @return string |
|
139 | 3 | */ |
|
140 | public function getIdentifier() : string |
||
144 | 2 | ||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getAttributeName() : string |
||
162 | } |
||
163 |