1 | <?php |
||
18 | class SimpleGrid extends AbstractGrid implements ColumHidebleProviderInterface |
||
19 | { |
||
20 | /** |
||
21 | * Коллекция колонок таблицы скрытых/показаннах пользователем |
||
22 | * @var array | Traversable |
||
23 | */ |
||
24 | protected $userHiddenColums; |
||
25 | |||
26 | /** |
||
27 | * Флаг указывающий на изменеи списка колонок используется для проверки необходимости переустановки скрытия колонок пользователем |
||
|
|||
28 | * @var bool |
||
29 | */ |
||
30 | protected $columsChanged = false; |
||
31 | /** |
||
32 | * Данные в гриде |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $rowSet = []; |
||
36 | |||
37 | /** |
||
38 | * Возвращает массив строк таблицы |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getRowSet() |
||
50 | |||
51 | /** |
||
52 | * Добавление колонок в таблицу |
||
53 | * @param array | Traversable $columns |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function setColumns($columns) |
||
57 | { |
||
58 | $this->setColumsChanged(true); |
||
59 | return parent::setColumns($columns); |
||
60 | } |
||
61 | /** |
||
62 | * Добавление колонки в таблицу |
||
63 | * @param ColumnInterface|array|ArrayAccess $column |
||
64 | * @return $this |
||
65 | * @throws Column\Exception\InvalidColumnException |
||
66 | * @throws Exception\InvalidArgumentException |
||
67 | * @throws Exception\RuntimeException |
||
68 | */ |
||
69 | public function add($column) |
||
70 | { |
||
71 | $this->setColumsChanged(true); |
||
72 | return parent::add($column); |
||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Создает из данных адаптера rowSet |
||
78 | * @param array | ArrayAccess $data |
||
79 | * @return $this |
||
80 | * @throws Exception\RuntimeException |
||
81 | */ |
||
82 | protected function buildRowSet($data) |
||
83 | { |
||
84 | if (!is_array($data) && $data instanceof Traversable) { |
||
85 | throw new Exception\RuntimeException( |
||
86 | sprintf('Данные должны быть массивом или %s', ArrayAccess::class) |
||
87 | ); |
||
88 | } |
||
89 | $columns = $this->getColumns(); |
||
90 | |||
91 | foreach ($data as $item) { |
||
92 | $mutators = $this->getMutators(); |
||
93 | $item = $this->mutate($mutators, $item); |
||
94 | /** @var ColumnInterface $column */ |
||
95 | foreach ($columns as $column) { |
||
96 | $columnName = $column->getName(); |
||
97 | $mutators = $column->getMutators(); |
||
98 | if (array_key_exists($columnName, $item)) { |
||
99 | $item = $this->mutate($mutators, $item, $columnName); |
||
100 | } |
||
101 | } |
||
102 | $this->rowSet[] = new Row($item); |
||
103 | } |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Метод непосредственно осуществляет мутацию данных |
||
109 | * @param array $mutators |
||
110 | * @param array | Row $row |
||
111 | * @param string $name |
||
112 | * @return mixed |
||
113 | */ |
||
114 | protected function mutate(array $mutators, $row, $name = null) |
||
115 | { |
||
116 | /** @var MutatorInterface $mutator */ |
||
117 | foreach ($mutators as $mutator) { |
||
118 | if ($mutator instanceof MutatorInterface) { |
||
119 | $mutator->setRowData($row); |
||
120 | if ($mutator->validate()) { |
||
121 | if ($name) { |
||
122 | $row[$name] = $mutator->change($row[$name]); |
||
123 | } else { |
||
124 | $row = $mutator->change($row); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | return $row; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param array $rowSet |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function setRowSet($rowSet) |
||
141 | |||
142 | /** |
||
143 | * Функция инициализации колонок |
||
144 | * @return void |
||
145 | */ |
||
146 | public function init() |
||
149 | /** |
||
150 | * Возвращает коллекцию колонок |
||
151 | * @return array | Traversable |
||
152 | */ |
||
153 | public function getColumns() |
||
182 | |||
183 | |||
184 | /** |
||
185 | * получить коллекцию колонок таблицы скрытых/показаннах пользователем |
||
186 | * @return array|Traversable |
||
187 | */ |
||
188 | public function getUserHiddenColums() |
||
192 | |||
193 | /** |
||
194 | * установить коллекцию колонок таблицы скрытых/показаннах пользователем |
||
195 | * @param array|Traversable $userHiddenColums |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setUserHiddenColums($userHiddenColums) |
||
204 | |||
205 | /** |
||
206 | * @return boolean |
||
207 | */ |
||
208 | public function isColumsChanged() |
||
212 | |||
213 | /** |
||
214 | * @param boolean $columsChanged |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setColumsChanged($columsChanged) |
||
222 | } |
||
223 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.