Passed
Push — sudav3 ( 468d62...6cc4c6 )
by 世昌
02:44
created

ObjectMiddleware::outputName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\middleware;
3
4
use ReflectionClass;
5
use ReflectionProperty;
6
use suda\orm\TableStruct;
7
use suda\orm\middleware\NullMiddleware;
8
use suda\orm\struct\TableStructBuilder;
9
10
/**
11
 * 结构中间件
12
 */
13
class ObjectMiddleware extends NullMiddleware
14
{
15
    /**
16
     * 处理方式
17
     *
18
     * @var array
19
     */
20
    protected $processor;
21
22
    /**
23
     * 数据对象
24
     *
25
     * @var string
26
     */
27
    protected $object;
28
29
    /**
30
     * 字段映射处理
31
     *
32
     * @var array
33
     */
34
    protected $nameAlias;
35
36
    const RAW = 0;
37
    const SERIALIZE = 2;
38
39
    /**
40
     * 创建中间件
41
     *
42
     * @param string $object
43
     */
44
    public function __construct(string $object)
45
    {
46
        $this->prepareProcessorSet($object);
47
        $this->object = $object;
48
    }
49
50
    /**
51
     * 处理输入数据
52
     *
53
     * @param string $name
54
     * @param mixed $data
55
     * @return mixed
56
     */
57
    public function input(string $name, $data)
58
    {
59
        if ($this->processor[$name] === ObjectMiddleware::SERIALIZE) {
60
            return \serialize($data);
61
        }
62
        return $data;
63
    }
64
65
    /**
66
     * 处理输出数据
67
     *
68
     * @param string $name
69
     * @param mixed $data
70
     * @return mixed
71
     */
72
    public function output(string $name, $data)
73
    {
74
        if ($this->processor[$name] === ObjectMiddleware::SERIALIZE) {
75
            return \unserialize($data);
76
        }
77
        return $data;
78
    }
79
80
    /**
81
     * 创建处理集合
82
     *
83
     * @param string $object
84
     * @return void
85
     */
86
    protected function prepareProcessorSet(string $object)
87
    {
88
        $reflectObject = new ReflectionClass($object);
89
        $this->processor = [];
90
        foreach ($reflectObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $property) {
91
            $name = $this->getFieldName($property);
92
            $this->processor[$name] = $this->getProcessorType($property);
93
            $this->nameAlias[$name] = $property->getName();
94
        }
95
    }
96
97
    /**
98
     * 获取字段名
99
     *
100
     * @param ReflectionProperty $property
101
     * @return string
102
     */
103
    protected function getFieldName(ReflectionProperty $property)
104
    {
105
        return $property->getName();
106
    }
107
108
    /**
109
     * 处理输入字段名
110
     */
111
    public function inputName(string $name):string
112
    {
113
        return TableStructBuilder::getTableFieldName($this->object, $name);
114
    }
115
116
    /**
117
     * 处理输出字段名
118
     *
119
     * @param string $name
120
     * @param mixed $data
121
     * @return mixed
122
     */
123
    public function outputName(string $name):string
124
    {
125
        return $this->nameAlias[$name] ?? $name;
126
    }
127
128
    /**
129
     * 获取处理方式
130
     *
131
     * @param ReflectionProperty $property
132
     * @return integer
133
     */
134
    protected function getProcessorType(ReflectionProperty $property):int {
135
        if ($doc = $property->getDocComment()) {
136
            if (is_string($doc) && preg_match('/@var\s+(\w+)/i', $doc, $match)) {
137
                $type = \strtolower($match[1]);
138
                if (\in_array($type, ['boolean', 'bool', 'integer', 'int' , 'float' , 'double', 'string'])) {
139
                    return ObjectMiddleware::RAW;
140
                }
141
            }
142
        }
143
        return ObjectMiddleware::SERIALIZE;
144
    }
145
}
146