Blame view

vendor/alibabacloud/client/src/Result/Result.php 2.65 KB
f0d6cde5   xu   vendor
1
2
3
4
<?php

namespace AlibabaCloud\Client\Result;

0c34aba8   xu   更新阿里云短信接口
5
6
7
8
9
10
11
use AlibabaCloud\Client\Request\Request;
use AlibabaCloud\Client\Traits\HasDataTrait;
use GuzzleHttp\Psr7\Response;

/**
 * Result from Alibaba Cloud
 *
f0d6cde5   xu   vendor
12
13
 * @property string|null RequestId
 *
f0d6cde5   xu   vendor
14
15
16
17
18
19
20
21
 * @package   AlibabaCloud\Client\Result
 */
class Result implements \ArrayAccess, \IteratorAggregate, \Countable
{
    use HasDataTrait;

    /**
     * Instance of the response.
0c34aba8   xu   更新阿里云短信接口
22
     *
f0d6cde5   xu   vendor
23
24
25
26
     * @var Response
     */
    protected $response;

f0d6cde5   xu   vendor
27
28
29
30
31
32
33
34
35
    /**
     * Instance of the request.
     *
     * @var Request
     */
    protected $request;

    /**
     * Result constructor.
0c34aba8   xu   更新阿里云短信接口
36
37
     *
     * @param Response $response
f0d6cde5   xu   vendor
38
     * @param Request  $request
0c34aba8   xu   更新阿里云短信接口
39
     */
f0d6cde5   xu   vendor
40
    public function __construct(Response $response, Request $request = null)
0c34aba8   xu   更新阿里云短信接口
41
42
43
44
45
46
47
48
49
50
51
52
    {
        $format = ($request instanceof Request) ? \strtoupper($request->format) : 'JSON';

        switch ($format) {
            case 'JSON':
                $data = $this->jsonToArray($response->getBody()->getContents());
                break;
            case 'XML':
                $data = $this->xmlToArray($response->getBody()->getContents());
                break;
            case 'RAW':
                $data = $this->jsonToArray($response->getBody()->getContents());
f0d6cde5   xu   vendor
53
                break;
0c34aba8   xu   更新阿里云短信接口
54
55
56
57
58
            default:
                $data = $this->jsonToArray($response->getBody()->getContents());
        }

        if (empty($data)) {
f0d6cde5   xu   vendor
59
            $data = [];
0c34aba8   xu   更新阿里云短信接口
60
        }
f0d6cde5   xu   vendor
61
62

        $this->dot($data);
0c34aba8   xu   更新阿里云短信接口
63
        $this->response = $response;
f0d6cde5   xu   vendor
64
65
        $this->request  = $request;
    }
0c34aba8   xu   更新阿里云短信接口
66

f0d6cde5   xu   vendor
67
68
    /**
     * @param string $response
0c34aba8   xu   更新阿里云短信接口
69
     *
f0d6cde5   xu   vendor
70
71
     * @return array
     */
0c34aba8   xu   更新阿里云短信接口
72
73
    private function jsonToArray($response)
    {
f0d6cde5   xu   vendor
74
75
        try {
            return \GuzzleHttp\json_decode($response, true);
0c34aba8   xu   更新阿里云短信接口
76
77
78
79
80
81
82
83
84
85
86
        } catch (\InvalidArgumentException $e) {
            return [];
        }
    }

    /**
     * @param string $string
     *
     * @return array
     */
    private function xmlToArray($string)
f0d6cde5   xu   vendor
87
88
89
90
91
92
93
94
95
96
97
    {
        try {
            return json_decode(json_encode(simplexml_load_string($string)), true);
        } catch (\Exception $exception) {
            return [];
        }
    }

    /**
     * @return string
     */
0c34aba8   xu   更新阿里云短信接口
98
    public function __toString()
f0d6cde5   xu   vendor
99
100
101
102
103
104
105
106
107
108
109
110
111
    {
        return (string)$this->response->getBody();
    }

    /**
     * @return Request
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
0c34aba8   xu   更新阿里云短信接口
112
     * @return Response
f0d6cde5   xu   vendor
113
114
115
116
117
118
119
120
121
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * @return bool
     */
0c34aba8   xu   更新阿里云短信接口
122
    public function isSuccess()
f0d6cde5   xu   vendor
123
124
125
126
127
    {
        return 200 <= $this->response->getStatusCode()
               && 300 > $this->response->getStatusCode();
    }
}
0c34aba8   xu   更新阿里云短信接口

f0d6cde5   xu   vendor

0c34aba8   xu   更新阿里云短信接口

f0d6cde5   xu   vendor

0c34aba8   xu   更新阿里云短信接口

f0d6cde5   xu   vendor

0c34aba8   xu   更新阿里云短信接口

f0d6cde5   xu   vendor