Skip to content
This repository was archived by the owner on Mar 22, 2020. It is now read-only.

Commit e72b118

Browse files
committed
Added 'allownull' option to allow formatter to return a null string
1 parent 9a45325 commit e72b118

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/Formatter.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public function format(Address $address, $options = [])
102102
//
103103
// 'abbreviate', if supplied common abbreviations are applied
104104
// to the resulting output.
105+
//
106+
// 'allownull', if the template matched is empty, allow a null
107+
// result rather than returning everything available
105108
public function formatArray($addressArray, $options = [])
106109
{
107110
$countryCode = (isset($options['country'])) ? $options['country'] : $this->determineCountryCode($addressArray);
@@ -155,7 +158,7 @@ public function formatArray($addressArray, $options = [])
155158
}
156159

157160
//Render the template
158-
$text = $this->render($tplText, $addressArray);
161+
$text = $this->render($tplText, $addressArray, $options);
159162

160163
//Post render cleanup
161164
if (isset($tpl['postformat_replace'])) {
@@ -248,40 +251,45 @@ private function postFormatReplace($text, $replacements)
248251
return $text;
249252
}
250253

251-
private function render($tplText, $addressArray)
254+
private function render($tplText, $addressArray, $options)
252255
{
253256
$m = new \Mustache_Engine;
254257

255258
$context = $addressArray;
256-
$context['first'] = function($text) use (&$m, &$addressArray) {
257-
$newText = $m->render($text, $addressArray);
259+
$context['first'] = function($text) use (&$m, &$addressArray, $options) {
260+
$newText = $m->render($text, $addressArray, $options);
258261
$matched = preg_split("/\s*\|\|\s*/", $newText);
259262
$first = current(array_filter($matched));
260263

261264
return $first;
262265
};
263266

264-
$text = $m->render($tplText, $context);
267+
$text = $m->render($tplText, $context, $options);
265268

266269
//Cleanup the output
267270
$text = $this->cleanupRendered($text);
268271

269272
//Make sure we have at least something
270273
if (preg_match('/\w/u', $text) == 0) {
271-
$backupParts = [];
272-
273-
foreach ($addressArray as $key => $val) {
274-
if (strlen($val) > 0) {
275-
$backupParts[] = $val;
274+
if (!isset($options['allownull']) || $options['allownull'] != true) {
275+
$backupParts = [];
276+
277+
foreach ($addressArray as $key => $val) {
278+
if (strlen($val) > 0) {
279+
$backupParts[] = $val;
280+
}
276281
}
282+
283+
$text = implode(', ', $backupParts);
284+
}
285+
else {
286+
$text = ' ';
277287
}
278-
279-
$text = implode(', ', $backupParts);
280-
281-
//Cleanup the output again
282-
$text = $this->cleanupRendered($text);
283288
}
284289

290+
//Cleanup the output again
291+
$text = $this->cleanupRendered($text);
292+
285293
return $text;
286294
}
287295

0 commit comments

Comments
 (0)