/** * createByXml * * @param \SimpleXmlElement $xml * @param \SplPriorityQueue $namespaces * * @return AbstractField */ public static function createByXml(\SimpleXmlElement $xml, \SplPriorityQueue $namespaces = null) { $type = XmlHelper::get($xml, 'type', 'text'); if (class_exists($type)) { $class = $type; } else { $class = static::findFieldClass($type, $namespaces); } if (!class_exists($class)) { // Fallback to TextField $class = static::$defaultNamespace . '\\TextField'; } return new $class($xml); }
/** * Method to test def(). * * @return void * * @covers Windwalker\Dom\SimpleXml\XmlHelper::def */ public function testDef() { XmlHelper::def($this->xml, 'flower', 'rose'); XmlHelper::def($this->xml, 'name', 'Play'); $this->assertEquals('rose', XmlHelper::get($this->xml, 'flower')); $this->assertEquals('List', XmlHelper::get($this->xml, 'name')); }
/** * handleXml * * @param \SimpleXMLElement $xml * * @return void */ protected function handleXml(\SimpleXMLElement $xml) { $this->name = XmlHelper::get($xml, 'name'); $this->label = XmlHelper::get($xml, 'label'); $this->attributes = XmlHelper::getAttributes($xml); $form = $xml; $group = array(); while ($parent = $form->xpath('..')) { $parent = $parent[0]; $name = $parent->getName(); if ($name == 'fieldset') { $this->fieldset = $this->fieldset ?: (string) $parent['name']; } elseif ($name == 'group') { array_unshift($group, (string) $parent['name']); } $form = $parent; } $this->group = implode('.', $group); }
/** * prepareOptions * * @param string|\SimpleXMLElement $xml * @param Option[] $options * * @throws \InvalidArgumentException * @return void */ protected function handleOptions($xml, $options = array()) { if ($xml instanceof \SimpleXMLElement) { foreach ($xml->children() as $name => $option) { if ($option->getName() == 'optgroup') { foreach ($option->children() as $opt) { $attributes = XmlHelper::getAttributes($opt); $opt = new Option((string) $opt, XmlHelper::getAttribute($opt, 'value'), $attributes); $this->options[XmlHelper::getAttribute($option, 'label')][] = $opt; } } else { $attributes = XmlHelper::getAttributes($option); $option = new Option((string) $option, XmlHelper::getAttribute($option, 'value'), $attributes); $this->options[] = $option; } } } else { foreach ($options as $name => $option) { // If is array, means it is group if (is_array($option)) { foreach ($option as $opt) { if (!$opt instanceof Option) { throw new \InvalidArgumentException(sprintf('Please give me %s class as option, %s given.', 'Windwalker\\Html\\Option', get_class($opt))); } } } else { if (!$option instanceof Option) { throw new \InvalidArgumentException(sprintf('Please give me %s class as option, %s given.', 'Windwalker\\Html\\Option', get_class($option))); } } if (is_numeric($name)) { $this->options[] = $option; } else { $this->options[$name] = $option; } } } }