|
1 | 1 | <?php
|
2 | 2 | declare(strict_types=1);
|
3 |
| -namespace RestJS\Trait; |
| 3 | +namespace RestJS\Abstract; |
4 | 4 |
|
5 | 5 | use RestJS\Class\Response;
|
6 | 6 | use function RestJS\response, RestJS\checkNull;
|
7 | 7 |
|
8 |
| -/** Core Controller Functions */ |
9 |
| -trait Controller { |
| 8 | +/** Abstract Controller Functions */ |
| 9 | +abstract class Controller { |
10 | 10 |
|
11 | 11 | /** Entity or Table All Data */
|
12 | 12 | private $data;
|
13 | 13 |
|
14 |
| - /** Find All Data */ |
15 |
| - public function findAll($req, $res) { |
| 14 | + /** Model Class */ |
| 15 | + private $model; |
| 16 | + |
| 17 | + /** Abstract Function for Set Model Class */ |
| 18 | + abstract protected function setModel(); |
| 19 | + |
| 20 | + /** Abstract Function for Set Table Data */ |
| 21 | + abstract protected function setData(); |
| 22 | + |
| 23 | + function __construct() { |
| 24 | + $this->model = $this->setModel(); |
| 25 | + $this->data = $this->setdata(); |
| 26 | + } |
| 27 | + |
| 28 | + /** Find All Data */ |
| 29 | + public function findAll($req, $res) { |
16 | 30 |
|
17 | 31 | /** Filter Column Query Params */
|
18 | 32 | $filter = $req->getQueryParams()['filter'] ?? null;
|
19 | 33 |
|
20 | 34 | /** Filter Data */
|
21 | 35 | $data = $this->data;
|
22 |
| - |
| 36 | + |
23 | 37 | // Selected Column Fetch All Data
|
24 | 38 | if ($filter):
|
25 | 39 | $data = [];
|
26 | 40 | $filter = explode(",", $filter);
|
27 |
| - |
| 41 | + |
28 | 42 | foreach ($this->data as $item)
|
29 |
| - array_push($data, array_intersect_key((array) $item, array_flip($filter))); |
| 43 | + array_push($data, array_intersect_key((array) $item, array_flip($filter))); |
30 | 44 | endif;
|
31 | 45 |
|
32 | 46 | return response($req, $res, new Response(data: $data));
|
33 | 47 | }
|
34 | 48 |
|
35 | 49 | /** Find Data by Column */
|
36 | 50 | public function findByColumn($req, $res, $args) {
|
37 |
| - |
| 51 | + |
38 | 52 | foreach ($args as $key => $value)
|
39 |
| - $data = array_filter($this->data, fn($item) => $item->$key == $args[$key]); |
| 53 | + $data = array_filter($this->data, fn($item) => $item->$key == $args[$key]); |
40 | 54 |
|
41 | 55 | checkNull($data, $req);
|
42 | 56 | return response($req, $res, args: new Response(data: [...$data]));
|
43 | 57 | }
|
44 | 58 |
|
45 | 59 | /** Delete Data by Id */
|
46 |
| - public function delete($req, $res, $args) { |
| 60 | + public function delete($req, $res, $args) { |
47 | 61 | $data = $this->model->delete($args['id']);
|
48 | 62 | checkNull($data, $req);
|
49 | 63 | return response($req, $res, new Response(message: "This item has been successfully removed.", data: $data));
|
|
0 commit comments