Location: PHPKode > scripts > md_Taconite > md_Taconite.php
<?php
/*
		This file is part of the md library.

		md library is free software; you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation; either version 3 of the License, or
		any later version.

		md library is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		You should have received a copy of the GNU General Public License
		along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


/**
 * md library
 * 
 * @category md
 * @package md_Taconite
 * @copyright copyright (c) 2007 Mickaël Desfrênes <hide@address.com>
 */

/**
 * Class for generating Taconite response
 *  
 * @category md
 * @package md_Taconite
 */
 
class md_Taconite
{
	/**
	 * @var string
	 */	
	private $content;
	
	/**
	 * Public constructor	(obviously !)
	 */	
	public function __construct()
	{
		$this->content = '';
	}
	
	/**
	 * Appends XHTML content to matching elements
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content	 
	 */
	public function append($selector,$content)
	{
		$this->elementCommand('append',$selector,$content);
	}
	
	/**
	 * Prepends XHTML content to matching elements
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content	 
	 */
	public function prepend($selector,$content)
	{
		$this->elementCommand('prepend',$selector,$content);
	}
	
	/**
	 * Puts XHTML content before matching elements
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content	 	 	 	
	 */
	public function before($selector,$content)
	{
		$this->elementCommand('before',$selector,$content);
	}
	
	/**
	 * Puts XHTML content after matching elements
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content	  	 	
	 */
	public function after($selector,$content)
	{
		$this->elementCommand('after',$selector,$content);
	}
	
	/**
	 * Wraps matching elements with given tags.
	 * 	 
	 * Don't use text in $content
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Wrapper string	 	 	 	
	 */	
	public function wrap($selector,$content)
	{
		$this->elementCommand('wrap',$selector,$content);
	}
	
	/**
	 * Replaces matching elements with given content.
	 * 	 
	 * This is not JQuery-native but a convenience of the Taconite plugin
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content
	 */	
	public function replace($selector,$content)
	{
		$this->elementCommand('replace',$selector,$content);
	}
	
	/**
	 * Replaces matching element's content with given content.
	 * 	 
	 * This is not JQuery-native but a convenience if the Taconite plugin
	 * 	 	 	
	 * @param string $selector Any valid JQuery selector
	 * @param string $content Any valid XHTML content
	 */	
	public function replaceContent($selector,$content)
	{
		$this->elementCommand('replaceContent',$selector,$content);
	}

	/**
	 * Removes matching elements
	 * 
	 * @param string $selector Any valid JQuery selector 	 
	 */	
	public function remove($selector)
	{
		$this->rawCommand('<remove select="'.$selector.'" />');
	}

	/**
	 * Hides matching elements
	 * 
	 * @param string $selector Any valid JQuery selector	 	 	
	 */	
	public function hide($selector)
	{
		$this->rawCommand('<hide select="'.$selector.'" />');
	}

	/**
	 * Remove content from matching elements (JQuery's empty method)
	 *	
	 * @param string $selector Any valid JQuery selector	 	 	
	 */	
	public function removeContent($selector)
	{
		$this->rawCommand('<empty select="'.$selector.'" />');
	}

	/**
	 * Adds class to matching elements
	 * 
	 * @param string $class CSS class to add
	 * @param string $selector Any valid JQuery selector	 	 	 	
	 */	
	public function addClass($class,$selector)
	{
		$this->rawCommand('<addClass select="'.$selector.'" arg1="'.$class.'" /><addClass select="'.$selector.'" value="'.$class.'" />');
	}

	/**
	 * Removes class from matching elements
	 * 
	 * @param string $class CSS class to remove
	 * @param string $selector Any valid JQuery selector	 	 	 	
	 */		
	public function removeClass($class,$selector)
	{
		$this->rawCommand('<removeClass select="'.$selector.'" arg1="'.$class.'" /><removeClass select="'.$selector.'" value="'.$class.'" />');
	}
	
	/**
	 * Toggles a class to matching elements
	 * 
	 * @param string $class CSS class to toggle
	 * @param string $selector Any valid JQuery selector	 	 	 	
	 */		
	public function toggleClass($class,$selector)
	{
		$this->rawCommand('<toggleClass select="'.$selector.'" arg1="'.$class.'" /><toggleClass select="'.$selector.'" value="'.$class.'" />');
	}

	/**
	 * Modifies a css property
	 * 
	 * The taconite plugin requires that you "camelize" all css properties but
	 * this will do it for you if forget it.	 	 	 
	 * 
	 * @param string $selector Any valid JQuery selector
	 * @param string $property Any CSS property
	 * @param string $value CSS value	 	 	 	 	
	 */	
	public function css($selector,$property,$value)
	{
		$property = $this->camelize($property);
		$taco = '<css select="'.$selector.'" name="'.$property.'" value="'.$value.'" />';
		$this->rawCommand($taco);
	}

	/**
	 * Adds Javascript to be evaluated in the global context
	 * 
	 * @param string $script Javascript string	 	 
	 */	
	public function js($script)
	{
		$taco = '<eval><![CDATA['.$script.']]></eval>';
		$this->rawCommand($taco);
	}

	/**
	 * Adds an element command, as described in the Taconite plugin docs.
	 * 
	 * @param string $method A JQuery method
	 * @param string $selector Any valid JQuery selector
	 * @param string $content XHTML content	 	 	 	 	
	 */		
	public function elementCommand($method,$selector,$content)
	{
		$taco = '<'.$method.' select="'.$selector.'">'.$content.'</'.$method.'>';
		$this->rawCommand($taco);
	}
	
	/**
	 * Adds a raw Taconite command to the document
	 * 
	 * @param string $command A Taconite command	 	 	
	 */	
	public function rawCommand($command)
	{
		$this->content.= $command;
	}
	
	/**
	 * Returns the command document string
	 * 
	 * This method does not perform any syntax check !	 	 
	 * 
	 * @return string	 	 	
	 */	
	public function __toString()
	{
		return('<taconite>'.$this->content.'</taconite>');
	}
	
	/**
	 * Renders the Taconite command document.
	 * 
	 * This performs basic syntax check with the DOMDocument extension.	 	 
	 * 
	 * @param bool $die Wether to die after rendering or not
	 * @exception	 
	 */	
	public function render($die = false)
	{
		$trans = array('&nbsp;' => '&#160;');
		$this->content = strtr($this->content,$trans);
		if($this->isValid())
		{
			header('Content-type: text/xml; charset=UTF-8');
			if($die)
			{
				die($this);
			}
			else
			{
				echo($this);
			}
		}
		else
		{
			throw new Exception('Taconite command string is not valid XML.');
		}
	}

	private function camelize($property)
	{
		$property_chops = explode('-',$property);
		$chops_size = count($property_chops);
		if($chops_size > 1)
		{
			for($i = 1;$i < $chops_size;$i++)
			{
				$property_chops[$i] = ucfirst(trim($property_chops[$i]));
			}
			$property = implode('',$property_chops);
		}
		return $property;
	}
	
	private function isValid()
	{
		$string = '<taconite>'.$this->content.'</taconite>';
		libxml_use_internal_errors(true);
		$dom = new DOMDocument('1.0','utf-8');
		$dom->loadXML($string);
		$errors = libxml_get_errors();
		if(empty($errors))
		{
			return true;
		}
		return false;
	}
	
}
Return current item: md_Taconite