‪TYPO3CMS  9.5
TYPO3\CMS\Extbase\Mvc\View\JsonView Class Reference
Inheritance diagram for TYPO3\CMS\Extbase\Mvc\View\JsonView:
TYPO3\CMS\Extbase\Mvc\View\AbstractView TYPO3\CMS\Extbase\Mvc\View\ViewInterface

Public Member Functions

 injectPersistenceManager (PersistenceManagerInterface $persistenceManager)
 
 injectReflectionService (ReflectionService $reflectionService)
 
 setVariablesToRender (array $variablesToRender)
 
 setConfiguration (array $configuration)
 
string render ()
 
- ‪Public Member Functions inherited from ‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
 setControllerContext (\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
 
TYPO3 CMS Extbase Mvc View AbstractView assign ($key, $value)
 
TYPO3 CMS Extbase Mvc View AbstractView assignMultiple (array $values)
 
bool canRender (\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
 
 initializeView ()
 

Public Attributes

const EXPOSE_CLASSNAME_FULLY_QUALIFIED = 1
 
const EXPOSE_CLASSNAME_UNQUALIFIED = 2
 

Protected Member Functions

mixed renderArray ()
 
mixed transformValue ($value, array $configuration)
 
array string transformObject (object $object, array $configuration)
 

Protected Attributes

ReflectionService $reflectionService
 
TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
 
string[] $variablesToRender = array( 'value' )
 
array $configuration = array( )
 
PersistenceManagerInterface $persistenceManager
 
- ‪Protected Attributes inherited from ‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
 
array $variables = array( )
 

Detailed Description

A JSON view

Definition at line 28 of file JsonView.php.

Member Function Documentation

◆ injectPersistenceManager()

TYPO3\CMS\Extbase\Mvc\View\JsonView::injectPersistenceManager ( PersistenceManagerInterface  $persistenceManager)
Parameters
PersistenceManagerInterface$persistenceManager

Definition at line 167 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$persistenceManager.

◆ injectReflectionService()

TYPO3\CMS\Extbase\Mvc\View\JsonView::injectReflectionService ( ReflectionService  $reflectionService)
Parameters
ReflectionService$reflectionService

Definition at line 176 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$reflectionService.

◆ render()

string TYPO3\CMS\Extbase\Mvc\View\JsonView::render ( )

Transforms the value view variable to a serializable array representation using a YAML view configuration and JSON encodes the result.

Returns
‪string The JSON encoded variables

Implements TYPO3\CMS\Extbase\Mvc\View\ViewInterface.

Definition at line 207 of file JsonView.php.

References $GLOBALS, and TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray().

◆ renderArray()

mixed TYPO3\CMS\Extbase\Mvc\View\JsonView::renderArray ( )
protected

Loads the configuration and transforms the value to a serializable array.

Returns
‪mixed

Definition at line 239 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\render().

◆ setConfiguration()

TYPO3\CMS\Extbase\Mvc\View\JsonView::setConfiguration ( array  $configuration)
Parameters
array$configuration‪The rendering configuration for this JSON view

Definition at line 195 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration.

◆ setVariablesToRender()

TYPO3\CMS\Extbase\Mvc\View\JsonView::setVariablesToRender ( array  $variablesToRender)

Specifies which variables this JsonView should render By default only the variable 'value' will be rendered

Parameters
array$variablesToRender

Definition at line 187 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender.

◆ transformObject()

array string TYPO3\CMS\Extbase\Mvc\View\JsonView::transformObject ( object  $object,
array  $configuration 
)
protected

Traverses the given object structure in order to transform it into an array structure.

Parameters
object$objectObject to traverse
array$configurationConfiguration for transforming the given object or NULL
Returns
‪array|string Object structure as an array or as a rendered string (for a DateTime instance)

Definition at line 296 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames(), TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

◆ transformValue()

mixed TYPO3\CMS\Extbase\Mvc\View\JsonView::transformValue (   $value,
array  $configuration 
)
protected

Transforms a value depending on type recursively using the supplied configuration.

Parameters
mixed$value‪The value to transform
array$configurationConfiguration for transforming the value
Returns
‪mixed The transformed value

Definition at line 263 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject().

Member Data Documentation

◆ $configuration

array TYPO3\CMS\Extbase\Mvc\View\JsonView::$configuration = array( )
protected

The rendering configuration for this JSON view which determines which properties of each variable to render.

The configuration array must have the following structure:

Example 1:

[ 'variable1' => [ '_only' => ['property1', 'property2', ...] ], 'variable2' => [ '_exclude' => ['property3', 'property4, ...] ], 'variable3' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ '_only' => [array(]'firstName', 'lastName'] ] ] ], 'somearrayvalue' => [ '_descendAll' => [ '_only' => ['property1'] ] ] ]

Of variable1 only property1 and property2 will be included. Of variable2 all properties except property3 and property4 are used. Of variable3 all properties except secretTitle are included.

If a property value is an array or object, it is not included by default. If, however, such a property is listed in a "_descend" section, the renderer will descend into this sub structure and include all its properties (of the next level).

The configuration of each property in "_descend" has the same syntax like at the top level. Therefore - theoretically - infinitely nested structures can be configured.

To export indexed arrays the "_descendAll" section can be used to include all array keys for the output. The configuration inside a "_descendAll" will be applied to each array element.

Example 2: exposing object identifier

[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being a persisted entity '_only' => ['firstName'], '_exposeObjectIdentifier' => TRUE, '_exposedObjectIdentifierKey' => 'guid' ] ] ] ]

Note for entity objects you are able to expose the object's identifier also, just add an "_exposeObjectIdentifier" directive set to TRUE and an additional property '__identity' will appear keeping the persistence identifier. Renaming that property name instead of '__identity' is also possible with the directive "_exposedObjectIdentifierKey". Example 2 above would output (summarized): {"customer":{"firstName":"John","guid":"892693e4-b570-46fe-af71-1ad32918fb64"}}

Example 3: exposing object's class name

[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being an object '_only' => ['firstName'], '_exposeClassName' => \TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_FULLY_QUALIFIED ] ] ] ]

