Index

aConverter

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

class

aConverter

aConverter.php:25

Class used to convert a value from one type to another.

abstract class MMO\core\classes\aConverter extends MMO\core\classes\aModule implements MMO\core\interfaces\iConverter
Namespace
MMO\core\classes
Extends
aModule
Implements
iConverter
Any converter MUST throw an eInvalid|InvalidArgumentException exception if the value cannot be converted A "type" here is either one of [`getType`](https://mmoframework.fr/documentation/mmo.core/php/classes/aConverter.md#aconverter-gettype)'s normalized type names ('integer', 'number', 'string', 'boolean', 'array', 'object', 'null', 'enum', 'instance'), or a class/interface name (matched by [`getClassFunction`](https://mmoframework.fr/documentation/mmo.core/php/classes/aConverter.md#aconverter-getclassfunction), which also matches subclasses/implementations, e.g. a function registered for `\DateTime::class` is found for a `MyDate extends \DateTime` instance too).
class MyConverter extends aConverter { public function __invoke(mixed $value, array $options = []): mixed {return $this->call($this->getType($value), $value, $options);} } $c = MyConverter::Shared(); $c->setFunction('integer', fn($v, $opts) => (int)round($v * ($opts['scale']??1)), ['scale' => 1]); $c($value); // e.g. converts an int/float/string $value to an int, scaled per the registered options

Index

cachedClassFunctions
functions
globalOptions
addOptions($type, $options)Merge $options into $type's existing options (via merge), rather than replacing them wholesale like setOptions.
call($type, $value, $options)Call the function associated to a type
clear()Remove every registered type/function.
getClassFunction($class)Find 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)
getFunctions()
getOptions($type)
getType($value, $allowClass)Normalize $value's type into one of this converter's type names.
hasType($type)
setFunction($type, $fn, $options)Set a coercer
setFunctions($fns)Register many functions at once, e.g.
setOptions($type, $options)Replace $type's options wholesale.

Properties3

cachedClassFunctions

private array $cachedClassFunctions = []
Type
array

functions

#[MMO\core\attributes\Serialize(serialize: false, unserialize: 'setFunctions')] protected array $functions = []
Type
array

globalOptions

array $globalOptions = []
Type
array

Methods12

addOptions($type, $options)

aConverter.php:209

Merge $options into $type's existing options (via merge), rather than replacing them wholesale like setOptions.

function addOptions(string $type, array $options): void
$typestring
$optionsarray
returnsvoid
throwsif $type is not registered

call($type, $value, $options)

aConverter.php:242

Call the function associated to a type

function call(string $type, mixed $value, array $options = []): mixed
See
$globalOptions (lowest priority)
$typestring
$valuemixed
$options = []arraymerged with $type's registered options (these take priority) and with
returnsmixedwhatever the registered function returns
throwsif $type is not registered
$c->setFunction('integer', fn($v, $opts) => (int)round($v * ($opts['scale']??1)), ['scale' => 1]); $c->call('integer', 3.7); // 4 -- uses the registered options (scale: 1) $c->call('integer', 3.7, ['scale' => 10]); // 37 -- $options overrides the registered scale

getClassFunction($class)

aConverter.php:60

Find which registered type (a class or interface name) applies to $class: itself if directly registered, else the closest registered ancestor class or implemented interface. Results are cached per $class.

function getClassFunction(string $class): ?string
$classstring
returnsnull | stringthe matching registered type name, or null if none of $class's ancestors/interfaces are registered
$c->setFunction(\DateTime::class, fn($v, $o) => $v->format('Y')); $c->getClassFunction(MyDateSubclass::class); // \DateTime::class, since MyDateSubclass extends \DateTime $c->getClassFunction(\DateTimeImmutable::class); // null: it does NOT extend \DateTime

getFunction($type)

aConverter.php:46
function getFunction(string $type): ?callable
$typestring
returnsnull | callablethe raw function registered for $type (without its options -- see call to invoke it correctly), or null if $type is not registered

getFunctions()

aConverter.php:122
function getFunctions(): array
returnsarraytype => [callable, options] for every registered type

getOptions($type)

aConverter.php:197
function getOptions(string $type): ?array
$typestring
returnsnull | arraythe options currently registered for $type, or null if $type is not registered or has no options

getType($value, $allowClass)

aConverter.php:94

Normalize $value's type into one of this converter's type names.

function getType(mixed $value, bool $allowClass = true): string
$valuemixed
$allowClass = trueboolif true (the default) and $value is an object whose class matches a registered type (via getClassFunction), that registered type name is returned instead of the generic 'instance'/'enum'
returnsstringone of: 'integer', 'number' (float), 'string', 'boolean', 'null', 'array' (a list), 'object' (an associative array), 'resource', 'unknown', 'enum', 'instance', or a registered class/interface name
$c->getType(5); // 'integer' $c->getType(5.5); // 'number' $c->getType([1, 2]); // 'array' -- a list $c->getType(['a'=>1]); // 'object' -- associative $c->getType(new \DateTime()); // 'instance', unless \DateTime (or an ancestor/interface) is a registered type

hasType($type)

aConverter.php:37
function hasType(string $type): bool
$typestring
returnsbooltrue if a conversion function is registered for $type

setFunction($type, $fn, $options)

aConverter.php:160

Set a coercer

function setFunction(string $type, ?callable $fn, ?array $options = null): bool
$typestring
$fnnull | callablefunction($value, $options). if null remove function
$options = nullnull | arrayoptions to pass to the coercer when called. if null, leave the previous options as they were.
returnsbooltrue if function or options have changed
$c->setFunction('integer', fn($v, $opts) => (int)round($v), []);$c->setFunction('integer', null); // removes the 'integer' converter entirely

setFunctions($fns)

aConverter.php:132

Register many functions at once, e.g. ['integer' => [$fn, ['scale' => 1]], 'string' => $fn2].

function setFunctions(array $fns): bool
$fnsarraytype => callable, or type => [callable, options]
returnsbooltrue if any registration actually changed something (see setFunction)
throwsif any entry is not a valid callable

setOptions($type, $options)

aConverter.php:221

Replace $type's options wholesale.

function setOptions(string $type, ?array $options): void
$typestring
$optionsnull | arraynull removes the options entirely
returnsvoid
throwsif $type is not registered
Inherited members (49)
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
verbosityaModule
MethodsDeclared byDescription
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).
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.
getVerbosity()aModule
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
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.
__construct($config)aModuleprotected constructor, so it is not possible to do new Module(...) called by the serialization process just before being injected with data.
__invoke($value, $options)iConverterConvert any value to a given type