-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathEmail.php
More file actions
122 lines (107 loc) · 2.29 KB
/
Email.php
File metadata and controls
122 lines (107 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace BeaconsBay\QrCode\DataTypes;
use BaconQrCode\Exception\InvalidArgumentException;
class Email implements DataTypeInterface
{
/**
* The prefix of the QrCode.
*
* @var string
*/
protected $prefix = 'mailto:';
/**
* The email address.
*
* @var string
*/
protected $email;
/**
* The subject of the email.
*
* @var string
*/
protected $subject;
/**
* The body of an email.
*
* @var string
*/
protected $body;
/**
* Generates the DataType Object and sets all of its properties.
*
* @param $arguments
*/
public function create(array $arguments)
{
$this->setProperties($arguments);
}
/**
* Returns the correct QrCode format.
*
* @return string
*/
public function __toString()
{
return $this->buildEmailString();
}
/*
* Builds the email string.
*
* @return string
*/
protected function buildEmailString()
{
$email = $this->prefix.$this->email;
if (isset($this->subject) || isset($this->body)) {
$data = [
'subject' => $this->subject,
'body' => $this->body,
];
$email .= '?'.http_build_query($data);
}
return $email;
}
/**
* Sets the objects properties.
*
* @param $arguments
*/
protected function setProperties(array $arguments)
{
if (isset($arguments[0])) {
$this->setEmail($arguments[0]);
}
if (isset($arguments[1])) {
$this->subject = $arguments[1];
}
if (isset($arguments[2])) {
$this->body = $arguments[2];
}
}
/**
* Sets the email property.
*
* @param $email
*/
protected function setEmail($email)
{
if ($this->isValidEmail($email)) {
$this->email = $email;
}
}
/**
* Ensures an email is valid.
*
* @param string $email
*
* @return bool
*/
protected function isValidEmail($email)
{
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email provided');
}
return true;
}
}