class JsonView extends AbstractView (View source)

A JSON view

Constants

EXPOSE_CLASSNAME_FULLY_QUALIFIED

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.

EXPOSE_CLASSNAME_UNQUALIFIED

Puts out only the actual class name without namespace.

See EXPOSE_CLASSNAME_FULL for the meaning of the constant at all.

Properties

protected array $supportedOptions

Supported options

protected array $options

The configuration options of this view

from  AbstractView
protected array $variables

View variables and their values

from  AbstractView
protected ControllerContext $controllerContext
protected array $variablesToRender

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

protected array $configuration

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

protected PersistenceManagerInterface $persistenceManager

Methods

static ViewInterface
createWithOptions(array $options)

Factory method to create an instance with given options.

__construct(array $options = [])

Set default options based on the supportedOptions provided

mixed
getOption(string $optionName)

Get a specific option of this View

void
setOption(string $optionName, mixed $value)

Set a specific option of this View

assign(string $key, mixed $value)

Add a variable to $this->variables.

assignMultiple(array $values)

Add multiple variables to $this->variables.

void
setControllerContext(ControllerContext $controllerContext)

Sets the current controller context

bool
canRender(ControllerContext $controllerContext)

Tells if the view implementation can render the view for the given context.

void
setVariablesToRender(array $variablesToRender)

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

void
setConfiguration(array $configuration)

No description

string|ActionResponse|ResponseInterface|StreamInterface|object
render()

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

array|string|int|float|null
renderArray()

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

array|string|int|float|null
transformValue(mixed $value, array $configuration)

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

array|string
transformObject(object $object, array $configuration)

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

Details

static ViewInterface createWithOptions(array $options)

Factory method to create an instance with given options.

Parameters

array $options

Return Value

ViewInterface

__construct(array $options = [])

Set default options based on the supportedOptions provided

Parameters

array $options

Exceptions

Exception

mixed getOption(string $optionName)

Get a specific option of this View

Parameters

string $optionName

Return Value

mixed

Exceptions

Exception

void setOption(string $optionName, mixed $value)

Set a specific option of this View

Parameters

string $optionName
mixed $value

Return Value

void

Exceptions

Exception

ViewInterface assign(string $key, mixed $value)

Add a variable to $this->variables.

Can be chained, so $this->view->assign(..., ...)->assign(..., ...); is possible

Parameters

string $key

Key of variable

mixed $value

Value of object

Return Value

ViewInterface

an instance of $this, to enable chaining

ViewInterface assignMultiple(array $values)

Add multiple variables to $this->variables.

Parameters

array $values

array in the format array(key1 => value1, key2 => value2)

Return Value

ViewInterface

an instance of $this, to enable chaining

void setControllerContext(ControllerContext $controllerContext)

Sets the current controller context

Parameters

ControllerContext $controllerContext

Context of the controller associated with this view

Return Value

void

bool canRender(ControllerContext $controllerContext)

Tells if the view implementation can render the view for the given context.

By default we assume that the view implementation can handle all kinds of contexts. Override this method if that is not the case.

Parameters

ControllerContext $controllerContext

Return Value

bool

true if the view has something useful to display, otherwise false

void setVariablesToRender(array $variablesToRender)

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

Parameters

array $variablesToRender

Return Value

void

void setConfiguration(array $configuration)

No description

Parameters

array $configuration

The rendering configuration for this JSON view

Return Value

void

string|ActionResponse|ResponseInterface|StreamInterface|object render()

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

Return Value

string|ActionResponse|ResponseInterface|StreamInterface|object

The rendered result; object is only handled if __toString() exists!

protected array|string|int|float|null renderArray()

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

Return Value

array|string|int|float|null

An array containing the values, ready to be JSON encoded

protected array|string|int|float|null transformValue(mixed $value, array $configuration)

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

Parameters

mixed $value

The value to transform

array $configuration

Configuration for transforming the value

Return Value

array|string|int|float|null

The transformed value

protected array|string transformObject(object $object, array $configuration)

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

Parameters

object $object

Object to traverse

array $configuration

Configuration for transforming the given object or NULL

Return Value

array|string

Object structure as an array