|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\database\struct; |
|
3
|
|
|
|
|
4
|
|
|
use function array_column; |
|
5
|
|
|
use function preg_match_all; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionException; |
|
8
|
|
|
use ReflectionProperty; |
|
9
|
|
|
use suda\database\middleware\ObjectMiddleware; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* 结构中间件 |
|
13
|
|
|
*/ |
|
14
|
|
|
class TableStructMiddleware extends ObjectMiddleware |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* 数据结构 |
|
18
|
|
|
* |
|
19
|
|
|
* @var TableStruct |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $struct; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 创建中间件 |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $object |
|
27
|
|
|
* @param TableStruct $struct |
|
28
|
|
|
* @throws ReflectionException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(string $object, TableStruct $struct) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->struct = $struct; |
|
33
|
|
|
parent::__construct($object); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 处理输入字段名 |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function inputName(string $name):string |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->struct->inputName($name); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* 处理输出字段名 |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function outputName(string $name):string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->struct->outputName($name); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* 创建处理集合 |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $object |
|
61
|
|
|
* @return void |
|
62
|
|
|
* @throws ReflectionException |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function prepareProcessorSet(string $object) |
|
65
|
|
|
{ |
|
66
|
|
|
$reflectObject = new ReflectionClass($object); |
|
67
|
|
|
$classDoc = is_string($reflectObject->getDocComment())?$reflectObject->getDocComment():''; |
|
68
|
|
|
$field = TableClassStructBuilder::readClassDocField($classDoc); |
|
69
|
|
|
if ($field !== null) { |
|
70
|
|
|
$this->createProccorFromStruct(); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->createProccorFromClass($reflectObject); |
|
73
|
|
|
} |
|
74
|
|
|
$this->rewriteFromClassDoc($classDoc); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function createProccorFromStruct() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->processor = []; |
|
80
|
|
|
$fields = $this->struct; |
|
81
|
|
|
foreach ($fields as $key => $value) { |
|
82
|
|
|
$this->processor[$key] = ObjectMiddleware::RAW; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function rewriteFromClassDoc(string $classDoc) |
|
87
|
|
|
{ |
|
88
|
|
|
if (preg_match_all('/@field-(serialize|json)\s+(\w+)/i', $classDoc, $matchs) > 0) { |
|
89
|
|
|
foreach ($matchs[0] as $index => $value) { |
|
90
|
|
|
$match = array_column($matchs, $index); |
|
91
|
|
|
list($comment, $type, $name) = $match; |
|
92
|
|
|
$this->processor[$name] = strtolower($type) === 'json' ? ObjectMiddleware::JSON : ObjectMiddleware::SERIALIZE; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function createProccorFromClass(ReflectionClass $reflectObject) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->processor = []; |
|
100
|
|
|
foreach ($reflectObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $property) { |
|
101
|
|
|
$name = $this->inputName($property->getName()); |
|
102
|
|
|
$this->processor[$name] = $this->getProcessorType($property); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|