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

TableStructMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
use ReflectionClass;
5
use ReflectionProperty;
6
use suda\orm\TableStruct;
7
use suda\orm\middleware\ObjectMiddleware;
8
9
/**
10
 * 结构中间件
11
 */
12
class TableStructMiddleware extends ObjectMiddleware
13
{
14
    /**
15
     * 数据结构
16
     *
17
     * @var TableStruct
18
     */
19
    protected $struct;
20
21
    /**
22
     * 创建中间件
23
     *
24
     * @param string $object
25
     */
26
    public function __construct(string $object, TableStruct $struct)
27
    {
28
        $this->object = $object;
29
        $this->struct = $struct;
30
        $this->prepareProcessorSet($object);
31
    }
32
33
    /**
34
     * 处理输入字段名
35
     */
36
    public function inputName(string $name):string
37
    {
38
        return $this->struct->getFields()->inputName($name);
39
    }
40
41
    /**
42
     * 处理输出字段名
43
     *
44
     * @param string $name
45
     * @param mixed $data
46
     * @return mixed
47
     */
48
    public function outputName(string $name):string
49
    {
50
        return $this->struct->getFields()->outputName($name);
51
    }    
52
    
53
    /**
54
     * 创建处理集合
55
     *
56
     * @param string $object
57
     * @return void
58
     */
59
    protected function prepareProcessorSet(string $object)
60
    {
61
        $reflectObject = new ReflectionClass($object);
62
        $this->processor = [];
63
        foreach ($reflectObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $property) {
64
            $name = $this->inputName($property->getName());
65
            $this->processor[$name] = $this->getProcessorType($property);
66
        }
67
    }
68
}
69