CoolCMS API

StringValueCoercer
in package

FinalYes

Converts text values to the declared type of an entity field.

The external Api client sends data in the query string (?data[currencyId]=1), so every value arrives as a string and a typed setter (setCurrencyId(?int)) would fail with TypeError. The hydrator therefore converts the string by the declared type:

  • the type comes from the setter parameter; when it is missing (or mixed) from the property type, and failing that from the Doctrine column mapping; a union type (int|string) is not converted,
  • int accepts only a canonical integer (-12, 0); not 1.9, 1e2, +1, 01, spaces,
  • float accepts numeric notation (-1.5, .5, 2e3) without spaces,
  • bool accepts 1/0/true/false/on/off (lowercase) and nothing else,
  • DateTime* accepts what new DateTime() accepts in the application time zone (same as DateTimeHelper::convertToDateTime()); an overflowing date (2026-02-31) is rejected,
  • an int-backed enum takes a canonical integer; the hydrator builds the enum from it,
  • '' is null for a nullable type and an error otherwise.

A string for a string field and native values from the internal path (int, bool, DateTime...) never get here: the hydrator calls the conversion only for a string whose target is not a string.

Table of Contents

Constants

Bool  : string = 'bool'
Float  : string = 'float'
Int  : string = 'int'
DoctrineTypeMap  : array<string|int, mixed> = ['integer' => self::Int, 'smallint' => self::In...

Methods

coerce()  : int|float|bool|DateTimeInterface|null
Converts a string to the target type from {@see resolveTarget()}.
resolveTarget()  : array{type: string, nullable: bool, enum: bool}|null
Resolves what type a string should be converted to for the given field.
informativeType()  : ReflectionType|null
A missing type and `mixed` say nothing about the field, so the next source decides.
targetFromType()  : array{type: string, nullable: bool, enum: bool}|null
toBool()  : bool|null
toDateTime()  : DateTimeInterface|null
toFloat()  : float|null
toInt()  : int|null

Constants

DoctrineTypeMap

private array<string|int, mixed> DoctrineTypeMap = ['integer' => self::Int, 'smallint' => self::Int, 'float' => self::Float, 'smallfloat' => self::Float, 'boolean' => self::Bool, 'datetime' => \DateTime::class, 'datetimetz' => \DateTime::class, 'date' => \DateTime::class, 'time' => \DateTime::class, 'datetime_immutable' => \DateTimeImmutable::class, 'datetimetz_immutable' => \DateTimeImmutable::class, 'date_immutable' => \DateTimeImmutable::class, 'time_immutable' => \DateTimeImmutable::class]

Methods

coerce()

Converts a string to the target type from {@see resolveTarget()}.

public static coerce(string $value, string $type, bool $nullable, string $field) : int|float|bool|DateTimeInterface|null
Parameters
$value : string
$type : string
$nullable : bool
$field : string
Tags
throws
InvalidFieldValueException

when the value cannot be converted

Return values
int|float|bool|DateTimeInterface|null

resolveTarget()

Resolves what type a string should be converted to for the given field.

public static resolveTarget(ReflectionClass<string|int, object> $reflection, string $setter, string $propertyName, array<string, mixed>|null $fieldMapping) : array{type: string, nullable: bool, enum: bool}|null
Parameters
$reflection : ReflectionClass<string|int, object>
$setter : string
$propertyName : string
$fieldMapping : array<string, mixed>|null

Doctrine column mapping (fallback)

Return values
array{type: string, nullable: bool, enum: bool}|null —

null means do not convert

informativeType()

A missing type and `mixed` say nothing about the field, so the next source decides.

private static informativeType(ReflectionType|null $type) : ReflectionType|null
Parameters
$type : ReflectionType|null
Return values
ReflectionType|null

targetFromType()

private static targetFromType(ReflectionType $declared, array<string, mixed>|null $fieldMapping) : array{type: string, nullable: bool, enum: bool}|null
Parameters
$declared : ReflectionType
$fieldMapping : array<string, mixed>|null
Return values
array{type: string, nullable: bool, enum: bool}|null

toBool()

private static toBool(string $value) : bool|null
Parameters
$value : string
Return values
bool|null

toDateTime()

private static toDateTime(string $value, DateTime>|DateTimeImmutable> $class) : DateTimeInterface|null
Parameters
$value : string
$class : DateTime>|DateTimeImmutable>
Return values
DateTimeInterface|null

toFloat()

private static toFloat(string $value) : float|null
Parameters
$value : string
Return values
float|null

toInt()

private static toInt(string $value) : int|null
Parameters
$value : string
Return values
int|null
On this page

Search results