Index

Coercer

MMO\core\schemas · 1 symbol · generated 30 Sep 2026

class

Coercer

Coercer.php:17

Converter from one type to another.

class MMO\core\schemas\Coercer extends MMO\core\classes\aCoercer
Namespace
MMO\core\schemas
Extends
aCoercer
Known options : - type: {string|array} an indication on what kind of conversion we are doing - format: {string} an indication on type of formating to do during the conversion - schema: {Schema} a json schema to use to coerce nested values

Index

coerceArray($value, $options)
coerceBoolean($value, $options)
coerceDateTime($value, $options)
coerceDateTimeImmutable($value, $options)Same as coerceDateTime, but returns a \DateTimeImmutable.
coerceEntity($value, $options)Coerce $value into an iEntity.
coerceEnum($value, $options)Coerce $value to match one of the schema's declared enum constants: each candidate constant's own PHP type is used to first try coercing $value to that same type, then compared strictly (===); if n…
coerceInstance($value, $options)Coerce $value into an instance of one of the declared candidate classes.
coerceInteger($value, $options)
coerceNull($value, $options)
coerceNumber($value, $options)
coerceObject($value, $options)
coerceString($value, $options)
coerceTimestamp($value, $options)
__construct($config)

Methods14

coerceArray($value, $options)

Coercer.php:77
function coerceArray(mixed $value, array $options)
$valuemixeda JSON array string, or a plain (list) array.
$optionsarray'schema' (a Schema) coerces each item's value via Schema::coerceItem.
returns
throwsif $value isn't (or doesn't decode to) a list array.

coerceBoolean($value, $options)

Coercer.php:145
function coerceBoolean(mixed $value, array $options)
$valuemixeda bool; the strings '0'/'false'/'1'/'true' (case-insensitive); the ints 0/1; or null (coerced to false).
$optionsarray
returns
throwsfor anything else.

coerceDateTime($value, $options)

Coercer.php:355
function coerceDateTime(mixed $value, array $options): DateTimeInterface
$valuemixedanything dateTime accepts (a string, timestamp, or DateTimeInterface).
$optionsarray'format': a DATE_FORMATS key, or a literal date() format string, to parse $value with. 'endOfDay': if true and the parsed time is exactly midnight (i.e. $value carried no time component), set it to 23:59:59 instead -- e.g. so a plain date used as a range's upper bound includes that whole day.
returnsDateTimeInterfacea mutable \DateTime.
throwsfor a null value.

coerceEntity($value, $options)

Coercer.php:281

Coerce $value into an iEntity.

function coerceEntity(mixed $value, array $options)
$valuemixedan array (with a 'uid' key, fetched by id via fetch and updated in place; or without one, used to build a new entity of whichever candidate class in $options['instanceOf'] best matches the array's keys against that class's required schema properties); an object already matching one of the candidate classes (returned as-is); or a string (an entity link/href, or a bare uid, resolved via fetch).
$optionsarray'instanceOf' (candidate entity classes), plus, for building a new non-embedded entity, 'repository'; for a new EmbeddedEntity, 'parent' and 'key'.
returns
throwsif $value can't be resolved to a valid entity.

coerceEnum($value, $options)

Coercer.php:186

Coerce $value to match one of the schema's declared enum constants: each candidate constant's own PHP type is used to first try coercing $value to that same type, then compared strictly (===); if no strict match is found but a string constant matches after normalization (normalize, e.g.

function coerceEnum(mixed $value, array $options)case/accent/whitespace-insensitive), that looser match is used as a fallback.
$valuemixed
$optionsarray'enum': the list of allowed constant values.
returnsthe matched enum constant (not necessarily $value itself, e.g. for a normalized string match).
throwsif $value matches none of the enum's constants.

coerceInstance($value, $options)

Coercer.php:222

Coerce $value into an instance of one of the declared candidate classes.

function coerceInstance(mixed $value, array $options)
$valuemixedan object (returned as-is if it's already an instance of one of $options['instanceOf'], or if no 'instanceOf' is given at all -- any object is accepted); an array (built into a new instance of the first non-abstract, non-enum candidate class, via create -- Create); or a string/int (matched against an enum candidate's cases, via tryEnumFrom).
$optionsarray'instanceOf': candidate class/interface names, tried in order. A candidate that has its own registered coercer (aConverter::getClassFunction) is delegated to that instead of the generic logic above.
returns
throwsif $value matches none of the candidates.

coerceInteger($value, $options)

Coercer.php:130
function coerceInteger(mixed $value, array $options)
$valuemixeda number or numeric string with no fractional part, or null (coerced to 0).
$optionsarray
returns
throwsfor a non-numeric or non-integral value.

coerceNull($value, $options)

Coercer.php:171
function coerceNull(mixed $value, array $options)
$valuemixednull, '', or the string 'null' (case-insensitive).
$optionsarray
returns
throwsfor anything else.

coerceNumber($value, $options)

Coercer.php:118
function coerceNumber(mixed $value, array $options)
$valuemixeda number or numeric string, or null (coerced to 0).
$optionsarray
returns
throwsfor a non-numeric value.

coerceObject($value, $options)

Coercer.php:50
function coerceObject(mixed $value, array $options)
$valuemixeda JSON object string, or an associative array (or empty array).
$optionsarray'schema' (a Schema) coerces each property's value via Schema::coerceProperty.
returns
throwsif $value isn't (or doesn't decode to) an associative array/empty array.

coerceString($value, $options)

