Exemplo n.º 1
0
 public function testToArray()
 {
     $nested_options_list = new OptionDefinitionList();
     $nested_options_list->addItem(new OptionDefinition(array('value' => 'Nested Foobar One')));
     $list = new OptionDefinitionList();
     $list->addItem(new OptionDefinition(array('name' => 'Parent Foobar', 'value' => $nested_options_list)));
     $expected_list_array = array('Parent Foobar' => array('Nested Foobar One'));
     $this->assertSame($expected_list_array, $list->toArray());
 }
 protected function preRenderOptions(OptionDefinitionList $options, $initial_indent = 0, $indent_size = 4)
 {
     if ($options->getSize() === 0) {
         return '[]';
     }
     $options_code = array('array(');
     $indent_spaces = str_repeat(" ", $initial_indent + $indent_size);
     $next_level_indent = $initial_indent + $indent_size;
     foreach ($options as $option) {
         $option_name = $option->getName();
         $option_value = $option->getValue();
         if ($option_name && $option_value instanceof OptionDefinitionList) {
             $options_code[] = sprintf("%s'%s' => %s,", $indent_spaces, $option_name, $this->preRenderOptions($option_value, $next_level_indent));
         } elseif ($option_value instanceof OptionDefinitionList) {
             $options_code[] = sprintf("%s%s,", $indent_spaces, $this->preRenderOptions($option_value, $next_level_indent));
         } elseif ($option_name) {
             $options_code[] = sprintf("%s'%s' => %s,", $indent_spaces, $option_name, var_export($option_value, true));
         } else {
             $options_code[] = sprintf("%s%s,", $indent_spaces, var_export($option_value, true));
         }
     }
     $options_code[] = sprintf('%s)', str_repeat(" ", $initial_indent));
     return implode(PHP_EOL, $options_code);
 }