Skip to content

Commit 6b28f37

Browse files
committed
Code refactoring
1 parent f2b3f68 commit 6b28f37

22 files changed

Lines changed: 630 additions & 501 deletions

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "justcoded/form2email",
2+
"name": "justcoded/form-handler",
33
"require": {
44
"vlucas/valitron": "^1.4",
55
"phpmailer/phpmailer": "^6.0"
@@ -9,14 +9,14 @@
99
"src/functions.php"
1010
],
1111
"psr-4": {
12-
"justcoded\\form2email\\": "src/"
12+
"JustCoded\\FormHandler\\": "src/"
1313
}
1414
},
1515
"license": "MIT",
1616
"authors": [
1717
{
18-
"name": "kostant1985",
19-
"email": "kostant21@yahoo.com"
18+
"name": "JustCoded",
19+
"email": "hello@justcoded.com"
2020
}
2121
],
2222
"minimum-stability": "dev",

composer.lock

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/form.php

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,54 @@
55
error_reporting(E_ALL);
66

77
// init autoload.
8-
require __DIR__.'/../vendor/autoload.php';
9-
10-
use justcoded\form2email\FormHandler;
11-
use justcoded\form2email\Handler\MailHandler;
12-
use justcoded\form2email\Message\Message;
13-
14-
$validation = [
15-
'rules' => [
16-
'required' => [
17-
'fields' => ['name', 'email'],
18-
'message' => '{field} is required'
19-
],
20-
'email' => [
21-
'fields' => ['email'],
22-
'message' => '{field} is not a valid email address'
23-
]
24-
], // acoording to Valitron doc
25-
'labels' => [
26-
'name' => 'Name',
27-
'email' => 'Email address'
28-
] // acoording to Valitron doc
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
use JustCoded\FormHandler\FormHandler;
11+
use JustCoded\FormHandler\Handlers\MailHandler;
12+
use JustCoded\FormHandler\DataObjects\MailMessage;
13+
14+
$validation = [
15+
'rules' => [
16+
'required' => [
17+
'fields' => ['name', 'email', 'subject', 'message'],
18+
'message' => '{field} is required'
19+
],
20+
'email' => [
21+
'fields' => ['email'],
22+
'message' => '{field} is not a valid email address'
23+
]
24+
], // acoording to Valitron doc.
25+
'labels' => [
26+
'name' => 'Name',
27+
'email' => 'Email address'
28+
] // acoording to Valitron doc.
2929
];
3030

3131
$mailerConfig = [
32-
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
33-
'host' => 'smtp.gmail.com',
34-
'user' => 'kos1985.dev@gmail.com',
35-
'password' => 'kos409834',
32+
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
33+
'host' => 'smtp.gmail.com',
34+
'user' => 'kos1985.dev@gmail.com',
35+
'password' => 'kos409834',
36+
'protocol' => 'tls',
37+
'port' => 587,
3638
];
3739

3840
$message = [
39-
'from' => ['kostant21@yahoo.com', 'kosFrom'],
40-
'to' => ['kostant21@yahoo.com', 'kosTo'], // can use tokens from input
41-
'cc' => ['kostant21@yahoo.com', 'kosCc'],
42-
'bcc' => ['kostant21@yahoo.com', 'kosBcc'],
43-
'subject' => 'Contact form', // can use tokens from input
44-
'body' => 'template.php', // include of external file template.php, can use tokens from input
45-
'altBody' => '...',
41+
'from' => ['kostant21@yahoo.com' => 'kosFrom'],
42+
'to' => ['alexp.test1@gmail.com' => 'Alex'],
43+
// 'cc' => ['email' => 'name'],
44+
// 'bcc' => ['email' => 'name'],
45+
'subject' => 'Contact request from {name}',
46+
'bodyTemplate' => __DIR__ . '/template-html.php',
47+
'altBodyTemplate' => __DIR__ . '/template-plain.php',
4648
];
4749

4850

49-
$mailerHandler = new MailHandler($mailerConfig);
50-
51-
$formHandler = new FormHandler($validation, $mailerHandler);
51+
$mailer = new MailHandler($mailerConfig, new MailMessage($message));
52+
$formHandler = new FormHandler($validation, $mailer);
5253

5354
if ($formHandler->validate($_POST)) {
54-
$formHandler->process(new Message($message));
55+
$formHandler->process();
5556
}
5657

5758
echo json_encode($formHandler->response());

examples/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<form action="form.php" method="post">
55
Name: <input type="text" name="name"><br>
66
E-mail: <input type="text" name="email"><br>
7+
Subject: <input type="text" name="subject"><br>
8+
Message: <textarea name="message"></textarea>
79
<input type="submit">
810
</form>
911

examples/template-html.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/* @var array $data */
3+
?>
4+
5+
<html>
6+
<body>
7+
<p><b>Name:</b> {name}</p>
8+
<p><b>Email:</b> {email}</p>
9+
<p><b>Subject:</b> {subject}</p>
10+
<p><b>Message:</b><br>
11+
{message}</p>
12+
</body>
13+
</html>

examples/template-plain.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/* @var array $data */
3+
?>
4+
Name: {name}
5+
Email: {email}
6+
Subject: {subject}
7+
Message:
8+
9+
{message}

examples/template.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/DataObject/DataObject.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/DataObjects/DataObject.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace JustCoded\FormHandler\DataObjects;
4+
5+
abstract class DataObject
6+
{
7+
public function __construct(array $config)
8+
{
9+
foreach ($config as $key => $value) {
10+
if (property_exists($this, $key)) {
11+
$this->$key = $value;
12+
} else {
13+
throw new \Exception('Property "' . $key . '" doesn\'t exists in ' . get_class($this));
14+
}
15+
}
16+
}
17+
}

src/DataObjects/EmailAddress.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace JustCoded\FormHandler\DataObjects;
3+
4+
class EmailAddress
5+
{
6+
/**
7+
* @var string
8+
*/
9+
protected $email;
10+
11+
/**
12+
* @var string
13+
*/
14+
protected $name;
15+
16+
public function __construct($data)
17+
{
18+
if (empty($data)) {
19+
throw new \Exception('Email Address can\'t be blank');
20+
}
21+
22+
if (! is_array($data)) {
23+
$data = [$data];
24+
}
25+
26+
reset($data);
27+
if (is_numeric(key($data))) {
28+
$this->name = '';
29+
$this->email = (string) current($data);
30+
} else {
31+
$this->email = (string) key($data);
32+
$this->name = (string) current($data);
33+
}
34+
35+
$this->name = str_replace('"', '', trim($this->name));
36+
$this->email = trim($this->email);
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getEmail()
43+
{
44+
return $this->email;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getName()
51+
{
52+
return $this->name;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getFormattedAddress()
59+
{
60+
if ($this->name) {
61+
return "\"{$this->name}\" <{$this->email}>";
62+
} else {
63+
return "<{$this->email}>";
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)