/** * add_subsections * Adds the listed subsections to the form section. * If $subsection_name_to_target is provided, * then new subsections are added before or after that subsection, * otherwise to the start or end of the entire subsections array. * * @param EE_Form_Section_Base[] $new_subsections array of new form subsections where keys are their names * @param string $subsection_name_to_target an existing for section that $new_subsections should be added before or after * IF $subsection_name_to_target is null, then $new_subsections will be added to * the beginning or end of the entire subsections array * @param boolean $add_before whether to add $new_subsections, before or after $subsection_name_to_target, * or if $subsection_name_to_target is null, before or after entire subsections array * @return void * @throws \EE_Error */ public function add_subsections($new_subsections, $subsection_name_to_target = NULL, $add_before = true) { foreach ($new_subsections as $subsection_name => $subsection) { if (!$subsection instanceof EE_Form_Section_Base) { EE_Error::add_error(sprintf(__("Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", "event_espresso"), get_class($subsection), $subsection_name, $this->name())); unset($new_subsections[$subsection_name]); } } $this->_subsections = EEH_Array::insert_into_array($this->_subsections, $new_subsections, $subsection_name_to_target, $add_before); /*$subsections_before = array(); if( $subsection_name_to_target ){ foreach( $this->_subsections as $subsection_name => $subsection ) { if ( $add_before && $subsection_name == $subsection_name_to_target ) { break; } $subsections_before[$subsection_name] = $subsection; if ( ! $add_before && $subsection_name == $subsection_name_to_target ) { break; } } $subsections_after = array_diff_key($this->_subsections, $subsections_before); $this->_subsections = array_merge($subsections_before,$new_subsections,$subsections_after); }else{ if( $add_before ) { //add before nothing, meaning nothing should be after it //don't use array_merge because keys might be numeric and we want to preserve their keys foreach( $new_subsections as $key => $subsection ){ $this->_subsections[ $key ] = $subsection; } }else{ //add after nothing, meaning nothing should be before it //again don't use array_merge because we want foreach( $this->_subsections as $key => $subsection ) { $new_subsections[ $key ] = $subsection; } $this->_subsections = $new_subsections; } }*/ if ($this->_construction_finalized) { foreach ($this->_subsections as $name => $subsection) { $subsection->_construct_finalize($this, $name); } } }
/** * test_insert_into_array_with_mixed_indexes */ function test_insert_into_array_with_mixed_indexes() { // starting data $fruits = array(1 => 'one', 'p' => 'potato'); // add to start $fruits = EEH_Array::insert_into_array($fruits, array(2 => 'two')); //echo '$fruits: ', var_dump( $fruits ); $this->assertEquals('two', reset($fruits)); $this->assertEquals('one', next($fruits)); $this->assertEquals('potato', next($fruits)); // add to end $fruits = EEH_Array::insert_into_array($fruits, array('t' => 'tomato'), null, false); //echo '$fruits: ', var_dump( $fruits ); $this->assertEquals('two', reset($fruits)); $this->assertEquals('one', next($fruits)); $this->assertEquals('potato', next($fruits)); $this->assertEquals('tomato', next($fruits)); // add to middle before potato $fruits = EEH_Array::insert_into_array($fruits, array(3 => 'three'), 'p'); //echo '$fruits: ', var_dump( $fruits ); $this->assertEquals('two', reset($fruits)); $this->assertEquals('one', next($fruits)); $this->assertEquals('three', next($fruits)); $this->assertEquals('potato', next($fruits)); $this->assertEquals('tomato', next($fruits)); // try to bork keys and add onto the end while also specifying the last known key $fruits = EEH_Array::insert_into_array($fruits, array(6 => 'tornado'), 't', false); $this->assertEquals('two', reset($fruits)); $this->assertEquals('one', next($fruits)); $this->assertEquals('three', next($fruits)); $this->assertEquals('potato', next($fruits)); $this->assertEquals('tomato', next($fruits)); $this->assertEquals('tornado', next($fruits)); // add to end and reindex keys $fruits = EEH_Array::insert_into_array($fruits, array('g' => 'GO NATO'), null, false, false); $this->assertEquals('two', reset($fruits)); $this->assertEquals('one', next($fruits)); $this->assertEquals('three', next($fruits)); $this->assertEquals('potato', next($fruits)); $this->assertEquals('tomato', next($fruits)); $this->assertEquals('tornado', next($fruits)); $this->assertEquals('GO NATO', next($fruits)); // now test keys reset($fruits); $this->assertEquals(0, key($fruits)); next($fruits); $this->assertEquals(1, key($fruits)); next($fruits); $this->assertEquals(2, key($fruits)); next($fruits); $this->assertEquals('p', key($fruits)); next($fruits); $this->assertEquals('t', key($fruits)); next($fruits); $this->assertEquals(3, key($fruits)); next($fruits); $this->assertEquals(4, key($fruits)); }