Skip to content

Commit

Permalink
[TASK] Tune RenderingContext withAttribute() and getAttribute()
Browse files Browse the repository at this point in the history
getAttribute($className) now always returns an instance of
$className, or throws. This avoids checking the returned
instance and helps with type hinting in IDEs.
  • Loading branch information
lolli42 committed Jun 26, 2024
1 parent 092befb commit 438d067
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
10 changes: 10 additions & 0 deletions src/Core/Rendering/AttributeNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

namespace TYPO3Fluid\Fluid\Core\Rendering;

class AttributeNotSetException extends \TYPO3Fluid\Fluid\Core\Exception {}
28 changes: 7 additions & 21 deletions src/Core/Rendering/RenderingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,32 +363,18 @@ public function buildParserConfiguration()
return $parserConfiguration;
}

/**
* Retrieve a single attribute.
*
* @see withAttribute()
* @param string $name The attribute name.
* @return object|null null if the specified attribute hasn't been set
*/
public function getAttribute(string $name): ?object
public function getAttribute(string $className): object
{
return $this->attributes[$name] ?? null;
if (!isset($this->attributes[$className])) {
throw new AttributeNotSetException('An attribute of type ' . $className . ' has not been set', 1719394231);
}
return $this->attributes[$className];
}

/**
* Return an instance with the specified attribute.
*
* This method allows you to attach arbitrary objects to the
* rendering context to be used later e. g. in ViewHelpers.
*
* @param string $name The attribute name.
* @param object $value The value of the attribute.
* @return static
*/
public function withAttribute(string $name, object $value): static
public function withAttribute(string $className, object $value): static
{
$clonedObject = clone $this;
$clonedObject->attributes[$name] = $value;
$clonedObject->attributes[$className] = $value;
return $clonedObject;
}

Expand Down
20 changes: 10 additions & 10 deletions src/Core/Rendering/RenderingContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,23 @@ public function getControllerAction();
public function setControllerAction($action);

/**
* Retrieve a single attribute.
* Retrieve a single attribute instance.
*
* @see withAttribute()
* @param string $name The attribute name.
* @return object|null null if the specified attribute hasn't been set
* @template T of object
* @param class-string<T> $className
* @return T
*/
public function getAttribute(string $name): ?object;
public function getAttribute(string $className): object;

/**
* Return an instance with the specified attribute.
* Return an instance with the specified attribute being attached.
*
* This method allows you to attach arbitrary objects to the
* rendering context to be used later e. g. in ViewHelpers.
*
* @param string $name The attribute name.
* @param object $value The value of the attribute.
* @return static
* @template T of object
* @param class-string<T> $className
* @param T $value
*/
public function withAttribute(string $name, object $value): RenderingContextInterface;
public function withAttribute(string $className, object $value): RenderingContextInterface;
}
15 changes: 11 additions & 4 deletions tests/Unit/Core/Rendering/RenderingContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessorInterface;
use TYPO3Fluid\Fluid\Core\Rendering\AttributeNotSetException;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker;
Expand Down Expand Up @@ -102,10 +103,16 @@ public function withAndGetAttribute(): void
{
$object = new stdClass();
$object->test = 'value';

$subject = new RenderingContext();
$clonedSubject = $subject->withAttribute('test', $object);
self::assertNull($subject->getAttribute('test'));
self::assertEquals($object, $clonedSubject->getAttribute('test'));
$clonedSubject = $subject->withAttribute(stdClass::class, $object);
self::assertEquals($object, $clonedSubject->getAttribute(stdClass::class));
}

#[Test]
public function getAttributeThrowsWithNoSuchAttribute(): void
{
$this->expectException(AttributeNotSetException::class);
$this->expectExceptionCode(1719394231);
(new RenderingContext())->getAttribute(stdClass::class);
}
}

0 comments on commit 438d067

Please sign in to comment.