Add data\Controller
[hoplite.git] / data / controller.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\data;
18 use \hoplite\http as http;
19
20 require_once HOPLITE_ROOT . '/http/response_code.php';
21 require_once HOPLITE_ROOT . '/http/rest_action.php';
22
23 /*!
24 A Controller is a RESTful http\Action that is used to bind a data\Model to a
25 web interface. Subclass this in order to perform business logic such as
26 validation and authentication.
27
28 This class is semi-abstract in that it cannot be used directly. At minimum,
29 FilterRequest() needs to be overridden to select the Model to use.
30 */
31 class Controller extends http\RestAction
32 {
33 /*! @var hoplite\data\Model The object that will be operated on. */
34 protected $model = NULL;
35
36 /*! Selects the Model object. */
37 public function FilterRequest(http\Request $request, http\Response $response)
38 {
39 // Example:
40 // $this->model = new webapp\models\User();
41 throw new ControllerException('Model not selected');
42 }
43
44 /*! Gets the data from the model. */
45 protected function _DoGet(http\Request $request, http\Response $response)
46 {
47 $this->model->SetFrom($request->data);
48 try {
49 $response->data = $this->model->Fetch();
50 } catch (ModelException $e) {
51 $response->data['message'] = $e->GetMessage();
52 $response->response_code = http\ResponseCode::NOT_FOUND;
53 }
54 }
55
56 /*! Updates an object in the store. */
57 protected function _DoPost(http\Request $request, http\Response $response)
58 {
59 $this->model->SetFrom($request->data);
60 try {
61 $this->model->Update();
62 $response->data = $this->model->Fetch();
63 } catch (ModelException $e) {
64 $response->data['message'] = $e->GetMessage();
65 $response->response_code = http\ResponseCode::NOT_FOUND;
66 }
67 }
68
69 /*! Deletes the object from the store. */
70 protected function _DoDelete(http\Request $request, http\Response $response)
71 {
72 $this->model->SetFrom($request->data);
73 try {
74 $this->model->Delete();
75 } catch (ModelException $e) {
76 $response->data['message'] = $e->GetMessage();
77 $response->response_code = http\ResponseCode::BAD_REQUEST;
78 }
79 }
80
81 /*! Updates an object in the store. */
82 protected function _DoPut(http\Request $request, http\Response $response)
83 {
84 $this->model->SetFrom($request->data);
85 try {
86 $this->model->Insert();
87 $response->data = $this->model->Fetch();
88 } catch (ModelException $e) {
89 $response->data['message'] = $e->GetMessage();
90 $response->response_code = http\ResponseCode::BAD_REQUEST;
91 }
92 }
93 }
94
95 class ControllerException extends \Exception {}