From d4ca33c609abba3cd6f0d9a18eb5e1bccd488538 Mon Sep 17 00:00:00 2001 From: Sergey Kasatkin Date: Thu, 26 Oct 2017 00:59:31 +0300 Subject: [PATCH] Fixed #22 Add method \Gietos\Dadata\Service\Suggestions::suggestEmail --- src/Model/Response/Suggestions/Email.php | 38 +++++++++++++ .../Response/Suggestions/EmailSuggestion.php | 21 +++++++ .../Suggestions/EmailSuggestionCollection.php | 24 ++++++++ .../Suggestions/EmailSuggestionResponse.php | 23 ++++++++ src/Service/Suggestions.php | 16 +++++- tests/SuggestionsTest.php | 57 +++++++++++++++++++ tests/data/suggest-email.json | 13 +++++ 7 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/Model/Response/Suggestions/Email.php create mode 100644 src/Model/Response/Suggestions/EmailSuggestion.php create mode 100644 src/Model/Response/Suggestions/EmailSuggestionCollection.php create mode 100644 src/Model/Response/Suggestions/EmailSuggestionResponse.php create mode 100644 tests/SuggestionsTest.php create mode 100644 tests/data/suggest-email.json diff --git a/src/Model/Response/Suggestions/Email.php b/src/Model/Response/Suggestions/Email.php new file mode 100644 index 0000000..29b7c91 --- /dev/null +++ b/src/Model/Response/Suggestions/Email.php @@ -0,0 +1,38 @@ +local; + } + + public function setLocal(string $local) + { + $this->local = $local; + } + + public function getDomain(): string + { + return $this->domain; + } + + public function setDomain(string $domain) + { + $this->domain = $domain; + } +} diff --git a/src/Model/Response/Suggestions/EmailSuggestion.php b/src/Model/Response/Suggestions/EmailSuggestion.php new file mode 100644 index 0000000..2fde345 --- /dev/null +++ b/src/Model/Response/Suggestions/EmailSuggestion.php @@ -0,0 +1,21 @@ +data; + } + + public function setData(Email $data) + { + $this->data = $data; + } +} diff --git a/src/Model/Response/Suggestions/EmailSuggestionCollection.php b/src/Model/Response/Suggestions/EmailSuggestionCollection.php new file mode 100644 index 0000000..ddfb8cb --- /dev/null +++ b/src/Model/Response/Suggestions/EmailSuggestionCollection.php @@ -0,0 +1,24 @@ +suggestions; + } + + public function setSuggestions(EmailSuggestionCollection $suggestions) + { + $this->suggestions = $suggestions; + } +} diff --git a/src/Service/Suggestions.php b/src/Service/Suggestions.php index 34f7b19..12d11b2 100644 --- a/src/Service/Suggestions.php +++ b/src/Service/Suggestions.php @@ -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; @@ -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; } } diff --git a/tests/SuggestionsTest.php b/tests/SuggestionsTest.php new file mode 100644 index 0000000..7b4f6d9 --- /dev/null +++ b/tests/SuggestionsTest.php @@ -0,0 +1,57 @@ +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('anton@mail.ru', $suggestion->getValue()); + $this->assertEquals('anton@mail.ru', $suggestion->getUnrestrictedValue()); + $data = $suggestion->getData(); + $this->assertInstanceOf(Email::class, $data); + $this->assertEquals('anton', $data->getLocal()); + $this->assertEquals('mail.ru', $data->getDomain()); + } +} diff --git a/tests/data/suggest-email.json b/tests/data/suggest-email.json new file mode 100644 index 0000000..3743662 --- /dev/null +++ b/tests/data/suggest-email.json @@ -0,0 +1,13 @@ +{ + "suggestions": [ + { + "value": "anton@mail.ru", + "unrestricted_value": "anton@mail.ru", + "data": { + "local": "anton", + "domain": "mail.ru", + "qc": null + } + } + ] +}