Completed
Push — master ( 460c8a...1ec27c )
by Carlos
03:26
created

Menu::test()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Menu.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    overtrue <[email protected]>
11
 * @copyright 2015 overtrue <[email protected]>
12
 * @link      https://github.com/overtrue
13
 * @link      http://overtrue.me
14
 */
15
16
namespace Overtrue\Wechat;
17
18
use Closure;
19
20
/**
21
 * 菜单
22
 *
23
 * @property array $sub_button
24
 */
25
class Menu
26
{
27
    const API_CREATE             = 'https://api.weixin.qq.com/cgi-bin/menu/create';
28
    const API_GET                = 'https://api.weixin.qq.com/cgi-bin/menu/get';
29
    const API_DELETE             = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
30
    const API_QUERY              = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info';
31
    const API_CONDITIONAL_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/addconditional';
32
    const API_CONDITIONAL_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delconditional';
33
    const API_CONDITIONAL_TEST   = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch';
34
35
    /**
36
     * Http对象
37
     *
38
     * @var Http
39
     */
40
    protected $http;
41
42
    /**
43
     * constructor
44
     *
45
     * @param string $appId
46
     * @param string $appSecret
47
     */
48
    public function __construct($appId, $appSecret)
49
    {
50
        $this->http = new Http(new AccessToken($appId, $appSecret));
51
    }
52
53
    /**
54
     * 设置菜单
55
     *
56
     * @return bool
57
     */
58
    public function set($menus)
59
    {
60
        $menus = $this->extractMenus($menus);
61
62
        $this->http->jsonPost(self::API_CREATE, array('button' => $menus));
63
64
        return true;
65
    }
66
67
    /**
68
     * 获取菜单
69
     *
70
     * @return array
71
     */
72
    public function get()
73
    {
74
        $menus = $this->http->get(self::API_GET);
75
76
        return empty($menus['menu']['button']) ? array() : $menus['menu']['button'];
77
    }
78
79
    /**
80
     * 删除菜单
81
     *
82
     * @return bool
83
     */
84
    public function delete()
85
    {
86
        $this->http->get(self::API_DELETE);
87
88
        return true;
89
    }
90
91
    /**
92
     * 获取菜单【查询接口,能获取到任意方式设置的菜单】
93
     *
94
     * @return array
95
     */
96
    public function current()
97
    {
98
        $menus = $this->http->get(self::API_QUERY);
99
        return empty($menus) ? array() : $menus;
100
    }
101
102
    /**
103
     * 添加个性化的菜单
104
     *
105
     * @param mixed $menus
106
     * @param array $condition
107
     */
108
    public function addConditional($menus, array $condition)
109
    {
110
        $menus = $this->extractMenus($menus);
111
112
        $this->http->jsonPost(self::API_CONDITIONAL_CREATE, array('button' => $menus, 'matchrule' => $condition));
113
114
        return true;
115
    }
116
117
    /**
118
     * 测试菜单
119
     *
120
     * @param string $userId
121
     *
122
     * @return boolean
123
     */
124
    public function test($userId)
125
    {
126
        return $this->http->post(self::API_CONDITIONAL_TEST, array('user_id' => $userId));
127
    }
128
129
    /**
130
     * 按菜单ID删除菜单
131
     *
132
     * @param int $menuId
133
     *
134
     * @return boolean
135
     */
136
    public function deleteById($menuId)
137
    {
138
        $this->http->post(self::API_CONDITIONAL_DELETE, array('menuid' => $menuId));
139
140
        return true;
141
    }
142
143
    /**
144
     * 转menu为数组
145
     *
146
     * @param mixed $menus
147
     *
148
     * @return array
149
     */
150
    protected function extractMenus($menus)
151
    {
152
        if ($menus instanceof Closure) {
153
            $menus = $menus($this);
154
        }
155
156
        if (!is_array($menus)) {
157
            throw new Exception('子菜单必须是数组或者匿名函数返回数组', 1);
158
        }
159
160
        foreach ($menus as $key => $menu) {
161
            $menus[$key] = $menu->toArray();
162
163
            if ($menu->sub_button) {
164
                $menus[$key]['sub_button'] = $this->extractMenus($menu->sub_button);
165
            }
166
        }
167
168
        return $menus;
169
    }
170
}
171