Skip to content

Commit

Permalink
made properties private, removed setters, added tostring for form html
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigh committed Oct 21, 2017
1 parent 4e8848d commit dfce02a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/helpers/contacts/recipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// This will build an HTML form to be embedded in your page. This form allows users to subscribe using their name and email.
function buildRecipientForm($url = 'http://www.examle.com/recipientFormSubmit')
{
$form = new RecipientForm($url);
echo $form->html;
$form = (string) new RecipientForm($url);
echo $form . PHP_EOL;
}

// This will accept a form submission from the above form. Will create a new Recipient, adding them to "contactdb". Note, it does not add the recipient to any list.
Expand Down
50 changes: 17 additions & 33 deletions lib/helpers/contacts/Recipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@

class RecipientForm
{
public $html;
private $html;

/**
* Form constructor
*
* @param string $url The url the form should submit to
*/
public function __construct($url)
{
$html = '<form action="' . $url . '" method="post">
First Name: <input type="text" name="first-name"><br>
Last Name: <input type="text" name="last-name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>';
$this->setHtml($html);
}

public function setHtml($html)
{
First Name: <input type="text" name="first-name"><br>
Last Name: <input type="text" name="last-name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>';
$this->html = $html;
}

public function getHtml()
public function __toString()
{
return $this->html;
}
Expand All @@ -44,43 +44,27 @@ public function getHtml()
*/
class Recipient implements \JsonSerializable
{
public $firstName;
public $lastName;
public $email;
public $id;
private $firstName;
private $lastName;
private $email;

public function __construct($firstName, $lastName, $email)
{
$this->setFirstName($firstName);
$this->setLastName($lastName);
$this->setEmail($email);
}

public function setFirstName($firstName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
}

public function getFirstName()
{
return $this->firstName;
}

public function setLastName($lastName)
{
$this->lastName = $lastName;
}

public function getLastName()
{
return $this->lastName;
}

public function setEmail($email)
{
$this->email = $email;
}

public function getEmail()
{
return $this->email;
Expand Down
14 changes: 7 additions & 7 deletions test/unit/helpers/contacts/RecipientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class RecipientsTest_Recipient extends \PHPUnit_Framework_TestCase
{
public function testRecipientsForm()
{
$form = new RecipientForm('http://www.examle.com/recipientFormSubmit');
$form = (string) new RecipientForm('http://www.examle.com/recipientFormSubmit');
$this->assertEquals(
$form->html, '<form action="http://www.examle.com/recipientFormSubmit" method="post">
First Name: <input type="text" name="first-name"><br>
Last Name: <input type="text" name="last-name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>'
$form, '<form action="http://www.examle.com/recipientFormSubmit" method="post">
First Name: <input type="text" name="first-name"><br>
Last Name: <input type="text" name="last-name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>'
);
}
public function testRecipientsFormSubmit()
Expand Down

0 comments on commit dfce02a

Please sign in to comment.