Skip to content

Commit

Permalink
Merge pull request #6 from ywchang0612/mitake/mitake-php
Browse files Browse the repository at this point in the history
Mitake/mitake php
  • Loading branch information
minchao authored Nov 26, 2021
2 parents 3a6f35c + fbe5288 commit 911d4a8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.3"
"guzzlehttp/guzzle": "^6.3|^7.3"
},
"require-dev": {
"codacy/coverage": "^1.4",
Expand Down
10 changes: 9 additions & 1 deletion src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ public function sendBatch(array $messages)
*/
public function send(Message\Message $message)
{
return $this->sendBatch([$message]);
$request = $this->client->newRequest(
'POST',
$this->client->buildUriWithQuery('/api/mtk/SmSend', ['CharsetURL' => 'UTF-8'] + $message->toArray()),
'application/x-www-form-urlencoded'
);

$response = $this->client->sendRequest($request);

return $this->parseMessageResponse($response);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Mitake;

use function GuzzleHttp\Psr7\build_query;
use function GuzzleHttp\Psr7\uri_for;
use GuzzleHttp\Psr7\Query;
use GuzzleHttp\Psr7\Utils;
use Mitake\Exception\BadResponseException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client as GuzzleHttpClient;
Expand All @@ -26,15 +26,15 @@
*/
class Client
{
const LIBRARY_VERSION = '0.3.0';
const LIBRARY_VERSION = '0.4.0';

const DEFAULT_BASE_URL = 'https://smexpress.mitake.com.tw:9601';
const DEFAULT_BASE_URL = 'https://smsapi.mitake.com.tw';

const DEFAULT_HTTP_BASE_URL = 'http://smexpress.mitake.com.tw:9600';
const DEFAULT_HTTP_BASE_URL = 'http://smsapi.mitake.com.tw';

const DEFAULT_LONG_MESSAGE_BASE_URL = 'https://smexpress.mitake.com.tw:7102';
const DEFAULT_LONG_MESSAGE_BASE_URL = 'https://smsapi.mitake.com.tw';

const DEFAULT_LONG_MESSAGE_HTTP_BASE_URL = 'http://smexpress.mitake.com.tw:7002';
const DEFAULT_LONG_MESSAGE_HTTP_BASE_URL = 'http://smsapi.mitake.com.tw';

const DEFAULT_USER_AGENT = 'mitake-php/' . self::LIBRARY_VERSION;

Expand Down Expand Up @@ -250,7 +250,7 @@ public function newRequest($method, $uri, $contentType = null, $body = null)
*/
public function buildUriWithQuery($uri, array $params = [])
{
$uri = uri_for($uri);
$uri = Utils::uriFor($uri);

if (!Uri::isAbsolute($uri)) {
$uri = $uri->withScheme($this->baseURL->getScheme())
Expand All @@ -264,7 +264,7 @@ public function buildUriWithQuery($uri, array $params = [])
'password' => $this->password,
];

return $uri->withQuery(build_query(array_merge($default, $params)));
return $uri->withQuery(Query::build(array_merge($default, $params)));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,24 @@ public function toINI()

return $ini;
}

public function toArray()
{
$param = [
'dstaddr' => $this->dstaddr,
'smbody' => $this->smbody,
];

if (!empty($this->dlvtime)) {
$param['dlvtime'] = $this->dlvtime;
}
if (!empty($this->vldtime)) {
$param['vldtime'] = $this->vldtime;
}
if (!empty($this->response)) {
$param['response'] = $this->response;
}

return $param;
}
}
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getBuildUriWithQueryCases()
Client::DEFAULT_BASE_URL . '?username=username&password=password&encoding=UTF8',
],
[
'path/sub',
'/path/sub',
['encoding' => 'UTF8'],
Client::DEFAULT_BASE_URL . '/path/sub?username=username&password=password&encoding=UTF8',
],
Expand Down
19 changes: 19 additions & 0 deletions tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ public function testToINI($obj)

$this->assertEquals($expected, $obj->toINI());
}

/**
* @depends testConstruct
* @param Message $obj
*/
public function testToArray($obj)
{
$smBody = 'Hello,' . chr(6) . '世界';

$expected = [
'dstaddr' => '0987654321',
'smbody' => $smBody,
'dlvtime' => '60',
'vldtime' => '120',
'response' => 'https://example.com',
];

$this->assertEquals($expected, $obj->toArray());
}
}

0 comments on commit 911d4a8

Please sign in to comment.