Tpl block behaviour #4
This commit is contained in:
Gnieark 2018-03-26 22:11:50 +02:00 committed by GitHub
commit 88962bb530
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 4229 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/vendor

View File

@ -7,6 +7,7 @@
* @category Template
* @package TplBlock
* @author gnieark <gnieark@tinad.fr>
* @link https://blog-du-grouik.tinad.fr
* @license GNU General Public License V3
* @link https://github.com/gnieark/tplBlock/
*/
@ -28,28 +29,28 @@ class TplBlock
*
* @var string
*/
const BLOCKSTARTSTART = '<!-- BEGIN ';
const BLOCKSTARTSTART = '<!--\s+BEGIN\s+';
/**
* The string ending a block start.
*
* @var string
*/
const BLOCKSTARTEND = ' -->';
const BLOCKSTARTEND = '\s+-->';
/**
* The string starting a block end.
*
* @var string
*/
const BLOCKENDSTART = '<!-- END ';
const BLOCKENDSTART = '<!--\s+END\s+';
/**
* The string ending a block end.
*
* @var string
*/
const BLOCKENDEND = ' -->';
const BLOCKENDEND = '\s+-->';
/**
* The string starting an enclosure.
@ -107,6 +108,12 @@ class TplBlock
*/
private $replaceNonGivenVars = true;
/**
* Use strict mode?
*
* @var boolean
*/
private $strictMode = true;
/**
* Initialize TplBlock
*
@ -200,6 +207,19 @@ class TplBlock
. '/is';
}
/**
* Make some tests to check template consistency.
* Return error message if needed, return 1 otherwise.
*
* @param string $str the string to check
* @return string message
*/
public function checkConsistency($str){
return 1;
}
/**
* Shake the template string and input vars then returns the parsed text.
*
@ -211,6 +231,14 @@ class TplBlock
*/
public function applyTplStr($str, $subBlocsPath = "")
{
if($this->strictMode){
$consistency = self::checkConsistency($str);
if($consistency <> 1 ){
throw new \UnexpectedValueException($consistency);
}
}
// Replace all simple vars.
$prefix = $subBlocsPath === "" ? "" : $subBlocsPath . ".";
@ -315,4 +343,27 @@ class TplBlock
}
/**
* Enable mode strict. If template is inconsistent, will throw an exception
* and return nothing
*
* @return TplBlock For chaining.
*/
public function doStrictMode()
{
$this->strictMode = true;
return $this;
}
/**
* Disable mode strict. If template is inconsistent, will be parsed anyway.
* and no errors will be returned.
*
* @return TplBlock For chaining.
*/
public function dontStrictMode(){
$this->strictMode = false;
return $this;
}
}

4143
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
hello {{title}} {{name}} {{firstname}}
The first primes numbers are:
<!-- BEGIN primes -->
<!-- BEGIN primes -->
- {{primes.number}}
<!-- END primes -->
<!-- END primes -->
Lets list all divisors of:

View File

@ -116,6 +116,33 @@ class TplBlockTest extends TestCase
$this->assertFalse(strpos("WONT", $actual));
}
/**
* Test blocs with tabs spaces etc..
*
* @return void
*/
public function testBlocsWithsWeirdSpaces()
{
$model = "
Bhah blah wpooie456
<!-- BEGIN bloc -->
have to be shown
<!-- END bloc -->
<!-- BEGIN blocTwo -->
WONT to be shown
<!-- END blocTwo -->
";
$template = new TplBlock();
$actual = $template
->addSubBlock(new TplBlock("bloc"))
->applyTplStr($model);
$this->assertContains("have", $actual);
$this->assertFalse(strpos("WONT", $actual));
}
/**
* Test if error on blocks names WTF.
*