Example #1
0
 /**
  * Add an element
  * @param \Foundation\Form\Element
  */
 public function addElement(Element $element)
 {
     if (array_key_exists($element->getName(), $this->form->getElements())) {
         $message = 'An element with the name ' . $element->getName() . ' already exists in this form';
         throw new \Foundation\Exception($message);
     }
     $this->elements[$element->getName()] = $element;
 }
Example #2
0
 /**
  * @param Element $element
  * @param \DOMNode $node
  */
 protected function build(Element $element, \DOMNode $node)
 {
     $dom = $this->getEngine();
     if ($element->getValue() instanceof Collection) {
         $newElement = $dom->createElement($element->getName());
         foreach ($element->getValue()->getElements() as $child) {
             $this->build($child, $newElement);
         }
     } else {
         $newElement = $dom->createElement($element->getName(), $element->getValue()->getValue());
     }
     foreach ($element->getAttributes()->getValues() as $attr => $value) {
         $newElement->setAttribute($attr, $value);
     }
     $node->appendChild($newElement);
 }
Example #3
0
 /**
  * @covers Xoops\Form\Element::getName
  * @todo   Implement testGetName().
  */
 public function testGetName()
 {
     $name = 'name';
     $this->object->setName($name);
     $value = $this->object->getName();
     $this->assertSame($name, $value);
 }
Example #4
0
 /** Tests for {@link Element::__construct}. */
 public function testConstructorWithNameOnly()
 {
     $e = new Element('html');
     $this->assertSame('html', $e->getName(), 'Element name created wrong.');
     $this->assertTrue(is_array($e->getAttributes()), 'Default Element attributes not created as array.');
     $this->assertSame(0, count($e->getAttributes()), 'Default Element attributes not created as empty array.');
     $this->assertTrue(is_array($e->getChildren()), 'Default Element children not created as array.');
     $this->assertSame(0, count($e->getChildren()), 'Element Element children not created as empty array.');
 }
Example #5
0
 /**
  * 開始タグまたは空要素タグの共通部分を書式化します.
  * @param  Element $element 書式化対象の要素
  * @return string           "<elementName ... "
  */
 protected final function formatTagPrefix(Element $element)
 {
     $tag = "<";
     $tag .= $element->getName();
     foreach ($element->getAttributes() as $name => $value) {
         $tag .= " ";
         $tag .= $value === null ? $this->formatBooleanAttribute($name) : $this->formatAttribute($name, $value);
     }
     return $tag;
 }
Example #6
0
 /** Returns a HTML representation of an element and its child nodes.
  * @param   Element                $element            the element to generate as HTML.
  * @return  string                                     the HTML. */
 public function createHtml($element)
 {
     $data = array_reduce($element->getChildren(), function ($carry, $child) {
         return $carry . $child->toHtml();
     }, '');
     $allAttributes = [];
     foreach ($element->getAttributes() as $name => $value) {
         $allAttributes[] = sprintf(static::ATTRIBUTE_TEMPLATE, htmlspecialchars($name, ENT_COMPAT), htmlspecialchars($value, ENT_COMPAT));
     }
     if (0 === count($allAttributes)) {
         $attributes = '';
     } else {
         $attributes = ' ' . implode(' ', $allAttributes);
     }
     if (in_array($element->getName(), static::VOID_ELEMENTS)) {
         $html = sprintf(static::VOID_ELEMENT_TEMPLATE, $element->getName(), $attributes);
     } else {
         $html = sprintf(static::ELEMENT_TEMPLATE, $element->getName(), $attributes, $data);
     }
     return $html;
 }
 public function startElement(Element $element)
 {
     $this->xmlWriter->startElement($element->getName());
     $attr = $element->getAttr();
     foreach ($attr as $k => $v) {
         $this->xmlWriter->startAttribute($k);
         $this->xmlWriter->text($v);
         $this->xmlWriter->endAttribute();
     }
     if ('' !== $element->getText()) {
         $this->xmlWriter->text($element->getText());
     }
 }
 /**
  * Construct new validation object
  *
  * @internal
  * @param Element $objField
  * @param array $arrValidationRules
  * @param array $arrErrorHandlers
  */
 public function __construct(Element $objField, array $arrValidationRules = array(), array $arrErrorHandlers = array())
 {
     foreach ($arrValidationRules as $key => $value) {
         $property = strtolower("__" . $key);
         if (property_exists($this, $property)) {
             $this->{$property} = $value;
         }
     }
     foreach ($arrErrorHandlers as $key => $value) {
         $property = strtolower("__" . $key . "error");
         if (property_exists($this, $property)) {
             $this->{$property} = $value;
         }
     }
     $this->__field = $objField;
     $this->__type = $objField->getType();
     $this->__fieldname = str_replace("[]", "", $objField->getName());
     $this->__fieldhint = $objField->getHint();
     // Store the default required state in a seperate property.
     // This way, we're able to reset back to default settings at any given time.
     $this->__defaultRequired = $this->__required;
 }
