예제 #1
0
 public function testSetGetValue()
 {
     $vh = new AgaviConfigValueHolder();
     $vh->setValue('value');
     $this->assertSame('value', $vh->getValue());
     $vh->setValue('newvalue');
     $this->assertSame('newvalue', $vh->getValue());
 }
 /**
  * Converts an AgaviConfigValueHolder into an array.
  *
  * @param      AgaviConfigValueHolder The config value to convert.
  *
  * @return     array The config values as an array.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 protected function convertToArray(AgaviConfigValueHolder $item, $topLevel = false)
 {
     $idAttribute = $this->getParameter('id_attribute', 'name');
     $valueKey = $this->getParameter('value_key', 'value');
     $forceArrayValues = $this->getParameter('force_array_values', false);
     $attributePrefix = $this->getParameter('attribute_prefix', '');
     $literalize = $this->getParameter('literalize', true);
     $singularParentName = AgaviInflector::singularize($item->getName());
     $data = array();
     $attribs = $item->getAttributes();
     $numAttribs = count($attribs);
     if ($idAttribute && $item->hasAttribute($idAttribute)) {
         $numAttribs--;
     }
     foreach ($item->getAttributes() as $name => $value) {
         if ($topLevel && in_array($name, array('context', 'environment')) || $name == $idAttribute) {
             continue;
         }
         if ($literalize) {
             $value = AgaviToolkit::literalize($value);
         }
         if (!isset($data[$name])) {
             $data[$attributePrefix . $name] = $value;
         }
     }
     if (!$item->hasChildren()) {
         $val = $item->getValue();
         if ($literalize) {
             $val = AgaviToolkit::literalize($val);
         }
         if ($val === null) {
             $val = '';
         }
         if (!$topLevel && ($numAttribs || $forceArrayValues)) {
             $data[$valueKey] = $val;
         } else {
             $data = $val;
         }
     } else {
         $names = array();
         $children = $item->getChildren();
         foreach ($children as $child) {
             $names[] = $child->getName();
         }
         $dupes = array();
         foreach (array_unique(array_diff_assoc($names, array_unique($names))) as $name) {
             $dupes[] = $name;
         }
         foreach ($children as $key => $child) {
             $hasId = $idAttribute && $child->hasAttribute($idAttribute);
             $isDupe = in_array($child->getName(), $dupes);
             $hasParent = $child->getName() == $singularParentName && $item->getName() != $singularParentName;
             if (($hasId || $isDupe) && !$hasParent) {
                 // it's one of multiple tags in this level without the respective plural form as the parent node
                 if (!isset($data[$idx = AgaviInflector::pluralize($child->getName())])) {
                     $data[$idx] = array();
                 }
                 $hasParent = true;
                 $to =& $data[$idx];
             } else {
                 $to =& $data;
             }
             if ($hasId) {
                 $key = $child->getAttribute($idAttribute);
                 if ($literalize) {
                     // no literalize, just constants!
                     $key = AgaviToolkit::expandDirectives($key);
                 }
                 $to[$key] = $this->convertToArray($child);
             } elseif ($hasParent) {
                 $to[] = $this->convertToArray($child);
             } else {
                 $to[$child->getName()] = $this->convertToArray($child);
             }
         }
     }
     return $data;
 }