Passed
Push — sudav3 ( 36433a...9b090b )
by 世昌
02:24
created

ObjectMiddleware::inputName()   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 1
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
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
    const RAW = 0;
30
    const SERIALIZE = 2;
31
32
    /**
33
     * 创建中间件
34
     *
35
     * @param string $object
36
     */
37
    public function __construct(string $object)
38
    {
39
        $this->prepareProcessorSet($object);
40
        $this->object = $object;
41
    }
42
43
    /**
44
     * 处理输入数据
45
     *
46
     * @param string $name
47
     * @param mixed $data
48
     * @return mixed
49
     */
50
    public function input(string $name, $data)
51
    {
52
        if ($this->processor[$name] === ObjectMiddleware::SERIALIZE) {
53
            return \serialize($data);
54
        }
55
        return $data;
56
    }
57
58
    /**
59
     * 处理输出数据
60
     *
61
     * @param string $name
62
     * @param mixed $data
63
     * @return mixed
64
     */
65
    public function output(string $name, $data)
66
    {
67
        if ($this->processor[$name] === ObjectMiddleware::SERIALIZE) {
68
            return \unserialize($data) ?: null;
69
        }
70
        return $data;
71
    }
72
73
    /**
74
     * 创建处理集合
75
     *
76
     * @param string $object
77
     * @return void
78
     */
79
    protected function prepareProcessorSet(string $object)
80
    {
81
        $reflectObject = new ReflectionClass($object);
82
        $this->processor = [];
83
        foreach ($reflectObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $property) {
84
            $this->processor[$property->getName()] = $this->getProcessorType($property);
85
        }
86
    }
87
88
    /**
89
     * 获取处理方式
90
     *
91
     * @param ReflectionProperty $property
92
     * @return integer
93
     */
94
    protected function getProcessorType(ReflectionProperty $property):int
95
    {
96
        if ($doc = $property->getDocComment()) {
97
            if (is_string($doc) && preg_match('/@var\s+(\w+)/i', $doc, $match)) {
98
                $type = \strtolower($match[1]);
99
                if (\in_array($type, ['boolean', 'bool', 'integer', 'int' , 'float' , 'double', 'string'])) {
100
                    return ObjectMiddleware::RAW;
101
                }
102
            }
103
        }
104
        return ObjectMiddleware::SERIALIZE;
105
    }
106
}
107