Example #9
0
 /**
  * Generates HTML output for the given field object and its child elements
  *
  * @internal
  * @param Element $objField The Element class-based object to parse
  * @param boolean $hideEmpty Set to true to hide empty field values from the overview. Defaults to false.
  * @param integer $intDynamicCount The dynamic counter for the current Element being parsed
  * @return string Generated HTML
  */
 private function fieldAsHtml($objField, $hideEmpty = false, $intDynamicCount = 0)
 {
     $strReturn = "";
     $strFieldName = $objField->getName();
     $strLabel = $objField->getShortLabel();
     // Passing 'true' gets the short label if available.
     $varValue = $intDynamicCount > 0 ? $objField->getValue($intDynamicCount) : $objField->getValue();
     $strValue = is_array($varValue) ? implode(", ", $varValue) : $varValue;
     if (!empty($strValue) && $hideEmpty || !$hideEmpty && !is_null($strValue)) {
         if (get_class($objField) == "ValidFormBuilder\\Hidden" && $objField->isDynamicCounter()) {
             return $strReturn;
         } else {
             switch ($objField->getType()) {
                 case static::VFORM_BOOLEAN:
                     $strValue = $strValue == 1 ? "yes" : "no";
                     break;
             }
             if (empty($strLabel) && empty($strValue)) {
                 // *** Skip the field.
             } else {
                 $strValue = nl2br($strValue);
                 $strValue = htmlspecialchars($strValue, ENT_QUOTES);
                 $strReturn .= "<tr class=\"vf__field_value\">";
                 $strReturn .= "<td valign=\"top\" style=\"padding-right: 20px\" class=\"vf__field\">" . ($strReturn .= $strLabel . ($strReturn .= "</td>" . ($strReturn .= "<td valign=\"top\" class=\"vf__value\">" . ($strReturn .= "<strong>" . $strValue . "</strong>" . ($strReturn .= "</td>\n")))));
                 $strReturn .= "</tr>";
             }
         }
     }
     return $strReturn;
 }
Example #10
0
 /**
  * Add an element to the array of elements we will be validating. Use the elements name as the array key for access
  * @param  $element
  * @return \fieldset 
  */
 public function addElement($element)
 {
     if (!$element instanceof ElementInterface) {
         if (!is_array($element)) {
             throw new \Exception('Element must be an array or interface ElementInterface');
         }
         $element = new Element($this->formbuilder, $element);
     }
     $this->_elements[$element->getAttribute('key') ?: $element->getName()] = $element;
     return $this;
 }
