diff --git a/src/Api/EmailValidation.php b/src/Api/EmailValidation.php index e2063ea..68a0865 100644 --- a/src/Api/EmailValidation.php +++ b/src/Api/EmailValidation.php @@ -1,5 +1,7 @@ $address, @@ -67,25 +69,22 @@ class EmailValidation extends HttpApi * * This operation is only accessible with the private API key and not subject to the daily usage limits. * - * @param string $addresses A delimiter separated list of addresses. Maximum: 8000 characters. - * @param bool|false $syntaxOnly Perform only syntax checks or DNS and ESP specific validation as well. - * The default is True. + * @param string $addresses A delimiter separated list of addresses. Maximum: 8000 characters. + * @param bool| $syntaxOnly Perform only syntax checks or DNS and ESP specific validation as well. + * The default is True. * * @throws InvalidArgumentException Thrown when local validation returns an error * @throws HttpClientException Thrown when there's an error on Client side * @throws HttpServerException Thrown when there's an error on Server side * @throws \Exception Thrown when we don't catch a Client or Server side Exception * - * @return ParseResponse + * @return ParseResponse|ResponseInterface */ - public function parse($addresses, $syntaxOnly = true) + public function parse(string $addresses, bool $syntaxOnly = true) { Assert::stringNotEmpty($addresses); Assert::maxLength($addresses, 8000); - // Validates the Syntax Only verification. - Assert::boolean($syntaxOnly); - $params = [ 'addresses' => $addresses, 'syntax_only' => $syntaxOnly, diff --git a/src/Model/EmailValidation/EmailValidation.php b/src/Model/EmailValidation/EmailValidation.php deleted file mode 100644 index ab27c11..0000000 --- a/src/Model/EmailValidation/EmailValidation.php +++ /dev/null @@ -1,171 +0,0 @@ - - */ -final class EmailValidation -{ - /** - * @var string|null - */ - private $address; - - /** - * @var string|null - */ - private $didYouMean; - - /** - * @var bool - */ - private $isDisposableAddress; - - /** - * @var bool - */ - private $isRoleAddress; - - /** - * @var bool - */ - private $isValid; - - /** - * @var bool - */ - private $mailboxVerification; - - /** - * @var Parts - */ - private $parts; - - /** - * @var string|null - */ - private $reason; - - /** - * EmailValidation constructor. - * - * @param string|null $address - * @param string|null $didYouMean - * @param bool $isDisposableAddress - * @param bool $isRoleAddress - * @param bool $isValid - * @param string|null $mailboxVerification - * @param array $parts - * @param string|null $reason - */ - private function __construct( - $address, - $didYouMean, - $isDisposableAddress, - $isRoleAddress, - $isValid, - $mailboxVerification, - $parts, - $reason - ) { - $this->address = $address; - $this->didYouMean = $didYouMean; - $this->isDisposableAddress = $isDisposableAddress; - $this->isRoleAddress = $isRoleAddress; - $this->isValid = $isValid; - $this->mailboxVerification = 'true' === $mailboxVerification ? true : false; - $this->parts = Parts::create($parts); - $this->reason = $reason; - } - - /** - * @param array $data - * - * @return EmailValidation - */ - public static function create(array $data) - { - return new self( - (isset($data['address']) ? $data['address'] : null), - (isset($data['did_you_mean']) ? $data['did_you_mean'] : null), - (isset($data['is_disposable_address']) ? $data['is_disposable_address'] : false), - (isset($data['is_role_address']) ? $data['is_role_address'] : false), - (isset($data['is_valid']) ? $data['is_valid'] : false), - (isset($data['mailbox_verification']) ? $data['mailbox_verification'] : null), - (isset($data['parts']) ? $data['parts'] : []), - (isset($data['reason']) ? $data['reason'] : null) - ); - } - - /** - * @return null|string - */ - public function getAddress() - { - return $this->address; - } - - /** - * @return null|string - */ - public function getDidYouMean() - { - return $this->didYouMean; - } - - /** - * @return bool - */ - public function isDisposableAddress() - { - return $this->isDisposableAddress; - } - - /** - * @return bool - */ - public function isRoleAddress() - { - return $this->isRoleAddress; - } - - /** - * @return bool - */ - public function isValid() - { - return $this->isValid; - } - - /** - * @return bool - */ - public function isMailboxVerification() - { - return $this->mailboxVerification; - } - - /** - * @return Parts - */ - public function getParts() - { - return $this->parts; - } - - /** - * @return null|string - */ - public function getReason() - { - return $this->reason; - } -} diff --git a/src/Model/EmailValidation/Parse.php b/src/Model/EmailValidation/Parse.php deleted file mode 100644 index 95fd011..0000000 --- a/src/Model/EmailValidation/Parse.php +++ /dev/null @@ -1,67 +0,0 @@ - - */ -final class Parse -{ - /** - * @var array - */ - private $parsed; - - /** - * @var array - */ - private $unparseable; - - /** - * Parse constructor. - * - * @param array $parsed - * @param array $unparseable - */ - private function __construct(array $parsed, array $unparseable) - { - $this->parsed = $parsed; - $this->unparseable = $unparseable; - } - - /** - * @param array $data - * - * @return Parse - */ - public static function create(array $data) - { - return new self( - ((isset($data['parsed']) && is_array($data['parsed'])) ? $data['parsed'] : []), - ((isset($data['unparseable']) && is_array($data['unparseable'])) ? $data['unparseable'] : []) - ); - } - - /** - * @return array - */ - public function getParsed() - { - return $this->parsed; - } - - /** - * @return array - */ - public function getUnparseable() - { - return $this->unparseable; - } -} diff --git a/src/Model/EmailValidation/ParseResponse.php b/src/Model/EmailValidation/ParseResponse.php new file mode 100644 index 0000000..79ad1cd --- /dev/null +++ b/src/Model/EmailValidation/ParseResponse.php @@ -0,0 +1,53 @@ + + */ +final class ParseResponse implements ApiResponse +{ + /** + * @var array + */ + private $parsed; + + /** + * @var array + */ + private $unparseable; + + private function __construct() + { + } + + public static function create(array $data): self + { + $model = new self(); + $model->parsed = (isset($data['parsed']) && is_array($data['parsed'])) ? $data['parsed'] : []; + $model->unparseable = (isset($data['unparseable']) && is_array($data['unparseable'])) ? $data['unparseable'] : []; + + return $model; + } + + public function getParsed(): array + { + return $this->parsed; + } + + public function getUnparseable(): array + { + return $this->unparseable; + } +} diff --git a/src/Model/EmailValidation/Parts.php b/src/Model/EmailValidation/Parts.php index 9e43278..90dfc64 100644 --- a/src/Model/EmailValidation/Parts.php +++ b/src/Model/EmailValidation/Parts.php @@ -30,19 +30,13 @@ final class Parts private $localPart; /** - * Parts constructor. * - * @param string|null $displayName - * @param string|null $domain - * @param string|null $localPart */ - private function __construct($displayName, $domain, $localPart) + private function __construct() { - $this->displayName = $displayName; - $this->domain = $domain; - $this->localPart = $localPart; } + /** * @param array $data * @@ -50,17 +44,18 @@ final class Parts */ public static function create(array $data) { - return new self( - (isset($data['display_name']) ? $data['display_name'] : null), - (isset($data['domain']) ? $data['domain'] : null), - (isset($data['local_part']) ? $data['local_part'] : null) - ); + $model = new self(); + $model->displayName = $data['display_name'] ?? null; + $model->domain = $data['domain'] ?? null; + $model->localPart = $data['local_part'] ?? null; + + return $model; } /** * @return null|string */ - public function getDisplayName() + public function getDisplayName(): ?string { return $this->displayName; } @@ -68,7 +63,7 @@ final class Parts /** * @return null|string */ - public function getDomain() + public function getDomain(): ?string { return $this->domain; } @@ -76,7 +71,7 @@ final class Parts /** * @return null|string */ - public function getLocalPart() + public function getLocalPart(): ?string { return $this->localPart; } diff --git a/src/Model/EmailValidation/Response/ParseResponse.php b/src/Model/EmailValidation/Response/ParseResponse.php deleted file mode 100644 index e786218..0000000 --- a/src/Model/EmailValidation/Response/ParseResponse.php +++ /dev/null @@ -1,68 +0,0 @@ - - */ -final class ParseResponse implements ApiResponse -{ - /** - * @var string - */ - private $message; - - /** - * @var Parse - */ - private $parse; - - /** - * {@inheritdoc} - */ - public static function create(array $data) - { - $message = isset($data['message']) ? $data['message'] : null; - $parse = Parse::create($data); - - return new self($message, $parse); - } - - /** - * ParseResponse Private Constructor. - * - * @param string|null $message - * @param Parse|null $parse - */ - private function __construct($message = null, Parse $parse = null) - { - $this->message = $message; - $this->parse = $parse; - } - - /** - * @return string - */ - public function getMessage() - { - return $this->message; - } - - /** - * @return Parse - */ - public function getParse() - { - return $this->parse; - } -} diff --git a/src/Model/EmailValidation/Response/ValidateResponse.php b/src/Model/EmailValidation/Response/ValidateResponse.php deleted file mode 100644 index f6ca594..0000000 --- a/src/Model/EmailValidation/Response/ValidateResponse.php +++ /dev/null @@ -1,68 +0,0 @@ - - */ -final class ValidateResponse implements ApiResponse -{ - /** - * @var string - */ - private $message; - - /** - * @var EmailValidation - */ - private $emailValidation; - - /** - * {@inheritdoc} - */ - public static function create(array $data) - { - $message = isset($data['message']) ? $data['message'] : null; - $emailValidation = EmailValidation::create($data); - - return new self($message, $emailValidation); - } - - /** - * CreateResponse Private Constructor. - * - * @param string|null $message - * @param EmailValidation|null $emailValidation - */ - private function __construct($message = null, EmailValidation $emailValidation = null) - { - $this->message = $message; - $this->emailValidation = $emailValidation; - } - - /** - * @return string - */ - public function getMessage() - { - return $this->message; - } - - /** - * @return EmailValidation - */ - public function getEmailValidation() - { - return $this->emailValidation; - } -} diff --git a/src/Model/EmailValidation/ValidateResponse.php b/src/Model/EmailValidation/ValidateResponse.php new file mode 100644 index 0000000..445bdcd --- /dev/null +++ b/src/Model/EmailValidation/ValidateResponse.php @@ -0,0 +1,120 @@ + + */ +final class ValidateResponse implements ApiResponse +{ + /** + * @var string|null + */ + private $address; + + /** + * @var string|null + */ + private $didYouMean; + + /** + * @var bool + */ + private $isDisposableAddress; + + /** + * @var bool + */ + private $isRoleAddress; + + /** + * @var bool + */ + private $isValid; + + /** + * @var bool + */ + private $mailboxVerification; + + /** + * @var Parts + */ + private $parts; + + /** + * @var string|null + */ + private $reason; + + private function __construct() + { + } + + public static function create(array $data): self + { + $model = new self(); + $model->address = $data['address'] ?? null; + $model->didYouMean = $data['did_you_mean'] ?? null; + $model->isDisposableAddress = $data['is_disposable_address'] ?? false; + $model->isRoleAddress = $data['is_role_address'] ?? false; + $model->isValid = $data['is_valid'] ?? false; + $model->mailboxVerification = isset($data['mailbox_verification']) ? 'true' === $data['mailbox_verification'] : false; + $model->parts = Parts::create($data['parts'] ?? []); + $model->reason = $data['reason'] ?? null; + + return $model; + } + + public function getAddress(): ?string + { + return $this->address; + } + + public function getDidYouMean(): ?string + { + return $this->didYouMean; + } + + public function isDisposableAddress(): bool + { + return $this->isDisposableAddress; + } + + public function isRoleAddress(): bool + { + return $this->isRoleAddress; + } + + public function isValid(): bool + { + return $this->isValid; + } + + public function isMailboxVerification(): bool + { + return $this->mailboxVerification; + } + + public function getParts(): Parts + { + return $this->parts; + } + + public function getReason(): ?string + { + return $this->reason; + } +} diff --git a/tests/Api/EmailValidationTest.php b/tests/Api/EmailValidationTest.php index 54ea748..7525224 100644 --- a/tests/Api/EmailValidationTest.php +++ b/tests/Api/EmailValidationTest.php @@ -1,5 +1,7 @@ markTestIncomplete('WIP'); - $params = [ 'address' => 'me@davidgarcia.cat', 'mailbox_verification' => true, @@ -43,8 +43,6 @@ class EmailValidationTest extends TestCase public function testParseEmail() { - $this->markTestIncomplete('WIP'); - $params = [ 'addresses' => 'me@davidgarcia.cat', 'syntax_only' => true, diff --git a/tests/Model/EmailValidation/EmailValidationTest.php b/tests/Model/EmailValidation/EmailValidationTest.php deleted file mode 100644 index ec3029c..0000000 --- a/tests/Model/EmailValidation/EmailValidationTest.php +++ /dev/null @@ -1,44 +0,0 @@ -markTestIncomplete('WIP'); - - $data = [ - 'address' => 'foo@mailgun.net', - 'did_you_mean' => null, - 'is_disposable_address' => false, - 'is_role_address' => false, - 'is_valid' => true, - 'mailbox_verification' => null, - 'parts' => ['display_name' => null, 'domain' => 'mailgun.net', 'local_part' => 'foo'], - 'reason' => null, - ]; - - $parts = EmailValidation::create($data); - - $this->assertEquals($data['address'], $parts->getAddress()); - $this->assertEquals($data['did_you_mean'], $parts->getDidYouMean()); - $this->assertEquals($data['is_disposable_address'], $parts->isDisposableAddress()); - $this->assertEquals($data['is_role_address'], $parts->isRoleAddress()); - $this->assertEquals($data['is_valid'], $parts->isValid()); - $this->assertEquals($data['mailbox_verification'], $parts->isMailboxVerification()); - $this->assertInstanceOf(Parts::class, $parts->getParts()); - $this->assertEquals($data['reason'], $parts->getReason()); - } -} diff --git a/tests/Model/EmailValidation/ParseTest.php b/tests/Model/EmailValidation/ParseResponseTest.php similarity index 50% rename from tests/Model/EmailValidation/ParseTest.php rename to tests/Model/EmailValidation/ParseResponseTest.php index 0bd87d6..b3e2b6d 100644 --- a/tests/Model/EmailValidation/ParseTest.php +++ b/tests/Model/EmailValidation/ParseResponseTest.php @@ -1,29 +1,43 @@ ", + "bob@example.com" + ], + "unparseable": [ + ] +} +JSON; + $model = ParseResponse::create(json_decode($json, true)); + $this->assertNotEmpty($model->getParsed()); + $this->assertCount(2, $model->getParsed()); + $this->assertEmpty($model->getUnparseable()); + } + public function testParseConstructorWithValidData() { - $this->markTestIncomplete('WIP'); $data = [ 'parsed' => ['parsed data'], 'unparseable' => ['unparseable data'], ]; - $parts = Parse::create($data); + $parts = ParseResponse::create($data); $this->assertEquals($data['parsed'], $parts->getParsed()); $this->assertEquals($data['unparseable'], $parts->getUnparseable()); @@ -31,16 +45,16 @@ class ParseTest extends BaseModelTest public function testParseConstructorWithInvalidData() { - $this->markTestIncomplete('WIP'); $data = [ 'parsed' => null, 'unparseable' => null, ]; - $parts = Parse::create($data); + $parts = ParseResponse::create($data); $this->assertEquals([], $parts->getParsed()); $this->assertEquals([], $parts->getUnparseable()); } + } diff --git a/tests/Model/EmailValidation/PartsTest.php b/tests/Model/EmailValidation/PartsTest.php index 0e61c11..7df9040 100644 --- a/tests/Model/EmailValidation/PartsTest.php +++ b/tests/Model/EmailValidation/PartsTest.php @@ -16,7 +16,6 @@ class PartsTest extends BaseModelTest { public function testPartsConstructor() { - $this->markTestIncomplete('WIP'); $data = [ 'display_name' => ' Display name', diff --git a/tests/Model/EmailValidation/ValidateResponseTest.php b/tests/Model/EmailValidation/ValidateResponseTest.php new file mode 100644 index 0000000..05fc72c --- /dev/null +++ b/tests/Model/EmailValidation/ValidateResponseTest.php @@ -0,0 +1,85 @@ +assertTrue($model->isMailboxVerification()); + + } + + public function testCreateWithoutMailboxVerification() + { + $json = + <<<'JSON' +{ + "address": "foo@mailgun.net", + "did_you_mean": null, + "is_disposable_address": false, + "is_role_address": false, + "is_valid": true, + "mailbox_verification": null, + "parts": { + "display_name": null, + "domain": "mailgun.net", + "local_part": "foo" + }, + "reason": null +} +JSON; + $model = ValidateResponse::create(json_decode($json, true)); + $this->assertFalse($model->isMailboxVerification()); + + } + + public function testEmailValidation() + { + + $data = [ + 'address' => 'foo@mailgun.net', + 'did_you_mean' => null, + 'is_disposable_address' => false, + 'is_role_address' => false, + 'is_valid' => true, + 'mailbox_verification' => null, + 'parts' => ['display_name' => null, 'domain' => 'mailgun.net', 'local_part' => 'foo'], + 'reason' => null, + ]; + + $parts = ValidateResponse::create($data); + + $this->assertEquals($data['address'], $parts->getAddress()); + $this->assertEquals($data['did_you_mean'], $parts->getDidYouMean()); + $this->assertEquals($data['is_disposable_address'], $parts->isDisposableAddress()); + $this->assertEquals($data['is_role_address'], $parts->isRoleAddress()); + $this->assertEquals($data['is_valid'], $parts->isValid()); + $this->assertEquals($data['mailbox_verification'], $parts->isMailboxVerification()); + $this->assertInstanceOf(Parts::class, $parts->getParts()); + $this->assertEquals($data['reason'], $parts->getReason()); + } +}