@@ -10,24 +10,26 @@ public function __construct(SparkPost $sparkpost)
10
10
}
11
11
12
12
/**
13
- * Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails
13
+ * Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails.
14
14
*
15
15
* @return SparkPostPromise or SparkPostResponse depending on sync or async request
16
16
*/
17
17
public function post ($ payload = [], $ headers = [])
18
18
{
19
19
$ payload = $ this ->formatPayload ($ payload );
20
+
20
21
return parent ::post ($ payload , $ headers );
21
22
}
22
23
23
24
/**
24
- * Runs the given payload through the formatting functions
25
+ * Runs the given payload through the formatting functions.
25
26
*
26
27
* @param array $payload - the request body
27
28
*
28
29
* @return array - the modified request body
29
30
*/
30
- public function formatPayload ($ payload ) {
31
+ public function formatPayload ($ payload )
32
+ {
31
33
$ payload = $ this ->formatBlindCarbonCopy ($ payload ); //Fixes BCCs into payload
32
34
$ payload = $ this ->formatCarbonCopy ($ payload ); //Fixes CCs into payload
33
35
$ payload = $ this ->formatShorthandRecipients ($ payload ); //Fixes shorthand recipients format
@@ -36,35 +38,35 @@ public function formatPayload($payload) {
36
38
}
37
39
38
40
/**
39
- * Formats bcc list into recipients list
41
+ * Formats bcc list into recipients list.
40
42
*
41
43
* @param array $payload - the request body
42
44
*
43
45
* @return array - the modified request body
44
46
*/
45
47
private function formatBlindCarbonCopy ($ payload )
46
48
{
47
-
49
+
48
50
//If there's a list of BCC recipients, move then into the correct format
49
- if (isset ($ payload ['bcc ' ])) {
51
+ if (isset ($ payload ['bcc ' ])) {
50
52
$ payload = $ this ->addListToRecipients ($ payload , 'bcc ' );
51
53
}
52
54
53
55
return $ payload ;
54
56
}
55
57
56
58
/**
57
- * Formats cc list into recipients list and adds the CC header to the content
59
+ * Formats cc list into recipients list and adds the CC header to the content.
58
60
*
59
61
* @param array $payload - the request body
60
62
*
61
63
* @return array - the modified request body
62
64
*/
63
65
private function formatCarbonCopy ($ payload )
64
66
{
65
- if (isset ($ payload ['cc ' ])) {
67
+ if (isset ($ payload ['cc ' ])) {
66
68
$ ccAddresses = [];
67
- for ($ i = 0 ; $ i < count ($ payload ['cc ' ]); $ i ++ ) {
69
+ for ($ i = 0 ; $ i < count ($ payload ['cc ' ]); ++ $ i ) {
68
70
array_push ($ ccAddresses , $ this ->toAddressString ($ payload ['cc ' ][$ i ]['address ' ]));
69
71
}
70
72
@@ -80,26 +82,25 @@ private function formatCarbonCopy($payload)
80
82
}
81
83
82
84
/**
83
- * Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ]
85
+ * Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ].
84
86
*
85
87
* @param array $payload - the request body
86
88
*
87
89
* @return array - the modified request body
88
90
*/
89
91
private function formatShorthandRecipients ($ payload )
90
92
{
91
-
92
93
$ payload ['content ' ]['from ' ] = $ this ->toAddressObject ($ payload ['content ' ]['from ' ]);
93
-
94
- for ($ i = 0 ; $ i < count ($ payload ['recipients ' ]); $ i ++ ) {
94
+
95
+ for ($ i = 0 ; $ i < count ($ payload ['recipients ' ]); ++ $ i ) {
95
96
$ payload ['recipients ' ][$ i ]['address ' ] = $ this ->toAddressObject ($ payload ['recipients ' ][$ i ]['address ' ]);
96
97
}
97
98
98
99
return $ payload ;
99
100
}
100
101
101
102
/**
102
- * Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names
103
+ * Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names.
103
104
*
104
105
* @param array $payload - the request body
105
106
* @param array $listName - the name of the array in the payload to be moved to the recipients list
@@ -112,22 +113,23 @@ private function addListToRecipients($payload, $listName)
112
113
foreach ($ payload [$ listName ] as $ recipient ) {
113
114
$ recipient ['address ' ] = $ this ->toAddressObject ($ recipient ['address ' ]);
114
115
$ recipient ['address ' ]['header_to ' ] = $ originalAddress ;
115
-
116
+
116
117
// remove name from address - name is only put in the header for cc and not at all for bcc
117
- if (isset ($ recipient ['address ' ]['name ' ]))
118
+ if (isset ($ recipient ['address ' ]['name ' ])) {
118
119
unset($ recipient ['address ' ]['name ' ]);
120
+ }
119
121
120
122
array_push ($ payload ['recipients ' ], $ recipient );
121
123
}
122
-
124
+
123
125
//Delete the original object from the payload.
124
126
unset($ payload [$ listName ]);
125
127
126
128
return $ payload ;
127
129
}
128
130
129
131
/**
130
- * Takes the shorthand form of an email address and converts it to the long form
132
+ * Takes the shorthand form of an email address and converts it to the long form.
131
133
*
132
134
* @param $address - the shorthand form of an email address "Name <Email address>"
133
135
*
@@ -141,57 +143,50 @@ private function toAddressObject($address)
141
143
142
144
if ($ this ->isEmail ($ address )) {
143
145
$ return ['email ' ] = $ address ;
144
- }
145
- else if (preg_match ('/"?(.[^"]+)"?\s*<(.+)>/ ' , $ address , $ matches )) {
146
+ } elseif (preg_match ('/"?(.[^"]*)?"?\s*<(.+)>/ ' , $ address , $ matches )) {
146
147
$ name = trim ($ matches [1 ]);
147
148
$ return ['name ' ] = $ matches [1 ];
148
149
$ return ['email ' ] = $ matches [2 ];
149
- }
150
- else {
150
+ } else {
151
151
throw new \Exception ('Invalid address format: ' .$ address );
152
152
}
153
-
154
153
}
155
154
156
155
return $ return ;
157
156
}
158
157
159
158
/**
160
- * Takes the longhand form of an email address and converts it to the shorthand form
159
+ * Takes the longhand form of an email address and converts it to the shorthand form.
161
160
*
162
161
* @param $address - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ]
163
- *
164
162
* @param string - the shorthand form of an email address "Name <Email address>"
165
163
*/
166
164
private function toAddressString ($ address )
167
165
{
168
166
// convert object to string
169
- if (!is_string ($ address )) {
167
+ if (!is_string ($ address )) {
170
168
if (isset ($ address ['name ' ])) {
171
- $ address = '" ' . $ address ['name ' ] . '" < ' . $ address ['email ' ] . '> ' ;
172
- }
173
- else {
174
- $ address = $ address ['email ' ];
169
+ $ address = '" ' .$ address ['name ' ].'" < ' .$ address ['email ' ].'> ' ;
170
+ } else {
171
+ $ address = $ address ['email ' ];
175
172
}
176
173
}
177
174
178
175
return $ address ;
179
176
}
180
177
181
178
/**
182
- * Checks if a string is an email
179
+ * Checks if a string is an email.
183
180
*
184
181
* @param string $email - a string that might be an email address
185
- *
186
- * @param boolean - true if the given string is an email
182
+ * @param bool - true if the given string is an email
187
183
*/
188
- private function isEmail ($ email ){
189
- if (filter_var ($ email , FILTER_VALIDATE_EMAIL )){
184
+ private function isEmail ($ email )
185
+ {
186
+ if (filter_var ($ email , FILTER_VALIDATE_EMAIL )) {
190
187
return true ;
191
188
} else {
192
189
return false ;
193
190
}
194
191
}
195
192
}
196
-
197
- ?>
0 commit comments