@@ -71,7 +71,11 @@ private function getMergedOptions(array $options)
7171 $ options
7272 );
7373
74- $ options ['formats ' ] = self ::mergeFormats ($ this ->getDefaultFormats (), @$ options ['formats ' ] ?: array ());
74+ if (isset ($ options ['formats ' ])) {
75+ $ options ['formats ' ] = self ::mergeFormats ($ this ->getDefaultFormats (), $ options ['formats ' ]);
76+ } else {
77+ $ options ['formats ' ] = $ this ->getDefaultFormats ();
78+ }
7579
7680 if (is_null ($ options ['line_ending ' ])) {
7781 $ options ['line_ending ' ] = $ options ['type ' ] === 'html ' ? '<br/> ' : "\n" ;
@@ -208,44 +212,39 @@ protected function getDefaultFormats()
208212 private static function mergeFormats (array $ formats , array $ newFormats )
209213 {
210214 $ added = array ();
215+ $ integers = array ('flags ' , 'type ' );
216+ $ strings = array ('html ' , 'cli ' , 'function ' );
211217
218+ /* Sanitize the array so that we do not have to care later */
219+ foreach ($ newFormats as $ j => $ new ) {
220+ foreach ($ integers as $ name ) {
221+ if (! isset ($ new [$ name ])) {
222+ $ newFormats [$ j ][$ name ] = 0 ;
223+ }
224+ }
225+ foreach ($ strings as $ name ) {
226+ if (! isset ($ new [$ name ])) {
227+ $ newFormats [$ j ][$ name ] = '' ;
228+ }
229+ }
230+ }
231+
232+ /* Process changes to existing formats */
212233 foreach ($ formats as $ i => $ original ) {
213234 foreach ($ newFormats as $ j => $ new ) {
214- if (isset ($ new ['type ' ])
215- && $ new ['type ' ] === $ original ['type ' ]
216- && (
217- (
218- isset ($ new ['flags ' ])
219- && $ original ['flags ' ] === $ new ['flags ' ]
220- )
221- || (
222- !isset ($ new ['flags ' ])
223- && $ original ['flags ' ] == 0
224- )
225- )
235+ if ($ new ['type ' ] === $ original ['type ' ]
236+ && $ original ['flags ' ] === $ new ['flags ' ]
226237 ) {
227- $ formats [$ i ] = array (
228- 'type ' => $ original ['type ' ],
229- 'flags ' => isset ($ new ['flags ' ]) ? $ new ['flags ' ] : 0 ,
230- 'html ' => isset ($ new ['html ' ]) ? $ new ['html ' ] : '' ,
231- 'cli ' => isset ($ new ['cli ' ]) ? $ new ['cli ' ] : '' ,
232- 'function ' => isset ($ new ['function ' ]) ? $ new ['function ' ] : '' ,
233- );
234-
238+ $ formats [$ i ] = $ new ;
235239 $ added [] = $ j ;
236240 }
237241 }
238242 }
239243
244+ /* Add not already handled formats */
240245 foreach ($ newFormats as $ j => $ new ) {
241- if (!in_array ($ j , $ added ) && isset ($ new ['type ' ])) {
242- $ formats [] = array (
243- 'type ' => $ new ['type ' ],
244- 'flags ' => isset ($ new ['flags ' ]) ? $ new ['flags ' ] : 0 ,
245- 'html ' => isset ($ new ['html ' ]) ? $ new ['html ' ] : '' ,
246- 'cli ' => isset ($ new ['cli ' ]) ? $ new ['cli ' ] : '' ,
247- 'function ' => isset ($ new ['function ' ]) ? $ new ['function ' ] : '' ,
248- );
246+ if (! in_array ($ j , $ added )) {
247+ $ formats [] = $ new ;
249248 }
250249 }
251250
0 commit comments