/** * Converts an AgaviXmlConfigDomElement into an array. * * @param AgaviXmlConfigDomElement The configuration element to convert. * @param bool Whether this is a top level element. * * @return array The configuration values as an array. * * @author Dominik del Bondio <*****@*****.**> * @author David Zülke <*****@*****.**> * @since 0.11.0 */ protected function convertToArray(AgaviXmlConfigDomElement $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 (!(int) $item->ownerDocument->getXpath()->evaluate(sprintf('count(*[namespace-uri() = "%s"])', $item->ownerDocument->getDefaultNamespaceUri()), $item)) { if ($literalize) { $val = $item->getLiteralValue(); } else { $val = $item->getValue(); } if ($val === null) { $val = ''; } if (!$topLevel && ($numAttribs || $forceArrayValues)) { $data[$valueKey] = $val; } elseif (!$topLevel) { $data = $val; } } else { $names = array(); $children = $item->ownerDocument->getXpath()->query(sprintf('*[namespace-uri() = "%s"]', $item->ownerDocument->getDefaultNamespaceUri()), $item); 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; }
/** * @dataProvider singularPluralTestData */ public function testPluralize($singular, $plural) { $this->assertEquals($plural, AgaviInflector::pluralize($singular)); }
/** * Retrieve singular form of given element name. * This does special splitting only of the last part of the name if the name * of the element contains hyphens, underscores or dots. * * @param string The element name to singularize. * * @return string The singularized element name. * * @author Noah Fontes <*****@*****.**> * @since 1.0.0 */ protected function singularize($name) { // TODO: shouldn't this be static? $names = preg_split('#([_\\-\\.])#', $name, -1, PREG_SPLIT_DELIM_CAPTURE); $names[count($names) - 1] = AgaviInflector::singularize(end($names)); return implode('', $names); }
/** * Register a source with the holder. Must be called in constructors, and * prior to calling the parent ctor. * * @param string The source name, typically passed using a constant. * @param array The variable that will hold the data for the source. * * @author David Zülke <*****@*****.**> * @since 0.11.0 */ protected final function registerSource($name, array &$holder) { $this->sources[$name] =& $holder; $this->sourceNames[$name] = AgaviInflector::singularize($name); }
/** * Checks whether the value has children at all (no params) or whether a * child with the given name exists. * * @param string The name of the child. * * @return bool True if children exist, false if not. * * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ public function hasChildren($child = null) { if ($child === null) { return count($this->_childs) > 0; } if (isset($this->_childs[$child])) { return true; } else { $tagName = $child; $tagNameStart = ''; if (($lastUScore = strrpos($tagName, '_')) !== false) { $lastUScore++; $tagNameStart = substr($tagName, 0, $lastUScore); $tagName = substr($tagName, $lastUScore); } $singularName = $tagNameStart . AgaviInflector::singularize($tagName); return isset($this->_childs[$singularName]); } }