1 | <?php |
||
12 | class Client |
||
13 | { |
||
14 | /** |
||
15 | * $apiEndpoint. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $apiEndpoint = 'http://api.every8d.com/API21/HTTP'; |
||
20 | |||
21 | /** |
||
22 | * $credit. |
||
23 | * |
||
24 | * @var float |
||
25 | */ |
||
26 | public $credit = null; |
||
27 | |||
28 | /** |
||
29 | * __construct. |
||
30 | * |
||
31 | * @param string $userId |
||
32 | * @param string $password |
||
33 | * @param \Http\Client\HttpClient $httpClient |
||
34 | * @param \Http\Message\MessageFactory $messageFactory |
||
35 | */ |
||
36 | 2 | public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
43 | |||
44 | /** |
||
45 | * setCredit. |
||
46 | * |
||
47 | * @param string $credit |
||
48 | */ |
||
49 | 2 | protected function setCredit($credit) |
|
55 | |||
56 | /** |
||
57 | * credit. |
||
58 | * |
||
59 | * @return float |
||
60 | */ |
||
61 | 2 | public function credit() |
|
62 | { |
||
63 | 2 | if (is_null($this->credit) === false) { |
|
64 | 1 | return $this->credit; |
|
65 | } |
||
66 | |||
67 | 1 | $response = $this->doRequest('getCredit.ashx', [ |
|
68 | 1 | 'UID' => $this->userId, |
|
69 | 1 | 'PWD' => $this->password, |
|
70 | 1 | ]); |
|
71 | |||
72 | 1 | if ($this->isValidResponse($response) === false) { |
|
73 | throw new DomainException($response); |
||
74 | } |
||
75 | |||
76 | 1 | return $this->setCredit($response) |
|
77 | 1 | ->credit; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * send. |
||
82 | * |
||
83 | * @param array $params |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 1 | public function send($params) |
|
88 | { |
||
89 | 1 | $response = $this->doRequest('sendSMS.ashx', array_filter([ |
|
90 | 1 | 'UID' => $this->userId, |
|
91 | 1 | 'PWD' => $this->password, |
|
92 | 1 | 'SB' => isset($params['subject']) ? $params['subject'] : null, |
|
93 | 1 | 'MSG' => $params['text'], |
|
94 | 1 | 'DEST' => $params['to'], |
|
95 | 1 | 'ST' => isset($params['ST']) ? Carbon::parse($params['ST'])->format('YmdHis') : null, |
|
96 | 1 | ])); |
|
97 | |||
98 | 1 | if ($this->isValidResponse($response) === false) { |
|
99 | throw new DomainException($response, 500); |
||
100 | } |
||
101 | |||
102 | 1 | list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response); |
|
103 | |||
104 | return [ |
||
105 | 1 | 'credit' => $this->setCredit($credit)->credit, |
|
106 | 1 | 'sended' => (int) $sended, |
|
107 | 1 | 'cost' => (float) $cost, |
|
108 | 1 | 'unsend' => (int) $unsend, |
|
109 | 1 | 'batchId' => $batchId, |
|
110 | 1 | ]; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * isValidResponse. |
||
115 | * |
||
116 | * @param string $response |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | 2 | protected function isValidResponse($response) |
|
124 | |||
125 | /** |
||
126 | * doRequest. |
||
127 | * |
||
128 | * @param string $uri |
||
129 | * @param array $params |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 2 | protected function doRequest($uri, $params) |
|
145 | } |
||
146 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: