MenuApi::extractMenus()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
namespace Wechat\API;
3
4
use Closure;
5
6
/**
7
 * 微信菜单相关接口.
8
 *
9
 * @author Tian.
10
 */
11
class MenuApi extends BaseApi
12
{
13
14
    /**
15
     * 设置菜单
16
     *
17
     * @param $menus
18
     *
19
     * @return array|bool
20
     */
21 View Code Duplication
    public function set($menus)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        if ($menus instanceof Closure) {
24
            $menus = $menus($this);
25
        }
26
27
        if (!is_array($menus)) {
28
            $this->setError('子菜单必须是数组或者匿名函数返回数组');
29
30
            return false;
31
        }
32
33
        $menus = $this->extractMenus($menus);
34
35
        $data = ['button' => $menus];
36
37
        $res = $this->_post('create', $data);
38
39
        return $res;
40
    }
41
42
    /**
43
     * 获取菜单
44
     *
45
     * @return array
46
     */
47
    public function get()
48
    {
49
        $queryStr = [];
50
51
        $res = $this->_get('get', $queryStr);
52
53
        return $res;
54
    }
55
56
    /**
57
     * 获取菜单【查询接口,能获取到任意方式设置的菜单】
58
     *
59
     * @return array
60
     */
61
    public function current()
62
    {
63
        $queryStr = [];
64
65
        $this->module = 'get_current_selfmenu_info';
66
67
        $res = $this->_get('', $queryStr);
68
69
        return $res;
70
    }
71
72
    /**
73
     * 删除菜单
74
     *
75
     * @return array
76
     */
77
    public function delete()
78
    {
79
        $queryStr = [];
80
81
        $res = $this->_get('delete', $queryStr);
82
83
        return $res;
84
    }
85
86
    /**
87
     * 转menu为数组
88
     *
89
     * @param array $menus
90
     *
91
     * @return array
92
     */
93
    protected function extractMenus(array $menus)
94
    {
95
        foreach ($menus as $key => $menu) {
96
            $menus[$key] = $menu->toArray();
97
98
            if ($menu->sub_button) {
99
                $menus[$key]['sub_button'] = $this->extractMenus($menu->sub_button);
100
            }
101
        }
102
103
        return $menus;
104
    }
105
106
    /**
107
     * 设置个性化菜单
108
     *
109
     * @author Jia <[email protected]>
110
     *
111
     * @date   2017-04-11
112
     *
113
     * @param  array $menus     菜单数组
114
     * @param  array $matchrule 个性化规则
115
     *
116
     * @return array|bool
117
     */
118 View Code Duplication
    public function setIndividuationMenu($menus, $matchrule)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        if ($menus instanceof Closure) {
121
            $menus = $menus($this);
122
        }
123
124
        if (!is_array($menus)) {
125
            $this->setError('子菜单必须是数组或者匿名函数返回数组');
126
127
            return false;
128
        }
129
        $menus = $this->extractMenus($menus);
130
131
        $data = [
132
            'button'    => $menus,
133
            'matchrule' => $matchrule,
134
        ];
135
136
        $res = $this->_post('addconditional', $data);
137
138
        return $res;
139
    }
140
141
    /**
142
     * 测试个性化菜单匹配结果
143
     *
144
     * @author Jia <[email protected]>
145
     *
146
     * @date   2017-04-11
147
     *
148
     * @param  string $openid 用户openid
149
     *
150
     * @return array
151
     */
152
    public function tryMatchUser($openid)
153
    {
154
        $queryStr = [
155
            "user_id" => $openid,
156
        ];
157
158
        $res = $this->_post('trymatch', $queryStr);
159
160
        return $res;
161
    }
162
163
    /**
164
     * 删除个性化菜单
165
     *
166
     * @author Jia <[email protected]>
167
     *
168
     * @date   2017-04-12
169
     *
170
     * @param  int $menuId 个性化菜单id
171
     *
172
     * @return array
173
     */
174
    public function deleteIndividuationMenu($menuId)
175
    {
176
        $queryStr = [
177
            "menuid" => $menuId,
178
        ];
179
180
        $res = $this->_post('delconditional', $queryStr);
181
182
        return $res;
183
    }
184
}
185