Completed
Pull Request — master (#176)
by ru cheng
02:33
created

BaseMessage::buildForBroadcastPreview()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * BaseMessage.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\Messages;
17
18
use Overtrue\Wechat\Utils\MagicAttributes;
19
use Overtrue\Wechat\Utils\XML;
20
21
/**
22
 * 消息基类
23
 *
24
 * @property string      $from
25
 * @property string      $to
26
 * @property string      $staff
27
 *
28
 * @method BaseMessage to($to)
29
 * @method BaseMessage from($from)
30
 * @method BaseMessage staff($staff)
31
 * @method array       toStaff()
32
 * @method array       toReply()
33
 * @method array       toBroadcast()
34
 */
35
abstract class BaseMessage extends MagicAttributes
36
{
37
38
    /**
39
     * 允许的属性
40
     *
41
     * @var array
42
     */
43
    protected $properties = array();
44
45
    /**
46
     * 基础属性
47
     *
48
     * @var array
49
     */
50
    protected $baseProperties = array(
51
                                 'from',
52
                                 'to',
53
                                 'to_group',
54
                                 'to_all',
55
                                 'staff',
56
                                );
57
58
    /**
59
     * 生成用于主动推送的数据
60
     *
61
     * @return array
62
     */
63
    public function buildForStaff()
64
    {
65
        if (!method_exists($this, 'toStaff')) {
66
            throw new \Exception(__CLASS__.'未实现此方法:toStaff()');
67
        }
68
69
        $base = array(
70
                 'touser'  => $this->to,
71
                 'msgtype' => $this->getDefaultMessageType(),
72
                );
73
        if (!empty($this->staff)) {
74
            $base['customservice'] = array('kf_account' => $this->staff);
75
        }
76
77
        return array_merge($base, $this->toStaff());
78
    }
79
80
    /**
81
     * 生成用于回复的数据
82
     *
83
     * @return array
84
     */
85
    public function buildForReply()
86
    {
87
        if (!method_exists($this, 'toReply')) {
88
            throw new \Exception(__CLASS__.'未实现此方法:toReply()');
89
        }
90
91
        $base = array(
92
                 'ToUserName'   => $this->to,
93
                 'FromUserName' => $this->from,
94
                 'CreateTime'   => time(),
95
                 'MsgType'      => $this->getDefaultMessageType(),
96
                );
97
98
        return XML::build(array_merge($base, $this->toReply()));
99
    }
100
101
    /**
102
     * 生成通过群发的数据
103
     *
104
     * @return array
105
     */
106
    public function buildForBroadcast()
107
    {
108
        if (!method_exists($this, 'toStaff')) {
109
            throw new \Exception(__CLASS__.'未实现此方法:toStaff()');
110
        }
111
112
        if (is_null($this->to_group)) {
0 ignored issues
show
Documentation introduced by
The property to_group does not exist on object<Overtrue\Wechat\Messages\BaseMessage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
            $group = array(
114
                'filter' => array(
115
                    'is_to_all' => true,
116
                ),
117
            );
118
        } elseif (is_array($this->to_group)) {
0 ignored issues
show
Documentation introduced by
The property to_group does not exist on object<Overtrue\Wechat\Messages\BaseMessage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
119
            $group = array(
120
                'touser' => $this->to_group,
0 ignored issues
show
Documentation introduced by
The property to_group does not exist on object<Overtrue\Wechat\Messages\BaseMessage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
            );
122
        } else {
123
            $group = array(
124
                'filter' => array(
125
                    'is_to_all' => false,
126
                    'group_id' => $this->to_group,
0 ignored issues
show
Documentation introduced by
The property to_group does not exist on object<Overtrue\Wechat\Messages\BaseMessage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
                ),
128
            );
129
        }
130
131
        $base = array(
132
            'msgtype' => $this->getDefaultMessageType(),
133
        );
134
135
        return array_merge($group, $this->toStaff(), $base);
136
    }
137
138
    /**
139
     * 生成通过群发预览的数据
140
     *
141
     * @param $type
142
     *
143
     * @return array
144
     *
145
     * @throws \Exception
146
     */
147
    public function buildForBroadcastPreview($type)
148
    {
149
        if (!method_exists($this, 'toStaff')) {
150
            throw new \Exception(__CLASS__.'未实现此方法:toStaff()');
151
        }
152
153
        $base = array(
154
            $type => $this->to,
155
            'msgtype' => $this->getDefaultMessageType(),
156
        );
157
158
        return array_merge($base, $this->toStaff());
159
    }
160
161
    /**
162
     * 获取默认的消息类型名称
163
     *
164
     * @return string
165
     */
166
    public function getDefaultMessageType()
167
    {
168
        $class = explode('\\', get_class($this));
169
170
        return strtolower(array_pop($class));
171
    }
172
173
    /**
174
     * 验证
175
     *
176
     * @param string $attribute
177
     * @param mixed  $value
178
     *
179
     * @return bool
180
     */
181
    protected function validate($attribute, $value)
182
    {
183
        $properties = array_merge($this->baseProperties, $this->properties);
184
185
        return in_array($attribute, $properties, true);
186
    }
187
}
188