Coercer.php:104
function coerceString(mixed $value, array $options)
$valuemixedany scalar, a \Stringable, or null (coerced to '').
$optionsarray
returns
throwsfor anything else (an array, or an object with no __toString()).

coerceTimestamp($value, $options)

Coercer.php:375
function coerceTimestamp(mixed $value, array $options): int
$valuemixedan int (used as-is), or anything coerceDateTime accepts.
$optionsarray
returnsinta Unix timestamp.
Inherited members (63)
ConstantsDeclared byDescription
ALERTiLogLevelAlert: Action must be taken immediately.
ALLiLogLevelMask for all
ALL_PLUSiLogLeveldebug everything
BACKTRACEiLogLevel
CRITICALiLogLevelCritical: Critical conditions.
DEBUGiLogLevelDebug: Detailed debug information.
DEEPiLogLeveldebug everything with arguments
EMERGENCYiLogLevelEmergency: System is unusable.
ERRORiLogLevelError: Runtime errors that do not require immediate action but should typically be logged and monitored.
ERROR_PLUSiLogLevelMask for all error
FATALiLogLevelMask for all fatal error
INFOiLogLevelInfo: Interesting events.
INFO_PLUSiLogLevelMask for all errors/warning/notices/info
MEMORYiLogLevelinclude backtrace object
NOTICEiLogLevelNotice: Normal but significant events.
NOTICE_PLUSiLogLevelMask for all errors/warning/notices/info
TRACEiLogLevelinclude backtrace as origins
WARNINGiLogLevelWarning: Exceptional occurrences that are not errors.
WARNING_PLUSiLogLevelMask for all errors and warnings
PropertiesDeclared byDescription
functionsaConverter
globalOptionsaConverter
verbosityaModule
MethodsDeclared byDescription
addOptions($type, $options)aConverterMerge $options into $type's existing options (via merge), rather than replacing them wholesale like setOptions.
alert($message, $context)aModuleAction must be taken immediately.
backtraces($message, $context)aModuleForce-log a message at INFO level together with a full stack trace/memory snapshot (iLogLevel::BACKTRACE), bypassing the verbosity mask entirely (any non-zero verbosity is enough to trigger it).
call($type, $value, $options)aConverterCall the function associated to a type
clear()aConverterRemove every registered type/function.
Coerce($types, $value, $options)aCoercer
Configure($config)aModuleMerge $config into this module's configuration in the shared Configuration.
critical($message, $context)aModuleCritical conditions. No-op if CRITICAL is currently filtered out by the verbosity mask.
debug($message, $context)aModuleDetailed debug information.
debugName()aModuleHuman-readable identifier used as the default {origin} in this instance's log messages.
DefaultConfig()aModuleThe config a module starts from before anything loaded via Configuration is merged in.
emergency($message, $context)aModuleSystem is unusable. No-op if EMERGENCY is currently filtered out by the verbosity mask.
error($message, $context)aModuleRuntime errors that do not require immediate action but should typically be logged and monitored.
getClassFunction($class)aConverterFind which registered type (a class or interface name) applies to $class: itself if directly registered, else the closest registered ancestor class or implemented interface.
getFunction($type)aConverter
getFunctions()aConverter
getOptions($type)aConverter
getType($value, $allowClass)aConverterNormalize $value's type into one of this converter's type names.
getVerbosity()aModule
hasType($type)aConverter
info($message, $context)aModuleInteresting events. No-op if INFO is currently filtered out by the verbosity mask.
Instantiate($config)aModule(Re)create the shared instance immediately, configured with $config merged into the class's existing config.
IsShared()aModule
log($level, $message, $context)aModuleLogs with an arbitrary level.
notice($message, $context)aModuleNormal but significant events.
offsetExists($key)aModule\ArrayAccess::offsetExists -- tells whether $key is a declared property at all, regardless of whether it is currently readable/writable through array access.
offsetGet($key)aModule\ArrayAccess::offsetGet -- reads a property directly, unless it has a #[Serialize(serialize: 'methodName')] attribute, in which case that zero-argument method is called instead (e.g.
offsetSet($key, $value)aModule\ArrayAccess::offsetSet -- sets a property directly, unless it has a #[Serialize(unserialize: 'methodName')] attribute, in which case that method is called with $value instead (e.g.
offsetUnset($key)aModule\ArrayAccess::offsetUnset -- equivalent to $module[$key] = null.
serialize($path, $serializer)aModule\MMO\core\interfaces\iSerializable::serialize -- exports every property (via
setFunction($type, $fn, $options)aConverterSet a coercer
setFunctions($fns)aConverterRegister many functions at once, e.g.
setOptions($type, $options)aConverterReplace $type's options wholesale.
setVerbosity($verb)aModuleSet the current verbosity mask, replacing whatever it was before.
Shared()aModuleGet the shared instance, creating (and configuring) it on first call.
testVerbosity($level)aModuleTest whether a given level (or set of levels) is currently enabled by this instance's verbosity mask.
traitUnshare($serialize)aModuleRemove the shared instance, so the next Shared() call creates a fresh one.
unserialize($data, $path, $instances)aModule\MMO\core\interfaces\iSerializable::unserialize -- merges $data onto DefaultConfig, runs it through the (possibly overridden) constructor, then applies each resulting value to its property (via
Unshare($serialize)aModuleLike tSingleton::Unshare, but also disconnects this module's instance from every event it was listening to (via Event::DisconnectListeners) before dropping it.
warning($message, $context)aModuleExceptional occurrences that are not errors.
__invoke($value, $options, $types)aCoercerTry each of $types in turn (via aConverter::call), returning the result of the first one that doesn't throw an eInvalid.