1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MagicAttributes.php |
4
|
|
|
* |
5
|
|
|
* @author Tian. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Wechat\Utils\Menu; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* 用于操作通用数组式属性的工具类 |
14
|
|
|
*/ |
15
|
|
|
abstract class MagicAttributes |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 允许设置的属性名称 |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $attributes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 方法名转换缓存 |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $snakeCache = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 设置属性 |
34
|
|
|
* |
35
|
|
|
* @param $attribute |
36
|
|
|
* @param $value |
37
|
|
|
* |
38
|
|
|
* @return \Wechat\Utils\Menu\MagicAttributes |
39
|
|
|
*/ |
40
|
|
|
public function setAttribute($attribute, $value) |
41
|
|
|
{ |
42
|
|
|
return $this->with($attribute, $value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 设置属性 |
47
|
|
|
* |
48
|
|
|
* @param string $attribute |
49
|
|
|
* @param mixed $value |
50
|
|
|
* |
51
|
|
|
* @return MagicAttributes |
52
|
|
|
*/ |
53
|
|
|
public function with($attribute, $value) |
54
|
|
|
{ |
55
|
|
|
$attribute = $this->snake($attribute); |
56
|
|
|
|
57
|
|
|
if (!$this->validate($attribute, $value)) { |
58
|
|
|
throw new InvalidArgumentException("错误的属性值'{$attribute}'"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->attributes[$attribute] = $value; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* 生成数组 |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function toArray() |
72
|
|
|
{ |
73
|
|
|
return $this->attributes; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* 验证 |
78
|
|
|
* |
79
|
|
|
* @param string $attribute |
80
|
|
|
* @param mixed $value |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function validate($attribute, $value) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 调用不存在的方法 |
91
|
|
|
* |
92
|
|
|
* @param string $method |
93
|
|
|
* @param array $args |
94
|
|
|
* |
95
|
|
|
* @return MagicAttributes |
96
|
|
|
*/ |
97
|
|
|
public function __call($method, $args) |
98
|
|
|
{ |
99
|
|
|
if (stripos($method, 'with') === 0) { |
100
|
|
|
$method = substr($method, 4); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->with($method, array_shift($args)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* 魔术读取 |
108
|
|
|
* |
109
|
|
|
* @param $property |
110
|
|
|
* |
111
|
|
|
* @return null |
112
|
|
|
*/ |
113
|
|
|
public function __get($property) |
114
|
|
|
{ |
115
|
|
|
return !isset($this->attributes[$property]) ? null : $this->attributes[$property]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 魔术写入 |
120
|
|
|
* |
121
|
|
|
* @param $property |
122
|
|
|
* @param $value |
123
|
|
|
* |
124
|
|
|
* @return \Wechat\Utils\Menu\MagicAttributes |
125
|
|
|
*/ |
126
|
|
|
public function __set($property, $value) |
127
|
|
|
{ |
128
|
|
|
return $this->with($property, $value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 转换为下划线模式字符串 |
133
|
|
|
* |
134
|
|
|
* @param string $value |
135
|
|
|
* @param string $delimiter |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
protected function snake($value, $delimiter = '_') |
140
|
|
|
{ |
141
|
|
|
$key = $value . $delimiter; |
142
|
|
|
|
143
|
|
|
if (isset(static::$snakeCache[$key])) { |
144
|
|
|
return static::$snakeCache[$key]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (!ctype_lower($value)) { |
148
|
|
|
$value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1' . $delimiter, $value)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return static::$snakeCache[$key] = $value; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.