diff --git a/src/Formatter.php b/src/Formatter.php index 27ba97d..0bce3af 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -102,6 +102,9 @@ public function format(Address $address, $options = []) // // 'abbreviate', if supplied common abbreviations are applied // to the resulting output. + // + // 'allownull', if the template matched is empty, allow a null + // result rather than returning everything available public function formatArray($addressArray, $options = []) { $countryCode = (isset($options['country'])) ? $options['country'] : $this->determineCountryCode($addressArray); @@ -155,7 +158,7 @@ public function formatArray($addressArray, $options = []) } //Render the template - $text = $this->render($tplText, $addressArray); + $text = $this->render($tplText, $addressArray, $options); //Post render cleanup if (isset($tpl['postformat_replace'])) { @@ -248,40 +251,45 @@ private function postFormatReplace($text, $replacements) return $text; } - private function render($tplText, $addressArray) + private function render($tplText, $addressArray, $options) { $m = new \Mustache_Engine; $context = $addressArray; - $context['first'] = function($text) use (&$m, &$addressArray) { - $newText = $m->render($text, $addressArray); + $context['first'] = function($text) use (&$m, &$addressArray, $options) { + $newText = $m->render($text, $addressArray, $options); $matched = preg_split("/\s*\|\|\s*/", $newText); $first = current(array_filter($matched)); return $first; }; - $text = $m->render($tplText, $context); + $text = $m->render($tplText, $context, $options); //Cleanup the output $text = $this->cleanupRendered($text); //Make sure we have at least something if (preg_match('/\w/u', $text) == 0) { - $backupParts = []; - - foreach ($addressArray as $key => $val) { - if (strlen($val) > 0) { - $backupParts[] = $val; + if (!isset($options['allownull']) || $options['allownull'] != true) { + $backupParts = []; + + foreach ($addressArray as $key => $val) { + if (strlen($val) > 0) { + $backupParts[] = $val; + } } + + $text = implode(', ', $backupParts); + } + else { + $text = ' '; } - - $text = implode(', ', $backupParts); - - //Cleanup the output again - $text = $this->cleanupRendered($text); } + //Cleanup the output again + $text = $this->cleanupRendered($text); + return $text; }