PHP Zend_Form对象到XML数据表示(用于XSLT)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Zend_Form对象到XML数据表示(用于XSLT)相关的知识,希望对你有一定的参考价值。

class XML_Form extends XML_Helper {
	/**
	 * 
	 * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend
	 * @param Zend_Form $object
	 * @param array $data to validate form against
	 * @return DOMDocument
	 */
	public static function formToDOM(Zend_Form $object=null, $data=null) {
		if (is_a($object, "Zend_Form")) {
			if (!is_null($data)) {
				if ($object->isValid($data)) $state = "valid";
				else $state = "fail";
			} else {
				$state = "init";
			}

			$doc = new DOMDocument('1.0', 'UTF-8');
			$doc->loadXML('<item type="form" />');
			$root = $doc->documentElement;

			// 1st level item 
			$item = $doc->getElementsByTagName('item')->item(0);
			$item->setAttribute('id',$object->getAttrib('id'));
			$item->setAttribute('state',$state);
			
			// 2nd level fragment form properties
			$options = $object->getAttribs();
			$options['enctype'] = $object->getEnctype();
			foreach ($options as $attribute => $value) {
				$fragment = $doc->createElement('fragment',$value);
				$fragment->setAttribute('id',$attribute);
				$item->appendChild($fragment);
			}
	
			// 3rd level group for form elements
			$group_fields = $doc->createElement('group');
			$group_fields->setAttribute('id','fields');
			foreach ($object->getElements() as $element) {
				/** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST
				  * @todo test redirect functionality, retrieve redirect value in 
				  **/
				
				$class = strtolower(str_replace('Zend_Form_Element_','',get_class($element)));
				if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) {
					$lang = Royal_Language::getLanguage();
					$region = Royal_FrontendRegion::getRegion();
					$element->setValue("/$lang/$region/memberhome/");
				}
				
				// 3rd level form elements
				$item_element = $doc->createElement('item');
				$item_element->setAttribute('id',$element->getName());
				$item_element->setAttribute('type', $class);
				
				//4th level fragments of element
				parent::createFragment('name', $item_element, $element->getName());
				parent::createFragment('label', $item_element, $element->getLabel());
				parent::createFragment('value', $item_element, $element->getValue());
				parent::createFragment('required', $item_element, $element->isRequired());
				parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength'));
				parent::createFragment('size', $item_element, $element->getAttrib('size'));
				parent::createFragment('readonly', $item_element, $element->getAttrib('readonly'));
				parent::createFragment('description', $item_element, $element->getDescription());
				if (!is_null($data) && array_key_exists($element->getName(), $data)) {
					foreach ($element->getErrors() as $error) {
						parent::createFragment('error', $item_element, $error);
					}
				}
				// possible errors
				if ($element->isValid($data[$element->getName()])===true) {
					$valid = 'true';
				} else {
					$valid = 'false';
				}
				parent::createFragment('valid', $item_element, $valid);
	
				if (method_exists($element, 'getMultiOptions')) {
					$group_options = parent::createGroup('options', $item_element);
					foreach ($element->getMultiOptions() as $key => $text) {
						$item_option = $doc->createElement('item');
						$item_option->setAttribute('id', 'option');
						parent::createFragment('text', $item_option, $text);
						parent::createFragment('value', $item_option, $key);
						$group_options->appendChild($item_option);
					}
				}
				
				$group_fields->appendChild($item_element);
				$item->appendChild($group_fields);
			}
			return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument
		} else {
			throw new Exception("Parameter <samp>\$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form");
		}
	}
}

class XML_Helper {
	/**
	 * 
	 * __constructor
	 */
	public function Royal_XML_Creator() {
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'>value</fragment> to a given origin and returns the node for further use
	 * @param string $id
	 * @param DOMNode $origin
	 * @param string $value
	 * @return DOMNode
	 */
	public static function createFragment($id, DOMNode $appendTo, $value='') {
		/** @todo maybe encode the value in order to make sure the XML stays valid (non-UTF-8 characters, HTML, etc.) **/
		$fragment = $appendTo->ownerDocument->createElement('fragment', $value);
		$fragment->setAttribute('id', (string) $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'><![CDATA[value]]></fragment> to a given DOM
	 * @param unknown_type $id
	 * @param DOMNode $origin
	 * @param unknown_type $value
	 */
	public static function createCDATAFragment($id, DOMNode $appendTo, $value='') {
		$fragment = $appendTo->ownerDocument->createElement('fragment');
		$fragment->appendChild($appendTo->ownerDocument->createCDATASection($value));
		$fragment->setAttribute('id', $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a group <group id='id' /> to a given origin
	 * @param string $id
	 * @param DOMNode $origin
	 * @return DOMNode
	 */
	public static function createGroup($id, DOMNode $appendTo) {
		$group = $appendTo->ownerDocument->createElement('group');
		$group->setAttribute('id', $id);
		$appendTo->appendChild($group);
		return $group;
	}
}

/**
Example Usage:
**/
$proc = new XSLTProcessor(); 
$xsldoc = new DOMDocument(); 
$xsldoc->load(APPLICATION_PATH.'/xsl/forms.xsl'); 
$proc->importStyleSheet($xsldoc); 
echo $proc->transformToXML(new XML_Form::formToDOM(new My_Custom_Zend_Form()));

以上是关于PHP Zend_Form对象到XML数据表示(用于XSLT)的主要内容,如果未能解决你的问题,请参考以下文章

Zend_Form 创建校验和解析表单的基础--(手冊)

PHP 在Zend_Form中显示错误

PHP 自定义Zend_Form:更改了装饰器和添加的方法

Zendèu表单的XML表示示例

Zend 框架表单与 jquery

如何使用装饰器将多个 div 或字段集添加到 zend_form?