/
Controller.php
174 lines (158 loc) · 4.41 KB
/
Controller.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace dee\rest;
use Yii;
use yii\db\ActiveRecord;
use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException;
use yii\filters\ContentNegotiator;
use yii\web\Response;
use yii\filters\VerbFilter;
/**
* Controller
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class Controller extends \yii\web\Controller
{
/**
* @var string the model class name. This property must be set.
*/
public $modelClass;
public $modelSearchClass;
/**
* @inheritdoc
*/
public $enableCsrfValidation = false;
/**
* @var string|array the configuration for creating the serializer that formats the response data.
*/
public $serializer = 'dee\rest\Serializer';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->modelClass === null) {
throw new InvalidConfigException('The "modelClass" property must be set.');
}
}
/**
* @inheritdoc
*/
public function actions()
{
return[
'options' => [
'class' => 'yii\rest\OptionsAction',
],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'contentNegotiator' => [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
],
],
'verbFilter' => [
'class' => VerbFilter::className(),
'actions' => $this->verbs(),
],
];
}
/**
* @inheritdoc
*/
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
return $this->serializeData($result);
}
/**
* Declares the allowed HTTP verbs.
* Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
* @return array the allowed HTTP verbs.
*/
protected function verbs()
{
return [];
}
/**
* Serializes the specified data.
* The default implementation will create a serializer based on the configuration given by [[serializer]].
* It then uses the serializer to serialize the given data.
* @param mixed $data the data to be serialized
* @return mixed the serialized data.
*/
protected function serializeData($data)
{
return Yii::createObject($this->serializer)->serialize($data);
}
/**
* Finds the model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer|array $id
* @return ActiveRecord the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
$keys = $modelClass::primaryKey();
if (count($keys) > 1) {
$values = explode(',', $id);
if (count($keys) === count($values)) {
$model = $modelClass::findOne(array_combine($keys, $values));
}
} elseif ($id !== null) {
$model = $modelClass::findOne($id);
}
if (isset($model)) {
return $model;
} else {
throw new NotFoundHttpException("Object not found: '$id'");
}
}
/**
* @return ActiveRecord
*/
protected function createModel()
{
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
return new $modelClass();
}
/**
* Patch resource
* @param ActiveRecord $model
* @param array $patch
*/
protected function doPatch($model, $patch)
{
$op = isset($patch['op']) ? $patch['op'] : 'replace';
switch ($op) {
case 'remove':
$model->{$patch['field']} = null;
break;
case 'move':
$model->{$patch['field']} = $model->{$patch['from']};
$model->{$patch['from']} = null;
break;
case 'copy':
$model->{$patch['field']} = $model->{$patch['from']};
break;
default:
$model->{$patch['field']} = $patch['value'];
break;
}
}
}