/**
  * Tests setting values.
  */
 public function testSet()
 {
     $optionCategory = new OptionCategory();
     // set category name
     $optionCategory->setName('hansolo');
     $this->assertEquals('hansolo', $optionCategory->getName());
     $optionCategory->setName('');
     $this->assertTrue(empty($optionCategory->getName()));
     $optionCategory->setName('123');
     $this->assertTrue('123' === $optionCategory->getName());
     // set options
     $options = [new Option(), new Option(0, 'testV', 'string', 'elephant'), new Option(0, 'samba', 'array', [1, 2, 3])];
     $optionCategory->setOptions($options);
     $this->assertEquals($options, $optionCategory->getOptions());
     // set categories
     $categories = [new OptionCategory('testv1'), new OptionCategory('testv0')];
     $optionCategory->setCategories($categories);
     $this->assertEquals($categories, $optionCategory->getCategories());
 }
Esempio n. 2
0
 /**
  * Tests the convertToArray method.
  */
 public function testConvertToArray()
 {
     // simple case
     $optionCategory = new OptionCategory();
     $mainCategory = new OptionCategory('twomartens.core');
     $options = [new Option(0, 'one', 'string', 'hello'), new Option(0, 'two', 'boolean', false), new Option(0, 'three', 'array', [1, 2, 3])];
     $mainCategory->setOptions($options);
     $optionCategory->setCategories([$mainCategory]);
     $yamlData = ConfigUtil::convertToArray($optionCategory);
     $expectedYamlData = ['twomartens.core' => ['one' => 'hello', 'two' => false, 'three' => [1, 2, 3]]];
     $this->assertEquals($expectedYamlData, $yamlData);
     // complex case
     $optionCategory = new OptionCategory();
     $acpCategory = new OptionCategory('acp');
     $ourCategory = new OptionCategory('twomartens.core');
     $acpCategory->setCategories([$ourCategory]);
     $optionCategory->setCategories([$acpCategory]);
     $options = [new Option(0, 'one', 'boolean', true), new Option(0, 'two', 'string', 'foo'), new Option(0, 'three', 'array', [4, 5, 6])];
     $ourCategory->setOptions($options);
     $yamlData = ConfigUtil::convertToArray($optionCategory);
     $expectedYamlData = ['acp' => ['twomartens.core' => ['one' => true, 'two' => 'foo', 'three' => [4, 5, 6]]]];
     $this->assertEquals($expectedYamlData, $yamlData);
 }
Esempio n. 3
0
 /**
  * Converts the given values into options.
  *
  * @param string      $key
  * @param array|null  $values
  *
  * @return OptionCategory
  */
 private static function convertValues($key, $values)
 {
     $returnCategory = new OptionCategory();
     if ($values === null) {
         return $returnCategory;
     }
     if (self::isAssoc($values)) {
         $options = [];
         $categories = [];
         foreach ($values as $_key => $value) {
             // case: value = [...]
             if (is_array($value)) {
                 // recursive call
                 $subCategory = self::convertValues($_key, $value);
                 $subOptions = $subCategory->getOptions();
                 $subCategories = $subCategory->getCategories();
                 // case: value = [a, b, c]
                 if (count($subOptions) == 1 && empty($subCategories) && !self::isAssoc($value)) {
                     $options[] = $subOptions[0];
                     // case: value = [[a: b], [c: d, e: f]] || [a: b, c: d, e: [...]]
                 } else {
                     $category = new OptionCategory($_key);
                     $category->setOptions($subOptions);
                     $category->setCategories($subCategories);
                     $categories[] = $category;
                 }
                 // case: value = b
             } else {
                 $type = str_replace('double', 'float', gettype($value));
                 $type = str_replace('NULL', 'null', $type);
                 $options[] = new Option(0, $_key, $type, $value);
             }
         }
         $returnCategory->setOptions($options);
         $returnCategory->setCategories($categories);
     } else {
         $options = [];
         $_values = [];
         foreach ($values as $_key => $value) {
             if (is_array($value)) {
                 $subCategory = self::convertValues($_key, $value);
                 $subOptions = $subCategory->getOptions();
                 $subCategories = $subCategory->getCategories();
                 // case: value = [a: b]
                 if (count($subOptions) == 1 && empty($subCategories)) {
                     $option = $subOptions[0];
                     $options[] = $option;
                     // case: value = [a: b, d: e]
                 } elseif (count($subOptions) > 1) {
                     $option = new Option(0, '', 'array');
                     $optionValues = [];
                     foreach ($subOptions as $subOption) {
                         $optionValues[$subOption->getName()] = $subOption->getValue();
                     }
                     $option->setValue($optionValues);
                     $options[] = $option;
                 }
             } else {
                 $_values[] = $value;
             }
         }
         // case: values = [a, b, c, d]
         if (!empty($_values)) {
             $options[] = new Option(0, $key, 'array', $_values);
         }
         $returnCategory->setOptions($options);
     }
     return $returnCategory;
 }