Skip to content

Commit 5eda6b6

Browse files
committed
Added support for get() to search messages:
- Query interface could be used with future API changes. - Does not remove the `search` (yet), but that should be able to proxy this. - Needs tests, and *may change*.
1 parent 996fb1c commit 5eda6b6

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

src/Message/Client.php

+68
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,74 @@ public function send($message)
8181
return $message;
8282
}
8383

84+
/**
85+
* @param $query
86+
* @return MessageInterface[]
87+
* @throws Exception\Exception
88+
* @throws Exception\Request
89+
*/
90+
public function get($query)
91+
{
92+
if($query instanceof Query){
93+
$params = $query->getParams();
94+
} else if($query instanceof MessageInterface){
95+
$params = ['ids' => [$query->getMessageId()]];
96+
} else if(is_string($query)) {
97+
$params = ['ids' => [$query]];
98+
} else if(is_array($query)){
99+
$params = ['ids' => $query];
100+
} else {
101+
throw new \InvalidArgumentException('query must be an instance of Query, MessageInterface, string ID, or array of IDs.');
102+
}
103+
104+
$request = new Request(
105+
\Nexmo\Client::BASE_REST . '/search/messages?' . http_build_query($params),
106+
'GET',
107+
'php://temp',
108+
['Accept' => 'application/json']
109+
);
110+
111+
$response = $this->client->send($request);
112+
$response->getBody()->rewind();
113+
$data = json_decode($response->getBody()->getContents(), true);
114+
115+
if($response->getStatusCode() != '200' && isset($data['error-code'])){
116+
throw new Exception\Request($data['error-code-label'], $data['error-code']);
117+
} elseif($response->getStatusCode() != '200'){
118+
throw new Exception\Request('error status from API', $response->getStatusCode());
119+
}
120+
121+
if(!isset($data['items'])){
122+
throw new Exception\Exception('unexpected response from API');
123+
}
124+
125+
if(count($data['items']) == 0){
126+
return [];
127+
}
128+
129+
$collection = [];
130+
131+
foreach($data['items'] as $index => $item){
132+
switch($item['type']){
133+
case 'MT':
134+
$new = new Message($item['message-id']);
135+
break;
136+
case 'MO':
137+
$new = new InboundMessage($item['message-id']);
138+
break;
139+
default:
140+
throw new Exception\Exception('unexpected response from API');
141+
}
142+
143+
$new->setResponse($response);
144+
$new->setIndex($index);
145+
$collection[] = $new;
146+
147+
}
148+
149+
return $collection;
150+
}
151+
84152
/**
85153
* @param string|MessageInterface $idOrMessage
86154
*/

src/Message/CollectionTrait.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Nexmo\Message;
3+
4+
trait CollectionTrait
5+
{
6+
protected $index = null;
7+
8+
public function setIndex($index)
9+
{
10+
$this->index = (int) $index;
11+
}
12+
}

src/Message/InboundMessage.php

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class InboundMessage implements MessageInterface, \ArrayAccess
1616
{
1717
use Psr7Trait;
1818
use JsonResponseTrait;
19+
use CollectionTrait;
1920

2021
protected $id;
2122

@@ -148,6 +149,11 @@ public function getNetwork()
148149
public function offsetExists($offset)
149150
{
150151
$response = $this->getResponseData();
152+
153+
if(isset($this->index)){
154+
$response = $response['items'][$this->index];
155+
}
156+
151157
$request = $this->getRequestData();
152158
$dirty = $this->getRequestData(false);
153159
return isset($response[$offset]) || isset($request[$offset]) || isset($dirty[$offset]);
@@ -164,6 +170,11 @@ public function offsetExists($offset)
164170
public function offsetGet($offset)
165171
{
166172
$response = $this->getResponseData();
173+
174+
if(isset($this->index)){
175+
$response = $response['items'][$this->index];
176+
}
177+
167178
$request = $this->getRequestData();
168179
$dirty = $this->getRequestData(false);
169180

src/Message/Message.php

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Message implements MessageInterface, \Countable, \ArrayAccess, \Iterator
2121
use Psr7Trait;
2222
use JsonResponseTrait;
2323
use RequestArrayTrait;
24+
use CollectionTrait;
2425

2526
const TYPE = null;
2627

@@ -199,6 +200,11 @@ protected function getMessageData($name, $index = null)
199200
public function offsetExists($offset)
200201
{
201202
$response = $this->getResponseData();
203+
204+
if(isset($this->index)){
205+
$response = $response['items'][$this->index];
206+
}
207+
202208
$request = $this->getRequestData();
203209
$dirty = $this->getRequestData(false);
204210
if(isset($response[$offset]) || isset($request[$offset]) || isset($dirty[$offset])){
@@ -216,6 +222,11 @@ public function offsetExists($offset)
216222
public function offsetGet($offset)
217223
{
218224
$response = $this->getResponseData();
225+
226+
if(isset($this->index)){
227+
$response = $response['items'][$this->index];
228+
}
229+
219230
$request = $this->getRequestData();
220231
$dirty = $this->getRequestData(false);
221232

src/Message/Query.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace Nexmo\Message;
3+
4+
class Query
5+
{
6+
protected $params = [];
7+
8+
public function __construct(\DateTime $date, $to)
9+
{
10+
$this->params['date'] = $date->format('Y-m-d');
11+
$this->params['to'] = $to;
12+
}
13+
14+
public function getParams()
15+
{
16+
return $this->params;
17+
}
18+
}

0 commit comments

Comments
 (0)