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

ObjectMiddleware::input()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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