Skip to content

Commit

Permalink
Fixed #22 Add method \Gietos\Dadata\Service\Suggestions::suggestEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kasatkin committed Oct 26, 2017
1 parent 8000b40 commit d4ca33c
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/Model/Response/Suggestions/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Gietos\Dadata\Model\Response\Suggestions;

use Gietos\Dadata\Model\AbstractModel;

class Email extends AbstractModel
{
/**
* @var string
*/
protected $local;

/**
* @var string
*/
protected $domain;

public function getLocal(): string
{
return $this->local;
}

public function setLocal(string $local)
{
$this->local = $local;
}

public function getDomain(): string
{
return $this->domain;
}

public function setDomain(string $domain)
{
$this->domain = $domain;
}
}
21 changes: 21 additions & 0 deletions src/Model/Response/Suggestions/EmailSuggestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Gietos\Dadata\Model\Response\Suggestions;

class EmailSuggestion extends AbstractSuggestion
{
/**
* @var Email
*/
private $data;

public function getData(): Email
{
return $this->data;
}

public function setData(Email $data)
{
$this->data = $data;
}
}
24 changes: 24 additions & 0 deletions src/Model/Response/Suggestions/EmailSuggestionCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Gietos\Dadata\Model\Response\Suggestions;

use Gietos\Dadata\Model\AbstractCollection;

class EmailSuggestionCollection extends AbstractCollection
{
/**
* @param EmailSuggestion[] $elements
*/
public function __construct(array $elements = [])
{
parent::__construct($elements);
}

/**
* @inheritdoc
*/
protected function getClass(): string
{
return EmailSuggestion::class;
}
}
23 changes: 23 additions & 0 deletions src/Model/Response/Suggestions/EmailSuggestionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Gietos\Dadata\Model\Response\Suggestions;

use Gietos\Dadata\Model\AbstractModel;

class EmailSuggestionResponse extends AbstractModel
{
/**
* @var EmailSuggestionCollection
*/
private $suggestions;

public function getSuggestions(): EmailSuggestionCollection
{
return $this->suggestions;
}

public function setSuggestions(EmailSuggestionCollection $suggestions)
{
$this->suggestions = $suggestions;
}
}
16 changes: 14 additions & 2 deletions src/Service/Suggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Gietos\Dadata\Model\Response\Error;
use Gietos\Dadata\Model\Response\Suggestions\AddressSuggestionsCollection;
use Gietos\Dadata\Model\Response\Suggestions\AddressSuggestionsResponse;
use Gietos\Dadata\Model\Response\Suggestions\EmailSuggestionCollection;
use Gietos\Dadata\Model\Response\Suggestions\EmailSuggestionResponse;
use Gietos\Dadata\Model\Response\Suggestions\FioSuggestionsCollection;
use Gietos\Dadata\Model\Response\Suggestions\FioSuggestionsResponse;

Expand Down Expand Up @@ -68,8 +70,18 @@ public function suggestBank()
// todo
}

public function suggestEmail()
/**
* @param string $query
* @return EmailSuggestionCollection|Error
*/
public function suggestEmail(string $query)
{
// todo
$request = $this->apiClient->createRequest('POST', $this->getBaseUri() . '/suggest/email', ['query' => $query]);
$response = $this->apiClient->sendRequest($request);
$result = $this->getResult($request, $response, EmailSuggestionResponse::class);
if ($result instanceof EmailSuggestionResponse) {
return $result->getSuggestions();
}
return $result;
}
}
57 changes: 57 additions & 0 deletions tests/SuggestionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Gietos\Dadata\Tests;

use Gietos\Dadata\Api;
use Gietos\Dadata\Model\Response\Suggestions\Email;
use Gietos\Dadata\Model\Response\Suggestions\EmailSuggestion;
use Gietos\Dadata\Model\Response\Suggestions\EmailSuggestionCollection;
use Gietos\Dadata\Service\Suggestions;
use GuzzleHttp\Psr7\Response;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Http\Message\UriFactory\DiactorosUriFactory;
use Http\Mock\Client;

class SuggestionsTest extends BaseTestCase
{
/**
* @var Suggestions
*/
private $suggestionsService;

/**
* @var Client
*/
private $httpClient;

public function setUp()
{
$this->httpClient = new Client;
$apiClient = new Api(
'token',
'',
$this->httpClient,
new DiactorosMessageFactory,
new DiactorosStreamFactory,
new DiactorosUriFactory
);
$this->suggestionsService = new Suggestions($apiClient);
}

public function testSuggestEmail()
{
$this->httpClient->addResponse(new Response(200, [], $this->loadDataFile('suggest-email.json')));
$result = $this->suggestionsService->suggestEmail('anton@');
$this->assertInstanceOf(EmailSuggestionCollection::class, $result);
/** @var EmailSuggestion $suggestion */
$suggestion = $result->current();
$this->assertInstanceOf(EmailSuggestion::class, $suggestion);
$this->assertEquals('[email protected]', $suggestion->getValue());
$this->assertEquals('[email protected]', $suggestion->getUnrestrictedValue());
$data = $suggestion->getData();
$this->assertInstanceOf(Email::class, $data);
$this->assertEquals('anton', $data->getLocal());
$this->assertEquals('mail.ru', $data->getDomain());
}
}
13 changes: 13 additions & 0 deletions tests/data/suggest-email.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"suggestions": [
{
"value": "[email protected]",
"unrestricted_value": "[email protected]",
"data": {
"local": "anton",
"domain": "mail.ru",
"qc": null
}
}
]
}

0 comments on commit d4ca33c

Please sign in to comment.