Location: PHPKode > projects > DIY Blog > diy-blog/classes/diy-blog/view/XMLSerializer.class.php
<?php

/**
 * Serializes Model objects into XML strings.
 * @package diy-blog.view
 * @author	Martynas Jusevicius <hide@address.com>
 * @link	http://www.xml.lt
 */

class XMLSerializer extends Serializer
{

/**
 * Serializes objects into XML strings.
 * @param mixed $page Object to serialize
 * @return string XML string
 */

	public static function serialize($mixed)
	{
		$xml = parent::serialize($mixed);

		if (is_array($mixed)) $xml = self::serializeArray($mixed);		
		
		if ($mixed instanceof Error) $xml = self::serializeError($mixed);
		if ($mixed instanceof User) $xml = self::serializeUser($mixed);
		if ($mixed instanceof PageForm) $xml = self::serializePageForm($mixed);
		if ($mixed instanceof CreateUserForm) $xml = self::serializeCreateUserForm($mixed);
		if ($mixed instanceof EditUserForm) $xml = self::serializeEditUserForm($mixed);
		if ($mixed instanceof CommentForm) $xml = self::serializeCommentForm($mixed);
		if ($mixed instanceof SearchForm) $xml = self::serializeSearchForm($mixed);
		if ($mixed instanceof Page)
		{
			if ($mixed instanceof PostPage) $xml = self::serializePostPage($mixed);
			else $xml = self::serializePage($mixed);
		}
		if ($mixed instanceof Comment) $xml = self::serializeComment($mixed);
		if ($mixed instanceof File) $xml = self::serializeFile($mixed);

		return $xml;
	}

	private static function serializeArray(Array $array)
	{
		$xml = "<Array>";
		foreach ($array as $item) $xml .= self::serialize($item);
		$xml .= "</Array>";

		return $xml;
	}

	private static function serializePageForm(PageForm $form)
	{
		$xml = "<".get_class($form).">";
		if ($form->getURL() != null) $xml .= "<URL>".$form->getURL()."</URL>";
		if ($form->isURLEntered()) $xml .= "<URLEntered/>";
		$xml .= "<Title>".htmlspecialchars($form->getTitle())."</Title><Content>".htmlspecialchars($form->getContent())."</Content><DateTime><Year>".$form->getYear()."</Year><Month>".$form->getMonth()."</Month><Day>".$form->getDay()."</Day><Hour>".$form->getHour()."</Hour><Minute>".$form->getMinute()."</Minute><Second>".$form->getSecond()."</Second></DateTime></".get_class($form).">";
		return $xml;
	}

	private static function serializeCreateUserForm(CreateUserForm $userForm)
	{
		return "<".get_class($userForm)."><Name>".htmlspecialchars($userForm->getName())."</Name><Password>".htmlspecialchars($userForm->getPassword())."</Password><Type>".htmlspecialchars($userForm->getType())."</Type></".get_class($userForm).">";
	}

	private static function serializeEditUserForm(EditUserForm $userForm)
	{
		return "<".get_class($userForm)."><Password>".htmlspecialchars($userForm->getPassword())."</Password><NewPassword>".htmlspecialchars($userForm->getNewPassword())."</NewPassword><Type>".htmlspecialchars($userForm->getType())."</Type></".get_class($userForm).">";
	}

	private static function serializeCommentForm(CommentForm $form)
	{
		$xml = "<".get_class($form)."><Name>".htmlspecialchars($form->getName())."</Name><Title>".htmlspecialchars($form->getTitle())."</Title><Content>".htmlspecialchars($form->getContent())."</Content><Website>".htmlspecialchars($form->getWebsite())."</Website><Captcha>".htmlspecialchars($form->getCaptcha())."</Captcha></".get_class($form).">";
		return $xml;
	}

	private static function serializeSearchForm(SearchForm $form)
	{
		$xml = "<".get_class($form)."><Search>".htmlspecialchars($form->getSearch())."</Search></".get_class($form).">";
		return $xml;
	}

	private static function serializePage(Page $page)
	{
		$xml = "<".get_class($page)." title=\"".htmlspecialchars($page->getTitle())."\" id=\"".$page->getID()."\" dateTime=\"".$page->getDateTime()."\" user=\"".htmlspecialchars($page->getUserName())."\">";
		$xml .= "<".$page->getFrontEndResource()->getType()." uri=\"".$page->getFrontEndURI()."\"/>";
		$xml .= "<".$page->getBackEndResource()->getType()." uri=\"".$page->getBackEndURI()."\"/>";
		$xml .= "<Content><div xmlns=\"http://www.w3.org/1999/xhtml\">".$page->getContent()."</div></Content></".get_class($page).">";
		return $xml;
	}

	private static function serializePostPage(PostPage $page)
	{
		//commentCount=\"".$page->countComments()
		$xml = "<".get_class($page)." title=\"".htmlspecialchars($page->getTitle())."\" id=\"".$page->getID()."\" commentCount=\"".$page->getCommentCount()."\"  dateTime=\"".$page->getDateTime()."\" user=\"".htmlspecialchars($page->getUserName())."\">";
		$xml .= "<".$page->getFrontEndResource()->getType()." uri=\"".$page->getFrontEndURI()."\"/>";
		$xml .= "<".$page->getBackEndResource()->getType()." uri=\"".$page->getBackEndURI()."\"/>";
		$xml .= "<Content><div xmlns=\"http://www.w3.org/1999/xhtml\">".$page->getContent()."</div></Content></".get_class($page).">";
		return $xml;
	}

	private static function serializeComment(Comment $comment)
	{
		$xml = "<".get_class($comment)." id=\"".$comment->getID()."\" postId=\"".$comment->getPageID()."\" dateTime=\"".$comment->getDateTime()."\"";
		if ($comment->getTitle() != null) $xml .= " title=\"".htmlspecialchars($comment->getTitle())."\"";
		if ($comment->getName() != null) $xml .= " name=\"".htmlspecialchars($comment->getName())."\"";
		if ($comment->getWebsite() != null) $xml .= " website=\"".htmlspecialchars($comment->getWebsite())."\"";
		$xml .= "><Content>".htmlspecialchars($comment->getContent())."</Content></".get_class($comment).">";
		return $xml;
	}

	private static function serializeError(Error $error)
	{
		$xml = "<".get_class($error)." name=\"".htmlspecialchars($error->getName())."\"";
		if ($error->getDescription() != null) $xml .= " description=\"".htmlspecialchars($error->getDescription())."\"";
		$xml .= "/>";
		return $xml;
	}

	private static function serializeUser(User $user)
	{
		$xml = "<".get_class($user)." name=\"".$user->getName()."\"";
		if (!($user instanceof GuestUser)) $xml .= " resource=\"".$user->getBackEndURI()."\"";
		$xml .= "/>";
		return $xml;
	}

	private static function serializeFile(File $file)
	{
		return "<".get_class($file)." name=\"".htmlspecialchars($file->getName())."\"/>";
	}
}

?>
Return current item: DIY Blog