This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
/
EntityControllerAbstract.php
142 lines (111 loc) · 3.69 KB
/
EntityControllerAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace App\Controller\Rest\V1;
use App\Exception\FormInvalidException;
use App\Exception\UnauthorizedException;
use App\Model\ModelFactory;
use App\Service\Session;
use App\Form\FormFactory;
use InputValidation\Form;
use Symfony\Component\HttpFoundation\Request;
/**
* @see https://docs.symlex.org/en/latest/framework/rest/
*/
class EntityControllerAbstract
{
protected $modelName = '';
protected $searchFormName = '';
protected $createFormName = '';
protected $editFormName = '';
protected $session;
protected $modelFactory;
protected $formFactory;
protected $model;
public function __construct(Session $session, ModelFactory $modelFactory, FormFactory $formFactory)
{
$this->session = $session;
$this->modelFactory = $modelFactory;
$this->formFactory = $formFactory;
$this->model = $this->createModel($this->modelName);
if (!$this->session->isUser()) {
throw new UnauthorizedException ('Authentication required, please login');
}
}
protected function createForm(string $name)
{
$params = array(
'session' => $this->session
);
return $this->formFactory->create($name, $params);
}
/**
* @param $name
* @return \App\Model\ModelAbstract
*/
protected function createModel(string $name)
{
return $this->modelFactory->create($name);
}
public function cgetAction(Request $request)
{
$form = $this->createForm($this->searchFormName)->setDefinedWritableValues($request->query->all())->validate();
return $this->search($form, $request);
}
protected function search(Form $form, Request $request)
{
if ($form->hasErrors()) {
throw new FormInvalidException($form->getFirstError());
}
$options = array(
'count' => (int)$request->query->get('count', 50),
'offset' => (int)$request->query->get('offset', 0),
'order' => (string)$request->query->get('order', '')
);
return $this->model->search($form->getValues(), $options);
}
public function getAction($id)
{
return $this->model->find($id)->getValues();
}
public function coptionsAction()
{
$form = $this->createForm($this->searchFormName);
return $form->getAsArray();
}
public function optionsAction($id, Request $request)
{
// Only load data for existing users
if ($id == 'new') {
$form = $this->createForm($this->createFormName);
} else {
$form = $this->createForm($this->editFormName);
$this->model->find($id);
$form->setDefinedValues($this->model->getValues());
}
return $form->getAsArray();
}
public function deleteAction($id)
{
$this->model->find($id)->delete();
}
public function putAction($id, Request $request)
{
$this->model->find($id);
$form = $this->createForm($this->editFormName);
$form->setDefinedWritableValues($request->request->all())->validate();
if ($form->hasErrors()) {
throw new FormInvalidException($form->getFirstError());
}
$this->model->update($form->getValues());
return $this->model->getValues();
}
public function postAction(Request $request)
{
$form = $this->createForm($this->createFormName);
$form->setDefinedWritableValues($request->request->all())->validate();
if ($form->hasErrors()) {
throw new FormInvalidException($form->getFirstError());
}
$this->model->save($form->getValues());
return $this->model->getValues();
}
}