1. Go to this page and download the library: Download shipmonk/input-mapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shipmonk / input-mapper example snippets
use ShipMonk\InputMapper\Compiler\Mapper\Optional;
class Person
{
public function __construct(
public readonly string $name,
public readonly int $age,
#[Optional]
public readonly ?string $email,
/** @var list<string> */
public readonly array $hobbies,
/** @var list<self> */
#[Optional(default: [])]
public readonly array $friends,
) {}
}
$tempDir = sys_get_temp_dir() . '/input-mapper';
$autoRefresh = true; // MUST be set to false in production
$mapperProvider = new ShipMonk\InputMapper\Runtime\MapperProvider($tempDir, $autoRefresh);
$mapper = $mapperProvider->get(Person::class);
try {
$person = $mapper->map([
'name' => 'John',
'age' => 30,
'hobbies' => ['hiking', 'reading'],
'friends' => [
[
'name' => 'Jane',
'age' => 28,
'hobbies' => ['hiking', 'reading'],
],
[
'name' => 'Jack',
'age' => 28,
'hobbies' => ['hiking', 'reading'],
],
],
]);
} catch (\ShipMonk\InputMapper\Runtime\Exception\MappingFailedException $e) {
// $e->getMessage() // programmer readable error message in English
// $e->getPath() // path of the problematic field for example ['friends', 0, 'name']
// ...
}
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertIntRange;
class Person
{
public function __construct(
public readonly string $name,
#[AssertIntRange(gte: 18, lte: 99)]
public readonly int $age,
) {}
}
use ShipMonk\InputMapper\Compiler\Mapper\Object\SourceKey;
class Person
{
public function __construct(
#[SourceKey('full_name')]
public readonly string $name,
) {}
}
use ShipMonk\InputMapper\Compiler\Mapper\Object\Discriminator;
#[Discriminator(
key: 'type', // key to use for mapping
mapping: [
'car' => Car::class,
'truck' => Truck::class,
]
)]
abstract class Vehicle {
public function __construct(
public readonly string $type,
) {}
}
class Car extends Vehicle {
public function __construct(
string $type,
public readonly string $color,
) {
parent::__construct($type);
}
}
class Truck extends Vehicle {
public function __construct(
string $type,
public readonly string $color,
) {
parent::__construct($type);
}
}
use ShipMonk\InputMapper\Compiler\Mapper\Object\Discriminator;
enum VehicleType: string {
case Car = 'car';
case Truck = 'truck';
}
#[Discriminator(
key: 'type', // key to use for mapping
mapping: [
VehicleType::Car->value => Car::class,
VehicleType::Truck->value => Truck::class,
]
)]
abstract class Vehicle {
public function __construct(
VehicleType $type,
) {}
}
class Car extends Vehicle {
public function __construct(
VehicleType $type,
public readonly string $color,
) {
parent::__construct($type);
}
}
class Truck extends Vehicle {
public function __construct(
VehicleType $type,
public readonly string $color,
) {
parent::__construct($type);
}
}
class MyCustomMapper implements ShipMonk\InputMapper\Runtime\Mapper
{
public function map(mixed $data, array $path = []): mixed
{
return MyCustomClass::createFrom($data);
}
}
$mapperProvider->registerFactory(MyCustomClass::class, function () {
return new MyCustomMapper();
});
$mapperProvider = new ShipMonk\InputMapper\Runtime\MapperProvider(
tempDir: $tempDir,
autoRefresh: $autoRefresh,
mapperCompilerFactoryProvider: new MyCustomMapperCompilerFactoryProvider(),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.