ShareFactory.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
 * Created by PhpStorm.
 * User: Hanson
 * Date: 2017/1/15
 * Time: 12:29
 */
namespace Hanson\Vbot\Message;
use Hanson\Vbot\Message\Entity\File;
use Hanson\Vbot\Message\Entity\Mina;
use Hanson\Vbot\Message\Entity\Official;
use Hanson\Vbot\Message\Entity\Share;
use Hanson\Vbot\Support\Content;
class ShareFactory
{
    protected $xml;
    public $type;
    public function make($msg)
    {
        $xml = Content::formatContent($msg['Content']);
        $this->parse($xml);
        if($this->type == 6){
            return new File($msg);
        }else if(official()->get($msg['FromUserName'])){
            return new Official($msg);
        }else if($this->type == 33){
            return new Mina($msg);
        }else{
            return new Share($msg);
        }
    }
    private function parse($xml)
    {
        if(starts_with($xml, '@')){
            $xml = preg_replace('/(@\S+:\\n)/', '', $xml);
        }
        $array = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $this->xml = $info = (array)$array['appmsg'];
        $this->type = $info['type'];
    }
}