The _exposeClassName is similar to the objectIdentifier one, but the class name is added to the JSON object output, for example (summarized): {"customer":{"firstName":"John","__class":"Acme\Foo\Domain\Model\Customer"}}

The other option is EXPOSE_CLASSNAME_UNQUALIFIED which only will give the last part of the class without the namespace, for example (summarized): {"customer":{"firstName":"John","__class":"Customer"}} This might be of interest to not provide information about the package or domain structure behind.

Definition at line 157 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray(), TYPO3\CMS\Extbase\Mvc\View\JsonView\setConfiguration(), TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

◆ $controllerContext

TYPO3 CMS Extbase Mvc Controller ControllerContext TYPO3\CMS\Extbase\Mvc\View\JsonView::$controllerContext
protected

Definition at line 52 of file JsonView.php.

◆ $persistenceManager

PersistenceManagerInterface TYPO3\CMS\Extbase\Mvc\View\JsonView::$persistenceManager
protected

◆ $reflectionService

ReflectionService TYPO3\CMS\Extbase\Mvc\View\JsonView::$reflectionService
protected

◆ $variablesToRender

string [] TYPO3\CMS\Extbase\Mvc\View\JsonView::$variablesToRender = array( 'value' )
protected

Only variables whose name is contained in this array will be rendered

Definition at line 58 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender().

◆ EXPOSE_CLASSNAME_FULLY_QUALIFIED

const TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_FULLY_QUALIFIED = 1

Definition for the class name exposure configuration, that is, if the class name of an object should also be part of the output JSON, if configured.

Setting this value, the object's class name is fully put out, including the namespace.

Definition at line 38 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\JsonViewTest\exposeClassNameSettingsAndResults().

◆ EXPOSE_CLASSNAME_UNQUALIFIED

const TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_UNQUALIFIED = 2

Puts out only the actual class name without namespace. See EXPOSE_CLASSNAME_FULL for the meaning of the constant at all.

Definition at line 44 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\JsonViewTest\exposeClassNameSettingsAndResults().