Last active
August 29, 2015 14:26
-
-
Save mneuhaus/1b90f6f7670392017670 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Famelo\Cider\Fluid; | |
/* | |
* This file belongs to the package "TYPO3 Fluid". | |
* See LICENSE.txt that was shipped with this package. | |
*/ | |
use NamelessCoder\Fluid\Core\Parser; | |
use NamelessCoder\Fluid\Core\Parser\SyntaxTree\Expression\AbstractExpressionNode; | |
use NamelessCoder\Fluid\Core\Rendering\RenderingContextInterface; | |
use NamelessCoder\Fluid\Core\Variables\VariableExtractor; | |
/** | |
* Default value shorthand expression will cycle through each variable until one | |
* is found that is not empty | |
*/ | |
class DefaultValueExpressionNode extends AbstractExpressionNode { | |
/** | |
* Pattern which detects a default value expression in shorthand syntax | |
* syntax, e.g. {checkvar||'foobar'}. | |
*/ | |
public static $detectionExpression = '/ | |
( | |
{ # Start of shorthand syntax | |
(?: # Math expression is composed of... | |
[a-zA-Z0-9.\\\'\"]+ # Check variable side | |
[\s]*\|\|[\s]* # check for a || to be the first thing after the variable | |
[a-zA-Z0-9.|\s\\\'\"]+ # at least one more variable behind the or, maybe more | |
) | |
} # End of shorthand syntax | |
)/x'; | |
/** | |
* @param RenderingContextInterface $renderingContext | |
* @param string $expression | |
* @param array $matches | |
* @return mixed | |
*/ | |
public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches) { | |
$variables = explode('||', $expression); | |
$variables = array_map(array(__CLASS__, 'trimPart'), $variables); | |
foreach ($variables as $variable) { | |
if (substr($variable, 0, 1) == '\'') { | |
return trim($variable, '\''); | |
} else if (substr($variable, 0, 1) == '"') { | |
return trim($variable, '"'); | |
} else { | |
$value = self::getTemplateVariable($variable, $renderingContext); | |
$checkResult = Parser\SyntaxTree\BooleanNode::convertToBoolean($value); | |
if ($checkResult === TRUE) { | |
return $value; | |
} | |
} | |
} | |
} | |
/** | |
* @param mixed $candidate | |
* @param RenderingContextInterface $renderingContext | |
* @return mixed | |
*/ | |
protected static function getTemplateVariable($candidate, RenderingContextInterface $renderingContext) { | |
$variables = $renderingContext->getVariableProvider()->getAll(); | |
$extractor = new VariableExtractor(); | |
return $extractor->getByPath($variables, $candidate); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
output first variable thats not false/null {foo||bar||foobar} | |
</p> | |
<p> | |
output variable or default value {foo||'foobar'} | |
</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment