Completed
Pull Request — master (#292)
by Carlos
03:42
created

User::batchGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * User.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\User;
22
23
use EasyWeChat\Core\AbstractAPI;
24
25
/**
26
 * Class User.
27
 */
28
class User extends AbstractAPI
29
{
30
    const API_GET = 'https://api.weixin.qq.com/cgi-bin/user/info';
31
    const API_BATCH_GET = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget';
32
    const API_LIST = 'https://api.weixin.qq.com/cgi-bin/user/get';
33
    const API_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/getid';
34
    const API_REMARK = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark';
35
    const API_OAUTH_GET = 'https://api.weixin.qq.com/sns/userinfo';
36
37
    /**
38
     * Fetch a user by open id.
39
     *
40
     * @param string $openId
41
     * @param string $lang
42
     *
43
     * @return array
44
     */
45 1 View Code Duplication
    public function get($openId, $lang = 'zh_CN')
46
    {
47
        $params = [
48 1
                   'openid' => $openId,
49 1
                   'lang' => $lang,
50 1
                  ];
51
52 1
        return $this->parseJSON('get', [self::API_GET, $params]);
53
    }
54
55
    /**
56
     * Batch get users.
57
     *
58
     * @param array  $openIds
59
     * @param string $lang
60
     *
61
     * @return \EasyWeChat\Support\Collection
62
     */
63 1
    public function batchGet(array $openIds, $lang = 'zh_CN')
64
    {
65 1
        $params = [];
66
67 1
        $params['user_list'] = array_map(function ($openId) use ($lang) {
68
            return [
69 1
                    'openid' => $openId,
70 1
                    'lang' => $lang,
71 1
                    ];
72 1
        }, $openIds);
73
74 1
        return $this->parseJSON('json', [self::API_BATCH_GET, $params]);
75
    }
76
77
    /**
78
     * List users.
79
     *
80
     * @param string $nextOpenId
81
     *
82
     * @return \EasyWeChat\Support\Collection
83
     */
84 1
    public function lists($nextOpenId = null)
85
    {
86 1
        $params = ['next_openid' => $nextOpenId];
87
88 1
        return $this->parseJSON('get', [self::API_LIST, $params]);
89
    }
90
91
    /**
92
     * Set user remark.
93
     *
94
     * @param string $openId
95
     * @param string $remark
96
     *
97
     * @return bool
98
     */
99 1 View Code Duplication
    public function remark($openId, $remark)
100
    {
101
        $params = [
102 1
                   'openid' => $openId,
103 1
                   'remark' => $remark,
104 1
                  ];
105
106 1
        return $this->parseJSON('json', [self::API_REMARK, $params]);
107
    }
108
109
    /**
110
     * Get user's group id.
111
     *
112
     * @param string $openId
113
     *
114
     * @return int
115
     */
116 1
    public function group($openId)
117
    {
118 1
        return $this->getGroup($openId);
119
    }
120
121
    /**
122
     * Get user's group.
123
     *
124
     * @param string $openId
125
     *
126
     * @return array
127
     */
128 1
    public function getGroup($openId)
129
    {
130 1
        $params = ['openid' => $openId];
131
132 1
        return $this->parseJSON('json', [self::API_GROUP, $params]);
133
    }
134
}
135