some fixes, and improve tests

This commit is contained in:
Rémi Passerieu 2018-03-19 11:56:15 +01:00
parent 11be3ce70c
commit 8f24cef9f6
2 changed files with 27 additions and 6 deletions

View File

@ -20,7 +20,7 @@ class TplBlock {
* Input object name * Input object name
*/ */
public function __construct(STRING $name =""){ public function __construct($name = NULL){
$this->name = $name; $this->name = $name;
} }
@ -34,7 +34,7 @@ class TplBlock {
} }
public function add_sub_block(TplBlock $bloc){ public function add_sub_block(TplBlock $bloc){
if(empty($bloc->name)){ if(is_null($bloc->name) || empty($bloc->name)){
throw new InvalidTemplateException("A sub tpl bloc can't have an empty name"); throw new InvalidTemplateException("A sub tpl bloc can't have an empty name");
return false; return false;
} }
@ -59,9 +59,9 @@ class TplBlock {
foreach($this->subBlocs as $blocName => $blocsArr){ foreach($this->subBlocs as $blocName => $blocsArr){
$str = preg_replace_callback( $str = preg_replace_callback(
'/' . self::blockStartStart . preg_quote($blocName) . self::blockStartEnd . '/' . self::blockStartStart . preg_quote($prefix . $blocName) . self::blockStartEnd .
'(.*?)'. '(.*?)'.
self::blockEndStart . preg_quote($blocName). self::blockEndEnd. self::blockEndStart . preg_quote($prefix . $blocName). self::blockEndEnd.
'/is', '/is',
function($m) use($blocName,$blocsArr,$prefix) { function($m) use($blocName,$blocsArr,$prefix) {
$out = ""; $out = "";

View File

@ -3,6 +3,9 @@
include("../class.TplBlock.php"); include("../class.TplBlock.php");
$tpl = new TplBlock(); $tpl = new TplBlock();
//simples vars
$tpl->add_vars(array( $tpl->add_vars(array(
"name" => "Gnieark", "name" => "Gnieark",
"title" => "Monsieur", "title" => "Monsieur",
@ -12,15 +15,33 @@ $tpl->add_vars(array(
$primes = array(1,2,3,5,7,11); $primes = array(1,2,3,5,7,11);
// a sub bloc
foreach($primes as $prime){ foreach($primes as $prime){
$tplPrime = new TplBlock('primes'); $tplPrime = new TplBlock('primes');
$tplPrime->add_vars(array('number' => $prime)); $tplPrime->add_vars(array('number' => $prime));
$tpl->add_sub_block($tplPrime); $tpl->add_sub_block($tplPrime);
}
// test sub - sub blocs
for ($i = 2; $i < 121; $i++){
$tplNumber = new TplBlock('number');
$tplNumber->add_vars( array("value" => $i));
$index = 1;
$number = $i;
while ( $number > 1 && $index < count($primes)){
if($number % $primes[$index] == 0){
$number = $number / $primes[$index];
$tplDivisor = new TplBlock("divisor");
$tplDivisor->add_vars( array("value" => $primes[$index]));
$tplNumber->add_sub_block($tplDivisor);
}else{
$index++;
}
}
$tpl->add_sub_block($tplNumber);
} }
echo $tpl->apply_tpl_file("sample.txt"); echo $tpl->apply_tpl_file("sample.txt");