Skip to content

Commit 04504fa

Browse files
committed
新增 Model 访问器修饰器
1 parent 9b10944 commit 04504fa

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

App/Controller/apiController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
class apiController extends Controller {
99

10-
public function testGet($id){
11-
dump($this->request->getMethod());
10+
public function testGet(){
11+
$demo = new Demo();
12+
$demo->abc = 1;
13+
$demo->acb = 1;
1214

15+
return $this->response($demo);
1316
}
1417

1518
// ORM 查询类方法演示

App/Model/Demo.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ class Demo extends Model
99
{
1010
// 数据表名称
1111
public static $table = 'demo';
12+
13+
// 访问器
14+
public function getPhpinfo(){
15+
echo phpinfo();
16+
}
17+
18+
// 修饰器
19+
public function setAbc($value){
20+
return "Baby ".$value;
21+
}
1222
}

System/Model.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static function all(){
4949

5050
/**
5151
* 数据表字段检索
52-
* @param string $field, string $string
52+
* @param string $field
53+
* @param string $string
5354
* @return Collection
5455
*/
5556
public static function search($field,$string){
@@ -86,7 +87,8 @@ public static function raw($sqlStatement){
8687

8788
/**
8889
* Model 分页
89-
* @param int $pageNum, boolean $furtherPageInfo
90+
* @param int $pageNum
91+
* @param boolean $furtherPageInfo
9092
* @return Collection
9193
* @uses Model 集合分页功能
9294
*/
@@ -187,16 +189,41 @@ public static function makeCollection($array = []) {
187189

188190
public function __set($name, $value) {
189191
// $demo->property = "text";
190-
return $this->objectData[$name] = $value;
192+
193+
// 通过反射判断是否已经预定义修饰器
194+
try {
195+
$modelClass = new \ReflectionClass(static::class);
196+
if ($modelClass->hasMethod("set".ucfirst($name))){
197+
$methodName = "set".ucfirst($name);
198+
$value = $this->$methodName($value);
199+
}
200+
} catch (\ReflectionException $e) {
201+
// do nothing..
202+
} finally {
203+
return $this->objectData[$name] = $value;
204+
}
191205
}
192206

193207
public function __get($name) {
194208
// echo $demo->property
195-
if (isset($this->objectData[$name])){
196-
return $this->objectData[$name];
197-
} else {
198-
return null;
209+
210+
// 通过反射判断是否已经预定义访问器
211+
try {
212+
$modelClass = new \ReflectionClass(static::class);
213+
if ($modelClass->hasMethod("get".ucfirst($name))){
214+
$methodName = "get".ucfirst($name);
215+
return $this->$methodName();
216+
}
217+
} catch (\ReflectionException $e) {
218+
// do nothing..
219+
} finally {
220+
if (isset($this->objectData[$name])){
221+
return $this->objectData[$name];
222+
} else {
223+
return null;
224+
}
199225
}
226+
200227
}
201228

202229
public function __isset($name) {
@@ -228,6 +255,7 @@ public function __toString() {
228255

229256
/**
230257
* 将实例转换为 JSON 字符串
258+
* @param $option
231259
* @return string
232260
*/
233261
public function toJson($option = 0) {

0 commit comments

Comments
 (0)