1<?php
2
3/**
4 * base plugin class
5 *
6 * you have to implement the <em>process()</em> method, it will receive the parameters that
7 * are in the template code
8 *
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from the use of this software.
11 *
12 * @author     Jordi Boggiano <j.boggiano@seld.be>
13 * @copyright  Copyright (c) 2008, Jordi Boggiano
14 * @license    http://dwoo.org/LICENSE   Modified BSD License
15 * @link       http://dwoo.org/
16 * @version    1.0.0
17 * @date       2008-10-23
18 * @package    Dwoo
19 */
20abstract class Dwoo_Plugin
21{
22	/**
23	 * the dwoo instance that runs this plugin
24	 *
25	 * @var Dwoo
26	 */
27	protected $dwoo;
28
29	/**
30	 * constructor, if you override it, call parent::__construct($dwoo); or assign
31	 * the dwoo instance yourself if you need it
32	 *
33	 * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin
34	 */
35	public function __construct(Dwoo_Core $dwoo)
36	{
37		$this->dwoo = $dwoo;
38	}
39
40	// plugins should always implement :
41	// public function process($arg, $arg, ...)
42	// or for block plugins :
43	// public function init($arg, $arg, ...)
44
45	// this could be enforced with :
46	// abstract public function process(...);
47	// if my feature request gets enough interest one day
48	// see => http://bugs.php.net/bug.php?id=44043
49
50	/**
51	 * utility function that converts an array of compiled parameters (or rest array) to a string of xml/html tag attributes
52	 *
53	 * this is to be used in preProcessing or postProcessing functions, example :
54	 *  $p = $compiler->getCompiledParams($params);
55	 *  // get only the rest array as attributes
56	 *  $attributes = Dwoo_Plugin::paramsToAttributes($p['*']);
57	 *  // get all the parameters as attributes (if there is a rest array, it will be included)
58	 *  $attributes = Dwoo_Plugin::paramsToAttributes($p);
59	 *
60	 * @param array $params an array of attributeName=>value items that will be compiled to be ready for inclusion in a php string
61	 * @param string $delim the string delimiter you want to use (defaults to ')
62	 * @return string
63	 */
64	public static function paramsToAttributes(array $params, $delim = '\'')
65	{
66		if (isset($params['*'])) {
67			$params = array_merge($params, $params['*']);
68			unset($params['*']);
69		}
70
71		$out = '';
72		foreach ($params as $attr=>$val) {
73			$out .= ' '.$attr.'=';
74			if (trim($val, '"\'')=='' || $val=='null') {
75				$out .= str_replace($delim, '\\'.$delim, '""');
76			} elseif (substr($val, 0, 1) === $delim && substr($val, -1) === $delim) {
77				$out .= str_replace($delim, '\\'.$delim, '"'.substr($val, 1, -1).'"');
78			} else {
79				$out .= str_replace($delim, '\\'.$delim, '"') . $delim . '.'.$val.'.' . $delim . str_replace($delim, '\\'.$delim, '"');
80			}
81		}
82
83		return ltrim($out);
84	}
85}
86