Re: Before I go any further - critique please!
Posted: Fri Oct 31, 2008 7:52 pm
OK, I've added modifiers to be able to modify display-characteristics variables, such as uppercase and lowercase them.
Here's my current implementation:
It works! But here's how I'd like it to work. A separate class for dealing with the modifiers needs to be created. The properties of this class would be:
* $modifierChar
* $modifiers
The following methods should be in that class:
* setModifierCharacter()
* addModifier()
* modify()
I feel like the following methods should be in their own separate files and require_once()'d when addModifier() is called
* lower()
* upper()
I know what I need to do (unless you guys have different ideas), but I'm not experienced enough in OO to do it.
Here's my current implementation:
Code: Select all
<?php
class SMTemplate
{
private $delimStart = '{ $';
private $delimEnd = ' }';
private $modifierChar = '|';
private $modifiers = array();
private $vars = array();
private $templateFilename = '';
private $templateCode;
public function __construct($templateFilename='')
{
if (!empty($templateFilename))
{
$this->templateFilename = $templateFilename;
}
$this->addModifier(array('upper', 'lower'));
}
public function assign($var, $value=null)
{
if (is_array($var))
{
foreach ($var AS $k => $v)
{
$this->vars[$k] = $v;
}
} else
{
$this->vars[$var] = $value;
}
}
public function setStartDelimeter($startDelim)
{
$this->delimStart = $startDelim;
}
public function setEndDelimeter($endDelim)
{
$this->delimEnd = $endDelim;
}
public function setModifierCharacter($modChar)
{
$this->modifierChar = $modChar;
}
public function clear()
{
$args = func_get_args();
foreach ($args AS $arg)
{
unset($arg);
}
}
private function getTemplate(&$templateFilename)
{
return (is_file($templateFilename) && ($this->templateCode = file_get_contents($templateFilename)));
}
public function addModifier($modifiers)
{
if (is_array($modifiers))
{
foreach ($modifiers AS $modifier)
{
$this->modifiers[] = $modifier;
}
} else
{
$this->modifiers[] = $modifiers;
}
}
private function render()
{
$out = $this->templateCode;
if (strpos($out, $this->delimStart) !== false)
{
foreach ($this->vars AS $k => $v)
{
$out = $this->replace($k, $v, $out);
}
}
return $out;
}
private function modify($modifier, $varValue)
{
if (method_exists($this, strtolower($modifier)))
{
return call_user_func(array($this, $modifier), $varValue);
} else
{
trigger_error(
'<strong>SMTemplate:</strong> Unknown variable modifier (' . $modifier . ').',
E_USER_ERROR
);
}
}
private function lower($varValue)
{
return strtolower($varValue);
}
private function upper($varValue)
{
return strtoupper($varValue);
}
private function replace($varName, $varValue, &$out)
{
if (!is_array($varValue))
{
if (strpos($out, $varName . $this->modifierChar) !== false)
{
foreach ($this->modifiers AS $modifier)
{
if (stripos($out, $varName . $this->modifierChar . $modifier))
{
$out = str_replace(
$this->delimStart . $varName . $this->modifierChar . $modifier . $this->delimEnd,
$this->modify($modifier, $varValue),
$out
);
break;
}
}
} else
{
$varToReplace = $this->delimStart . $varName . $this->delimEnd;
if (strpos($out, $varToReplace) !== false)
{
$out = str_replace($varToReplace, $varValue, $out);
}
}
} else
{
foreach ($varValue AS $k => $v)
{
$out = $this->replace($varName . '.' . $k, $v, $out);
}
}
return $out;
}
public function display($templateFilename='', $return=false)
{
if (empty($this->templateFilename))
{
$this->templateFilename = $templateFilename;
}
if (!$this->getTemplate($this->templateFilename))
{
trigger_error(
'<strong>SMTemplate:</strong> Could not load template file (' . $templateFilename . ').',
E_USER_ERROR
);
}
if ($return)
{
return $this->render();
}
echo $this->render();
}
}* $modifierChar
* $modifiers
The following methods should be in that class:
* setModifierCharacter()
* addModifier()
* modify()
I feel like the following methods should be in their own separate files and require_once()'d when addModifier() is called
* lower()
* upper()
I know what I need to do (unless you guys have different ideas), but I'm not experienced enough in OO to do it.