Passed
Push — sudav3 ( b29c56...468d62 )
by 世昌
02:20
created

ObjectMiddleware::input()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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