Skip to content

Commit 948a00c

Browse files
perf: trait controller convert abstract
1 parent e9a8973 commit 948a00c

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

src/trait/Controller.php renamed to src/abstract/controller.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
11
<?php
22
declare(strict_types=1);
3-
namespace RestJS\Trait;
3+
namespace RestJS\Abstract;
44

55
use RestJS\Class\Response;
66
use function RestJS\response, RestJS\checkNull;
77

8-
/** Core Controller Functions */
9-
trait Controller {
8+
/** Abstract Controller Functions */
9+
abstract class Controller {
1010

1111
/** Entity or Table All Data */
1212
private $data;
1313

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) {
1630

1731
/** Filter Column Query Params */
1832
$filter = $req->getQueryParams()['filter'] ?? null;
1933

2034
/** Filter Data */
2135
$data = $this->data;
22-
36+
2337
// Selected Column Fetch All Data
2438
if ($filter):
2539
$data = [];
2640
$filter = explode(",", $filter);
27-
41+
2842
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)));
3044
endif;
3145

3246
return response($req, $res, new Response(data: $data));
3347
}
3448

3549
/** Find Data by Column */
3650
public function findByColumn($req, $res, $args) {
37-
51+
3852
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]);
4054

4155
checkNull($data, $req);
4256
return response($req, $res, args: new Response(data: [...$data]));
4357
}
4458

4559
/** Delete Data by Id */
46-
public function delete($req, $res, $args) {
60+
public function delete($req, $res, $args) {
4761
$data = $this->model->delete($args['id']);
4862
checkNull($data, $req);
4963
return response($req, $res, new Response(message: "This item has been successfully removed.", data: $data));

src/api/category/controller.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
declare(strict_types=1);
33
namespace RestJS\Api\Category;
44

5-
use RestJS\Trait\Controller as CoreController;
5+
use RestJS\Abstract\Controller as AbstractController;
66
use RestJS\Api\Category\Model;
77

8-
class Controller {
8+
class Controller extends AbstractController {
99

1010
function __construct(private Model $model) {
11-
$this->data = $this->model->findAll();
11+
parent::__construct();
1212
}
1313

14-
// Trait Controller
15-
use CoreController;
14+
protected function setModel() {
15+
return $this->model;
16+
}
17+
18+
protected function setData() {
19+
return $this->model->findAll();
20+
}
1621
}

src/trait/GetterAndSetter.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)