Example #11
0
 /**
  * Radio element type
  * @param Element $element
  * @param $fieldHtml
  * @throws \Exception
  */
 protected static function radio(Element &$element, &$fieldHtml)
 {
     $name = $element->getName();
     $id = $element->getId();
     $value = $element->getValue();
     if ($element->getType() == self::TYPE_MULTICHECKBOX && !is_array($value)) {
         $value = array();
     }
     $options = $element->getProperties('options');
     if (!is_array($options)) {
         throw new \Exception("Options for field type '{$element->getType()}' must be given as array.");
     }
     //classes
     $class = $element->getProperties('class');
     if (!is_array($class)) {
         $class = array();
     }
     $attributes = $element->getProperties('attributes');
     $extraAttributes = '';
     if (is_array($attributes)) {
         foreach ($attributes as $attr => $val) {
             if (is_string($attr)) {
                 $extraAttributes .= sprintf('%s="%s" ', $attr, $val);
             } else {
                 $extraAttributes .= sprintf('%s ', $val);
             }
         }
     }
     foreach ($options as $option => $lbl) {
         $optionAttributes = $extraAttributes;
         //does this option have its own settings?
         if (is_array($lbl)) {
             $optionSettings = $lbl;
             if (!isset($optionSettings['label'])) {
                 throw new \Exception("A label has not been set for one of the arrayed options for field - " . $element->getName(true));
             }
             $lbl = $optionSettings['label'];
             if (isset($optionSettings['attributes']) && is_array($optionSettings['attributes'])) {
                 foreach ($optionSettings['attributes'] as $attr => $val) {
                     if (is_string($attr)) {
                         $optionAttributes .= sprintf('%s="%s" ', $attr, $val);
                     } else {
                         $optionAttributes .= sprintf('%s ', $val);
                     }
                 }
             }
         }
         $labelHtml = self::$templates['label'];
         $labelHtml = str_replace('{{id}}', "{$id}_{$option}", $labelHtml);
         $labelHtml = str_replace('{{text}}', $lbl, $labelHtml);
         if ($element->getType() == self::TYPE_RADIO) {
             $field = self::$templates[self::TYPE_RADIO];
             $field = str_replace('{{name}}', $name, $field);
             $field = str_replace('{{value}}', 'value="' . $option . '" ' . ($option == $value ? ' checked="checked"' : ''), $field);
         } else {
             if ($element->getType() == self::TYPE_MULTICHECKBOX) {
                 $field = self::$templates[self::TYPE_CHECKBOX];
                 $field = str_replace('{{name}}', "{$name}[]", $field);
                 $field = str_replace('{{value}}', 'value="' . $option . '" ' . (in_array($option, $value) ? ' checked="checked"' : ''), $field);
             }
         }
         $field = str_replace('{{id}}', "{$id}_{$option}", $field);
         $field = str_replace('{{class}}', implode(' ', $class), $field);
         $field = str_replace('{{attributes}}', $optionAttributes, $field);
         $field .= ' ' . $labelHtml;
         $options[$option] = "<div>{$field}</div>";
     }
     $fieldHtml = '<div>' . implode('', $options) . '</div>';
 }
Example #12
0
 public function parseBody(Element $node)
 {
     $nodeName = $node->getName();
     do {
         if ($this->current() != "<" || $nodeName == "script" && !$this->equal("</script>")) {
             $text = "";
             while ($this->current() != "<" || $nodeName == "script" && !$this->equal("</script>")) {
                 $text .= $this->current();
                 $this->next();
             }
             if (trim($text)) {
                 $textNode = new Text($text);
                 $node->addChild($textNode);
             }
         } elseif ($this->current() == "<" && $this->getNext() != "/") {
             $childNode = $this->parseNode();
             $node->addChild($childNode);
         } else {
             $this->next(2);
             $endName = $this->parseName();
             if ($endName != $nodeName) {
                 echo "unexpected end of '{$endName}', {$nodeName} expected at {$this->ccline}:{$this->ccpos}\n";
                 $this->back(strlen("</{$endName}"));
             } else {
                 $this->skipWhiteSpaces();
                 $this->next();
             }
             break;
         }
     } while (true);
 }