Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #612 Add TypeException and include type validations in classes inside mail/ #652

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions examples/helpers/mail/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
// require("./sendgrid-php.php");
// If not using Composer, uncomment the above line

use SendGrid\Mail\To;
use SendGrid\Mail\From;
use SendGrid\Mail\Content;
use SendGrid\Mail\Mail;


function helloEmail()
{
$from = new Email(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library";
$to = new Email(null, "test@example.com");
$content = new Content("text/plain", "some text here");
$mail = new Mail($from, $subject, $to, $content);
$to = new Email(null, "test2@example.com");
$mail->personalization[0]->addTo($to);

//echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
return $mail;
try {
$from = new From(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library";
$to = new To(null, "test@example.com");
$content = new Content("text/plain", "some text here");
$mail = new Mail($from, $to, $subject, $content);

$to = new To(null, "test2@example.com");
$mail->addPersonalization($to);

//echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
return $mail;
} catch (\Exception $e) {
echo $e->getMessage();
}

return null;
}

function kitchenSink()
Expand Down
16 changes: 14 additions & 2 deletions lib/mail/Asm.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ public function __construct(
*
* @param int|GroupId $group_id The unsubscribe group to associate with this
* email
*
*
* @throws TypeException
* @return null
*/
public function setGroupId($group_id)
{
if ($group_id instanceof GroupId) {
$this->group_id = $group_id->getGroupId();
} else {
if (!is_int($group_id)) {
throw new TypeException(
'$group_id must be an instance of SendGrid\Mail\GroupId or of type int.'
);
}
$this->group_id = new GroupId($group_id);
}
return;
Expand Down Expand Up @@ -94,14 +100,20 @@ public function getGroupId()
* to be displayed
* on the unsubscribe
* preferences page.
*
*
* @throws TypeException
* @return null
*/
public function setGroupsToDisplay($groups_to_display)
{
if ($groups_to_display instanceof GroupsToDisplay) {
$this->groups_to_display = $groups_to_display->getGroupsToDisplay();
} else {
if (!is_array($groups_to_display)) {
throw new TypeException(
'$groups_to_display must be an instance of SendGrid\Mail\GroupsToDisplay or of type array.'
);
}
$this->groups_to_display = new GroupsToDisplay($groups_to_display);
}
return;
Expand Down
21 changes: 20 additions & 1 deletion lib/mail/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ public function __construct(
* Add the content to a Attachment object
*
* @param string $content Base64 encoded content
*
*
* @throws TypeException
* @return null
*/
public function setContent($content)
{
if (!is_string($content)) {
throw new TypeException('$content must be of type string.');
}
$this->content = $content;
}

Expand All @@ -95,10 +99,14 @@ public function getContent()
*
* @param string $type Mime type of the attachment
*
* @throws TypeException
* @return null
*/
public function setType($type)
{
if (!is_string($type)) {
throw new TypeException('$type must be of type string.');
}
$this->type = $type;
}

Expand All @@ -117,10 +125,14 @@ public function getType()
*
* @param string $filename File name of the attachment
*
* @throws TypeException
* @return null
*/
public function setFilename($filename)
{
if (!is_string($filename)) {
throw new TypeException('$filename must be of type string');
}
$this->filename = $filename;
}

Expand All @@ -140,10 +152,14 @@ public function getFilename()
* @param string $disposition How the attachment should be displayed:
* inline or attachment, default is attachment
*
* @throws TypeException
* @return null
*/
public function setDisposition($disposition)
{
if (!is_string($disposition)) {
throw new TypeException('$disposition must be of type string.');
}
$this->disposition = $disposition;
}

Expand All @@ -167,6 +183,9 @@ public function getDisposition()
*/
public function setContentID($content_id)
{
if (!is_string($content_id)) {
throw new TypeException('$content_id must be of type string.');
}
$this->content_id = $content_id;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/mail/BatchId.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public function __construct($batch_id=null)
* @param string $batch_id This ID represents a batch of emails to be sent
* at the same time
*
* @throws TypeException
* @return null
*/
public function setBatchId($batch_id)
{
if (!is_string($batch_id)) {
throw new TypeException('$batch_id must be of type string.');
}
$this->batch_id = $batch_id;
}

Expand Down
14 changes: 13 additions & 1 deletion lib/mail/BccSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public function __construct($enable=null, $email=null)
*
* @param bool $enable Indicates if this setting is enabled
*
* @throws TypeException
* @return null
*/
public function setEnable($enable)
{
if (!is_bool($enable)) {
throw new TypeException('$enable must be of type bool.');
}
$this->enable = $enable;
}

Expand All @@ -67,13 +71,21 @@ public function getEnable()
/**
* Add the email setting on a BccSettings object
*
* @param bool $email The email address that you would like
* @param string $email The email address that you would like
* to receive the BCC
*
* @throws TypeException
* @return null
*/
public function setEmail($email)
{
if (!is_string($email) &&
filter_var($email, FILTER_VALIDATE_EMAIL)
) {
throw new TypeException(
'$email must valid and be of type string.'
);
}
$this->email = $email;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/mail/BypassListManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ public function __construct($enable=null)
*
* @param bool $enable Indicates if this setting is enabled
*
* @throws TypeException
* @return null
*/
public function setEnable($enable)
{
if (!is_bool($enable)) {
throw new TypeException('$enable must be of type bool.');
}
$this->enable = $enable;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/mail/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ public function __construct($category=null)
* @param string $category A category name for an email message.
* Each category name may not exceed 255
* characters
*
*
* @throws TypeException
* @return null
*/
public function setCategory($category)
{
if (!is_string($category)) {
throw new TypeException('$category must be of type string.');
}
$this->category = $category;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/mail/ClickTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ public function __construct($enable=null, $enable_text=null)
*
* @param bool $enable Indicates if this setting is enabled
*
* @throws TypeException
* @return null
*/
public function setEnable($enable)
{
if (!is_bool($enable)) {
throw new TypeException('$enable must be of type bool.');
}
$this->enable = $enable;
}

Expand All @@ -71,10 +75,14 @@ public function getEnable()
*
* @param bool $enable_text Indicates if this setting is enabled
*
* @throws TypeException
* @return null
*/
public function setEnableText($enable_text)
{
if (!is_bool($enable_text)) {
throw new TypeException('$enable_text must be of type bool');
}
$this->enable_text = $enable_text;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/mail/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ public function __construct($type=null, $value=null)
* in your email. For example, “text/plain” or
* “text/html”
*
* @throws TypeException
* @return null
*/
public function setType($type)
{
if (!is_string($type)) {
throw new TypeException('$type must be of type string.');
}
$this->type = $type;
}

Expand All @@ -76,10 +80,14 @@ public function getType()
* @param string $value The actual content of the specified mime type
* that you are including in your email
*
* @throws TypeException
* @return null
*/
public function setValue($value)
{
if (!is_string($value)) {
throw new TypeException('$value must be of type string');
}
$this->value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
}

Expand Down
8 changes: 8 additions & 0 deletions lib/mail/CustomArg.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ public function __construct($key=null, $value=null)
*
* @param string $key Custom arg key
*
* @throws TypeException
* @return null
*/
public function setKey($key)
{
if (!is_string($key)) {
throw new TypeException('$key must be of type string');
}
$this->key = (string) $key;
}

Expand All @@ -75,10 +79,14 @@ public function getKey()
*
* @param string $value Custom arg value
*
* @throws TypeException
* @return null
*/
public function setValue($value)
{
if (!is_string($value)) {
throw new TypeException('$value must be of type string.');
}
$this->value = (string) $value;
}

Expand Down
Loading