Passed
Push — master ( 566a3a...fbef46 )
by 世昌
02:19
created

TableStructMiddleware   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createProccorFromStruct() 0 6 2
A createProccorFromClass() 0 6 2
A inputName() 0 3 1
A prepareProcessorSet() 0 11 3
A outputName() 0 3 1
A __construct() 0 4 1
A rewriteFromClassDoc() 0 7 4
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