diff options
-rw-r--r-- | src/Utils/Formatter.php | 51 | ||||
-rw-r--r-- | tests/Utils/FormatterTest.php | 6 |
2 files changed, 29 insertions, 28 deletions
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index ff41065..e459b60 100644 --- a/src/Utils/Formatter.php +++ b/src/Utils/Formatter.php @@ -212,44 +212,39 @@ class Formatter private static function mergeFormats(array $formats, array $newFormats) { $added = array(); + $integers = array('flags', 'type'); + $strings = array('html', 'cli', 'function'); + /* Sanitize the array so that we do not have to care later */ + foreach ($newFormats as $j => $new) { + foreach ($integers as $name) { + if (! isset($new[$name])) { + $newFormats[$j][$name] = 0; + } + } + foreach ($strings as $name) { + if (! isset($new[$name])) { + $newFormats[$j][$name] = ''; + } + } + } + + /* Process changes to existing formats */ foreach ($formats as $i => $original) { foreach ($newFormats as $j => $new) { - if (isset($new['type']) - && $new['type'] === $original['type'] - && ( - ( - isset($new['flags']) - && $original['flags'] === $new['flags'] - ) - || ( - !isset($new['flags']) - && $original['flags'] == 0 - ) - ) + if ($new['type'] === $original['type'] + && $original['flags'] === $new['flags'] ) { - $formats[$i] = array( - 'type' => $original['type'], - 'flags' => isset($new['flags']) ? $new['flags'] : 0, - 'html' => isset($new['html']) ? $new['html'] : '', - 'cli' => isset($new['cli']) ? $new['cli'] : '', - 'function' => isset($new['function']) ? $new['function'] : '', - ); - + $formats[$i] = $new; $added[] = $j; } } } + /* Add not already handled formats */ foreach ($newFormats as $j => $new) { - if (!in_array($j, $added) && isset($new['type'])) { - $formats[] = array( - 'type' => $new['type'], - 'flags' => isset($new['flags']) ? $new['flags'] : 0, - 'html' => isset($new['html']) ? $new['html'] : '', - 'cli' => isset($new['cli']) ? $new['cli'] : '', - 'function' => isset($new['function']) ? $new['function'] : '', - ); + if (! in_array($j, $added)) { + $formats[] = $new; } } diff --git a/tests/Utils/FormatterTest.php b/tests/Utils/FormatterTest.php index 1cc3b06..f8a911f 100644 --- a/tests/Utils/FormatterTest.php +++ b/tests/Utils/FormatterTest.php @@ -59,6 +59,9 @@ class FormatTest extends TestCase array( 'type' => 0, 'flags' => 0, + 'html' => '', + 'cli' => '', + 'function' => '', ), ), array( // overriding @@ -69,6 +72,9 @@ class FormatTest extends TestCase array( 'type' => 0, 'flags' => 0, + 'html' => '', + 'cli' => '', + 'function' => '', ), ), ), |