Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ViewHelpers/Expression/CastViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Math Expression ViewHelper, seconds as expression type
* Cast Expression ViewHelper, seconds as expression type
*/
class CastViewHelper extends AbstractViewHelper implements ExpressionComponentInterface
{
Expand All @@ -31,7 +31,7 @@ public function __construct(iterable $parts = [])
protected function initializeArguments()
{
$this->registerArgument('subject', 'mixed', 'Numeric first value to calculate', true);
$this->registerArgument('as', 'string', 'Type to cast, valid values are: integer, boolean, string, float and array', true);
$this->registerArgument('as', 'string', 'Type to cast, valid values are: integer, boolean, string, float, array and DateTime', true);
}

public function evaluate(RenderingContextInterface $renderingContext)
Expand Down
85 changes: 85 additions & 0 deletions src/ViewHelpers/Expression/NullcoalescingViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace TYPO3Fluid\Fluid\ViewHelpers\Expression;

/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

use TYPO3Fluid\Fluid\Component\ExpressionComponentInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Ternary Condition Node - allows the shorthand version
* of a condition to be written as `{var ? thenvar : elsevar}`
*/
class NullcoalescingViewHelper extends AbstractViewHelper implements ExpressionComponentInterface
{
protected $parts = [];

public function __construct(iterable $parts = [])
{
$this->parts = $parts;
}

protected function initializeArguments()
{
$this->registerArgument('a', 'mixed', 'Anythong that can have a value or null', true);
$this->registerArgument('b', 'mixed', 'Fallback value', true);
}

public static function matches(array $parts): bool
{
var_dump('matches');
return isset($parts[2]) && strpos('??', $parts[1]) !== false;
}

/**
* @param RenderingContextInterface $renderingContext
* @param string $expression
* @param array $matches
* @return mixed
*/
public function evaluate(RenderingContextInterface $renderingContext)
{
$arguments = $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy();
$parts = empty($this->parts) ? [$arguments['a'], $arguments['b']] : $this->parts;

var_dump($parts);
var_dump($renderingContext->getVariableProvider()->getAll());

foreach($parts as $part) {
$value = static::getTemplateVariableOrValueItself($part, $renderingContext);
var_dump($value);
if(!is_null($value)) {
return $value;
}
}

return null;
}


/**
* @param mixed $candidate
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
protected static function getTemplateVariableOrValueItself($candidate, RenderingContextInterface $renderingContext)
{
if(is_null($candidate)) {
return null;
}
if (is_numeric($candidate)) {
return $candidate;
}

if (mb_strpos($candidate, '\'') === 0) {
return trim($candidate, '\'');
} elseif (mb_strpos($candidate, '"') === 0) {
return trim($candidate, '"');
}
return $renderingContext->getVariableProvider()->get($candidate);
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Core/Rendering/RenderingContextFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TYPO3Fluid\Fluid\Core\ErrorHandler\ErrorHandlerInterface;
use TYPO3Fluid\Fluid\Core\ErrorHandler\StandardErrorHandler;
use TYPO3Fluid\Fluid\Core\Parser\Configuration;
use TYPO3Fluid\Fluid\ViewHelpers\Expression\NullcoalescingViewHelper;
use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
use TYPO3Fluid\Fluid\Core\Rendering\FluidRenderer;
use TYPO3Fluid\Fluid\Core\Rendering\FluidRendererInterface;
Expand Down Expand Up @@ -71,6 +72,7 @@ class RenderingContextFixture implements RenderingContextInterface
MathViewHelper::class,
CastViewHelper::class,
IfViewHelper::class,
NullcoalescingViewHelper::class,
];

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/Unit/ViewHelpers/Expression/NullcoalescingViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace TYPO3Fluid\Fluid\Tests\Unit\ViewHelpers\Expression;


/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
use TYPO3Fluid\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestCase;
use TYPO3Fluid\Fluid\ViewHelpers\Expression\NullcoalescingViewHelper;

/**
* Class TernaryExpressionNodeTest
*/
class NullcoalescingViewHelperTest extends ViewHelperBaseTestCase
{


/**
* @dataProvider getEvaluateExpressionTestValues
* @param string $expression
* @param array $variables
* @param mixed $expected
*/
public function testEvaluateExpression($expression, array $variables, $expected)
{
$renderingContext = new RenderingContext();
$renderingContext->setVariableProvider(new StandardVariableProvider($variables));
$result = NullcoalescingViewHelper::evaluateExpression($renderingContext, $expression, []);
$this->assertEquals($expected, $result);
}

public function getStandardTestValues(): array
{

$context = new RenderingContextFixture();
foreach(['a' => 'a', 'b' => 'b', 'c' => 'c'] as $key => $value) {
$context->getVariableProvider()->add($key, $value);
}

return [
'value not null, default integer' => ['a', $context, ['a' => 'a', 'b' => 1]],
'value null, default to 1' => [1, $context, ['d' => null, 'b' => 1]],
'value not null, default other value' => ['a', $context, ['a' => 'a', 'b' => 'b']],
'value null, default to other value' => ['b', $context, ['d' => null, 'b' => 'b']],
'value not null, default other value with additional fallback object' => ['a', $context, ['a' => 'a', 'b' => 'b', 'c' => 'c']],
'value null, default other value with additional fallback object' => ['b', $context, ['d' => null, 'b' => 'b', 'c' => 'c']],
'value null, default other value also null, with additional fallback object' => ['c', $context, ['d' => null, ['e' => null, 'c' => 'c']]],
'value null, default other value also null, with additional fallback string (single quote)' => ['test', $context, ['d' => null, 'e' => null], [new TextNode('\'test\'')]],
'value null, default other value also null, with additional fallback string (double quote)' => ['test', $context, ['d' => null, 'e' => null], [new TextNode('"test"')]],
];
}
}