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

ArrayDataTrait::assertFieldName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
trait ArrayDataTrait  
5
{
6
    use PropertyDataTrait;
7
    /**
8
     * 表数据
9
     *
10
     * @var array
11
     */
12
    protected $data = [];
13
14
    
15
    public function offsetSet($offset, $value)
16
    {
17
        $this->__set($offset, $value);
18
    }
19
20
    public function offsetExists($offset)
21
    {
22
        return $this->__isset($offset);
23
    }
24
25
    public function offsetUnset($offset)
26
    {
27
        $this->__unset($offset);
28
    }
29
30
    public function offsetGet($offset)
31
    {
32
        return $this->__get($offset);
33
    }
34
35
    /**
36
     * 设置值
37
     *
38
     * @param string $name
39
     * @param mixed $value
40
     */
41
    public function __set(string $name, $value)
42
    {
43
        $this->assertFieldName($name);
44
        $this->data[$name] = $value;
45
    }
46
47
    
48
    
49
    /**
50
     * 获取参数值
51
     *
52
     * @param string $name
53
     * @return mixed
54
     */
55
    public function __get(string $name)
56
    {
57
        $this->assertFieldName($name);
58
        return $this->data[$name] ?? null;
59
    }
60
61
    /**
62
     * 判断是否设置
63
     *
64
     * @param string $name
65
     * @return boolean
66
     */
67
    public function __isset(string $name)
68
    {
69
        $this->assertFieldName($name);
70
        return array_key_exists($name, $this->data);
71
    }
72
73
    /**
74
     * 取消设置值
75
     *
76
     * @param string $name
77
     */
78
    public function __unset(string $name)
79
    {
80
        $this->assertFieldName($name);
81
        unset($this->data[$name]);
82
    }
83
84
    /**
85
     * 断言字段
86
     *
87
     * @param string $name
88
     * @return void
89
     */
90
    protected function assertFieldName(string $name)
91
    {
92
        if ($this->checkFieldExist($name) === false) {
0 ignored issues
show
introduced by
The condition $this->checkFieldExist($name) === false is always false.
Loading history...
93
            throw new InvalidArgumentException(sprintf('[%s] has no attribute %s', static::class, $name), 0);
0 ignored issues
show
Bug introduced by
The type suda\orm\struct\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
94
        }
95
    }
96
97
    /**
98
     * 检查字段是否存在
99
     *
100
     * @param string $name
101
     * @return boolean
102
     */
103
    public function checkFieldExist(string $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function checkFieldExist(/** @scrutinizer ignore-unused */ string $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        return true;
106
    }
107
}
108