@@ -10,24 +10,26 @@ public function __construct(SparkPost $sparkpost)
1010 }
1111
1212 /**
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.
1414 *
1515 * @return SparkPostPromise or SparkPostResponse depending on sync or async request
1616 */
1717 public function post ($ payload = [], $ headers = [])
1818 {
1919 $ payload = $ this ->formatPayload ($ payload );
20+
2021 return parent ::post ($ payload , $ headers );
2122 }
2223
2324 /**
24- * Runs the given payload through the formatting functions
25+ * Runs the given payload through the formatting functions.
2526 *
2627 * @param array $payload - the request body
2728 *
2829 * @return array - the modified request body
2930 */
30- public function formatPayload ($ payload ) {
31+ public function formatPayload ($ payload )
32+ {
3133 $ payload = $ this ->formatBlindCarbonCopy ($ payload ); //Fixes BCCs into payload
3234 $ payload = $ this ->formatCarbonCopy ($ payload ); //Fixes CCs into payload
3335 $ payload = $ this ->formatShorthandRecipients ($ payload ); //Fixes shorthand recipients format
@@ -36,35 +38,35 @@ public function formatPayload($payload) {
3638 }
3739
3840 /**
39- * Formats bcc list into recipients list
41+ * Formats bcc list into recipients list.
4042 *
4143 * @param array $payload - the request body
4244 *
4345 * @return array - the modified request body
4446 */
4547 private function formatBlindCarbonCopy ($ payload )
4648 {
47-
49+
4850 //If there's a list of BCC recipients, move then into the correct format
49- if (isset ($ payload ['bcc ' ])) {
51+ if (isset ($ payload ['bcc ' ])) {
5052 $ payload = $ this ->addListToRecipients ($ payload , 'bcc ' );
5153 }
5254
5355 return $ payload ;
5456 }
5557
5658 /**
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.
5860 *
5961 * @param array $payload - the request body
6062 *
6163 * @return array - the modified request body
6264 */
6365 private function formatCarbonCopy ($ payload )
6466 {
65- if (isset ($ payload ['cc ' ])) {
67+ if (isset ($ payload ['cc ' ])) {
6668 $ ccAddresses = [];
67- for ($ i = 0 ; $ i < count ($ payload ['cc ' ]); $ i ++ ) {
69+ for ($ i = 0 ; $ i < count ($ payload ['cc ' ]); ++ $ i ) {
6870 array_push ($ ccAddresses , $ this ->toAddressString ($ payload ['cc ' ][$ i ]['address ' ]));
6971 }
7072
@@ -80,26 +82,25 @@ private function formatCarbonCopy($payload)
8082 }
8183
8284 /**
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" ].
8486 *
8587 * @param array $payload - the request body
8688 *
8789 * @return array - the modified request body
8890 */
8991 private function formatShorthandRecipients ($ payload )
9092 {
91-
9293 $ 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 ) {
9596 $ payload ['recipients ' ][$ i ]['address ' ] = $ this ->toAddressObject ($ payload ['recipients ' ][$ i ]['address ' ]);
9697 }
9798
9899 return $ payload ;
99100 }
100101
101102 /**
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.
103104 *
104105 * @param array $payload - the request body
105106 * @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)
112113 foreach ($ payload [$ listName ] as $ recipient ) {
113114 $ recipient ['address ' ] = $ this ->toAddressObject ($ recipient ['address ' ]);
114115 $ recipient ['address ' ]['header_to ' ] = $ originalAddress ;
115-
116+
116117 // 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 ' ])) {
118119 unset($ recipient ['address ' ]['name ' ]);
120+ }
119121
120122 array_push ($ payload ['recipients ' ], $ recipient );
121123 }
122-
124+
123125 //Delete the original object from the payload.
124126 unset($ payload [$ listName ]);
125127
126128 return $ payload ;
127129 }
128130
129131 /**
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.
131133 *
132134 * @param $address - the shorthand form of an email address "Name <Email address>"
133135 *
@@ -141,57 +143,50 @@ private function toAddressObject($address)
141143
142144 if ($ this ->isEmail ($ address )) {
143145 $ return ['email ' ] = $ address ;
144- }
145- else if (preg_match ('/"?(.[^"]+)"?\s*<(.+)>/ ' , $ address , $ matches )) {
146+ } elseif (preg_match ('/"?(.[^"]*)?"?\s*<(.+)>/ ' , $ address , $ matches )) {
146147 $ name = trim ($ matches [1 ]);
147148 $ return ['name ' ] = $ matches [1 ];
148149 $ return ['email ' ] = $ matches [2 ];
149- }
150- else {
150+ } else {
151151 throw new \Exception ('Invalid address format: ' .$ address );
152152 }
153-
154153 }
155154
156155 return $ return ;
157156 }
158157
159158 /**
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.
161160 *
162161 * @param $address - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ]
163- *
164162 * @param string - the shorthand form of an email address "Name <Email address>"
165163 */
166164 private function toAddressString ($ address )
167165 {
168166 // convert object to string
169- if (!is_string ($ address )) {
167+ if (!is_string ($ address )) {
170168 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 ' ];
175172 }
176173 }
177174
178175 return $ address ;
179176 }
180177
181178 /**
182- * Checks if a string is an email
179+ * Checks if a string is an email.
183180 *
184181 * @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
187183 */
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 )) {
190187 return true ;
191188 } else {
192189 return false ;
193190 }
194191 }
195192}
196-
197- ?>
0 commit comments