Skip to content

Commit a2e66e9

Browse files
committed
Added Serialization to main client.
1 parent c2e23de commit a2e66e9

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Client.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Nexmo\Client\Factory\MapFactory;
1717
use Nexmo\Client\Response\Response;
1818
use Nexmo\Client\Signature;
19+
use Nexmo\Entity\EntityInterface;
20+
use Nexmo\Verify\Verification;
1921
use Psr\Http\Message\RequestInterface;
2022
use Zend\Diactoros\Uri;
2123

@@ -233,6 +235,28 @@ public function send(\Psr\Http\Message\RequestInterface $request)
233235
return $response;
234236
}
235237

238+
public function serialize(EntityInterface $entity)
239+
{
240+
if($entity instanceof Verification){
241+
return $this->verify()->serialize($entity);
242+
}
243+
244+
throw new \RuntimeException('unknown class `' . get_class($entity) . '``');
245+
}
246+
247+
public function unserialize($entity)
248+
{
249+
if(is_string($entity)){
250+
$entity = unserialize($entity);
251+
}
252+
253+
if($entity instanceof Verification){
254+
return $this->verify()->unserialize($entity);
255+
}
256+
257+
throw new \RuntimeException('unknown class `' . get_class($entity) . '``');
258+
}
259+
236260
public function __call($name, $args)
237261
{
238262
if(!$this->factory->hasApi($name)){

test/ClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Nexmo\Client\Credentials\Basic;
1616
use Nexmo\Client\Credentials\OAuth;
1717
use Nexmo\Client\Signature;
18+
use Nexmo\Verify\Verification;
1819
use Zend\Diactoros\Request;
1920
use Zend\Diactoros\Response;
2021

@@ -269,6 +270,25 @@ public function testUserAgentString()
269270
$this->assertEquals($expected, $agent);
270271
}
271272

273+
public function testSerializationProxiesVerify()
274+
{
275+
$verify = $this->prophesize('Nexmo\Verify\Client');
276+
$factory = $this->prophesize('Nexmo\Client\Factory\FactoryInterface');
277+
278+
$factory->hasApi('verify')->willReturn(true);
279+
$factory->getApi('verify')->willReturn($verify->reveal());
280+
281+
$client = new Client($this->basic);
282+
$client->setFactory($factory->reveal());
283+
284+
$verification = new Verification('15554441212', 'test app');
285+
$verify->serialize($verification)->willReturn('string data')->shouldBeCalled();
286+
$verify->unserialize($verification)->willReturn($verification)->shouldBeCalled();
287+
288+
$this->assertEquals('string data', $client->serialize($verification));
289+
$this->assertEquals($verification, $client->unserialize(serialize($verification)));
290+
}
291+
272292
/**
273293
* Allow tests to check that the API client is correctly forming the HTTP request before sending it to the HTTP
274294
* client.

0 commit comments

Comments
 (0)