xakepehok / scalarizer
Salarizer can convert value-objects to scalar values
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/xakepehok/scalarizer
Requires
- php: >=7.1.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: 7.5
This package is auto-updated.
Last update: 2025-10-05 23:48:45 UTC
README
\XAKEPEHOK\Scalarizer\Scalarizer
- component, that can help you extract scalar value from value-objects. Scalarizer
can accept anytype input values and try return single scalar value
Usage
Scalarizer constructor accept two arguments:
- associative array, where every key is a classname, and value is a callable, that can convert class to scalar value
- check class by reflection and try to return scalar value, if class has only one property
<?php $scalarizer = new \XAKEPEHOK\Scalarizer\Scalarizer( [DateTimeInterface::class => function (DateTimeInterface $dateTime) { return $dateTime->format('Y-m-d H:i:s'); }], true //use reflection ); echo $scalarizer->scalarize($someValue);
Scalarisation methods priority
- Check for already scalar or null values as input
- Scalarize by rules (array of callable)
- Scalarize object by \JsonSerializable::jsonSerialize() (if implemented)
- Scalarize object by Class::__toString() method (if implemented)
- Scalarize by reflection. It return some value if class has only one property