@@ -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 */
0 commit comments