DateTimeConverter extends AbstractTypeConverter

Converter which transforms from different input formats into DateTime objects.

Source can be either a string or an array. The date string is expected to be formatted according to DEFAULT_DATE_FORMAT.

But the default date format can be overridden in the initialize*Action() method like this::

$this->arguments['<argumentName>'] ->getPropertyMappingConfiguration() ->forProperty('<propertyName>') // this line can be skipped in order to specify the format for all properties ->setTypeConverterOption(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, '<dateFormat>');

If the source is of type array, it is possible to override the format in the source::

array( 'date' => '<dateString>', 'dateFormat' => '<dateFormat>' );

By using an array as source you can also override time and timezone of the created DateTime object::

array( 'date' => '<dateString>', 'hour' => '', // integer 'minute' => '', // integer 'seconds' => '', // integer 'timezone' => '', // string, see http://www.php.net/manual/timezones.php );

As an alternative to providing the date as string, you might supply day, month and year as array items each::

array( 'day' => '', // integer 'month' => '', // integer 'year' => '', // integer );

Table of Contents

Constants

CONFIGURATION_DATE_FORMAT  = 'dateFormat'
DEFAULT_DATE_FORMAT  = \DateTimeInterface::W3C
The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00" according to the W3C standard @see http://www.w3.org/TR/NOTE-datetime.html

Methods

convertFrom()  : mixed|Error
Converts $source to a \DateTime using the configured dateFormat
getSourceChildPropertiesToBeConverted()  : array<string|int, mixed>
Return a list of sub-properties inside the source object.
getTargetTypeForSource()  : string
Returns the type for a given source, depending on e.g. the __type setting or other properties.
getTypeOfChildProperty()  : string
Return the type of a given sub-property inside the $targetType
getDefaultDateFormat()  : string
Determines the default date format to use for the conversion.
isDatePartKeysProvided()  : bool
Returns whether date information (day, month, year) are present as keys in $source.
overrideTimeIfSpecified()  : DateTime
Overrides hour, minute & second of the given date with the values in the $source array
translateErrorMessage()  : string
Wrap static call to LocalizationUtility to simplify unit testing.

Constants

CONFIGURATION_DATE_FORMAT

public string CONFIGURATION_DATE_FORMAT = 'dateFormat'

DEFAULT_DATE_FORMAT

The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00" according to the W3C standard @see http://www.w3.org/TR/NOTE-datetime.html

public string DEFAULT_DATE_FORMAT = \DateTimeInterface::W3C

Methods

convertFrom()

Converts $source to a \DateTime using the configured dateFormat

public convertFrom(string|int|array<string|int, mixed> $source, string $targetType[, array<string|int, mixed> $convertedChildProperties = [] ][, PropertyMappingConfigurationInterface|null $configuration = null ]) : mixed|Error
Parameters
$source : string|int|array<string|int, mixed>

the string to be converted to a \DateTime object

$targetType : string

must be "DateTime"

$convertedChildProperties : array<string|int, mixed> = []

not used currently

$configuration : PropertyMappingConfigurationInterface|null = null
Internal

only to be used within Extbase, not part of TYPO3 Core API.

Tags
throws
TypeConverterException
Return values
mixed|Error

the target type, or an error object if a user-error occurred

getSourceChildPropertiesToBeConverted()

Return a list of sub-properties inside the source object.

public getSourceChildPropertiesToBeConverted(mixed $source) : array<string|int, mixed>
Parameters
$source : mixed
Tags
todo

this method is only used for converter sources that have children (i.e. objects). Introduce another ChildPropertyAwareTypeConverterInterface and drop this method from the main interface

Returns an empty list of sub property names

Return values
array<string|int, mixed>

getTargetTypeForSource()

Returns the type for a given source, depending on e.g. the __type setting or other properties.

public getTargetTypeForSource(mixed $source, string $originalTargetType[, PropertyMappingConfigurationInterface|null $configuration = null ]) : string
Parameters
$source : mixed

the source data

$originalTargetType : string

the type we originally want to convert to

$configuration : PropertyMappingConfigurationInterface|null = null
Tags
todo

The concept of this method is flawed because it enables the override of the target type depending on the structure of the source. So, technically we no longer convert type A to B but source of type A with structure X to type B defined by X. This makes a type converter non-deterministic.

Returns the $originalTargetType unchanged in this implementation.

Return values
string

getTypeOfChildProperty()

Return the type of a given sub-property inside the $targetType

public getTypeOfChildProperty(string $targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration) : string
Parameters
$targetType : string
$propertyName : string
$configuration : PropertyMappingConfigurationInterface
Tags
todo

this method is only used for converter sources that have children (i.e. objects). Introduce another ChildPropertyAwareTypeConverterInterface and drop this method from the main interface

This method is never called, as getSourceChildPropertiesToBeConverted() returns an empty array.

Return values
string

the type of $propertyName in $targetType

isDatePartKeysProvided()

Returns whether date information (day, month, year) are present as keys in $source.

protected isDatePartKeysProvided(array<string|int, mixed> $source) : bool
Parameters
$source : array<string|int, mixed>
Return values
bool

overrideTimeIfSpecified()

Overrides hour, minute & second of the given date with the values in the $source array

protected overrideTimeIfSpecified(DateTime $date, array<string|int, mixed> $source) : DateTime
Parameters
$date : DateTime
$source : array<string|int, mixed>
Return values
DateTime

translateErrorMessage()

Wrap static call to LocalizationUtility to simplify unit testing.

protected translateErrorMessage(string $translateKey) : string
Parameters
$translateKey : string
Return values
string

        
On this page

Search results