EmployeeDataTablesProvider::renderColumn()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 38
c 0
b 0
f 0
rs 8.0555
cc 9
nc 9
nop 2
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Provider;
13
14
use DateTime;
15
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesColumnInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface;
17
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
18
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory;
19
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesCSVExporterInterface;
20
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesEditorInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
22
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Entity\Employee;
23
use WBW\Library\Types\Exception\IntegerArgumentException;
24
25
/**
26
 * Employee DataTables provider.
27
 *
28
 * @author webeweb <https://github.com/webeweb>
29
 * @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Entity
30
 */
31
class EmployeeDataTablesProvider implements DataTablesProviderInterface, DataTablesCSVExporterInterface, DataTablesEditorInterface {
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function editColumn(DataTablesColumnInterface $dtColumn, $entity, $value): void {
37
38
        switch ($dtColumn->getData()) {
39
40
            case "age":
41
                if (1 !== preg_match("/[0-9]{1,}/", $value)) {
42
                    throw new IntegerArgumentException($value);
43
                }
44
                $entity->setAge(intval($value));
45
                break;
46
47
            case "name":
48
                $entity->setName($value);
49
                break;
50
51
            case "office":
52
                $entity->setOffice($value);
53
                break;
54
55
            case "position":
56
                $entity->setPosition($value);
57
                break;
58
59
            case "salary":
60
                $entity->setSalary(intval($value));
61
                break;
62
63
            case "startDate":
64
                $entity->setStartDate(new DateTime($value));
65
                break;
66
        }
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function exportColumns(): array {
73
74
        return [
75
            "#",
76
            "Name",
77
            "Position",
78
            "Office",
79
            "Age",
80
            "Start date",
81
            "Salary",
82
        ];
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function exportRow($entity): array {
89
90
        return [
91
            $entity->getId(),
92
            $entity->getName(),
93
            $entity->getPosition(),
94
            $entity->getOffice(),
95
            $entity->getAge(),
96
            null !== $entity->getStartDate() ? $entity->getStartDate()->format("Y-m-d") : "",
97
            $entity->getSalary(),
98
        ];
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function getCSVExporter(): ?DataTablesCSVExporterInterface {
105
        return $this;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function getColumns(): array {
112
113
        $dtColumns = [];
114
115
        $dtColumns[] = DataTablesFactory::newColumn("name", "Name");
116
        $dtColumns[] = DataTablesFactory::newColumn("position", "Position");
117
        $dtColumns[] = DataTablesFactory::newColumn("office", "Office");
118
        $dtColumns[] = DataTablesFactory::newColumn("age", "Age");
119
        $dtColumns[] = DataTablesFactory::newColumn("startDate", "Start date");
120
        $dtColumns[] = DataTablesFactory::newColumn("salary", "Salary");
121
        $dtColumns[] = DataTablesFactory::newColumn("actions", "Actions")
122
            ->setOrderable(false)
123
            ->setSearchable(false);
124
125
        return $dtColumns;
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function getEditor(): ?DataTablesEditorInterface {
132
        return $this;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function getEntity(): string {
139
        return Employee::class;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function getMethod(): string {
146
        return "POST";
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152
    public function getName(): string {
153
        return "employee";
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function getOptions(): DataTablesOptionsInterface {
160
        return DataTablesFactory::newOptions();
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166
    public function getPrefix(): string {
167
        return "e";
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function getView(): ?string {
174
        return null;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180
    public function renderColumn(DataTablesColumnInterface $dtColumn, $entity): ?string {
181
182
        $output = null;
183
184
        switch ($dtColumn->getData()) {
185
186
            case "actions":
187
                $output = "";
188
                break;
189
190
            case "age":
191
                $output = $entity->getAge();
192
                break;
193
194
            case "name":
195
                $output = $entity->getName();
196
                break;
197
198
            case "office":
199
                $output = $entity->getOffice();
200
                break;
201
202
            case "position":
203
                $output = $entity->getPosition();
204
                break;
205
206
            case "salary":
207
                $output = $entity->getSalary();
208
                break;
209
210
            case "startDate":
211
                if (null !== $entity->getStartDate()) {
212
                    $output = $entity->getStartDate()->format("Y-m-d");
213
                }
214
                break;
215
        }
216
217
        return $output;
218
    }
219
220
    /**
221
     * {@inheritDoc}
222
     */
223
    public function renderRow(string $dtRow, $entity, int $rowNumber) {
224
225
        $output = null;
226
227
        switch ($dtRow) {
228
229
            case DataTablesResponseInterface::DATATABLES_ROW_ATTR:
230
                break;
231
232
            case DataTablesResponseInterface::DATATABLES_ROW_CLASS:
233
                $output = (0 === $rowNumber % 2 ? "even" : "odd");
234
                break;
235
236
            case DataTablesResponseInterface::DATATABLES_ROW_DATA:
237
                $output = ["pkey" => $entity->getId()];
238
                break;
239
240
            case DataTablesResponseInterface::DATATABLES_ROW_ID:
241
                $output = "employee_" . $entity->getId();
242
                break;
243
        }
244
245
        return $output;
246
    }
247
}
248