GitHunt
MR

mrceperka/object-mapper

Raw data mapping to validated objects

Orisai
Object Mapper

Raw data mapping to validated objects

Ideal for validation of POST data, configurations, serialized and any other raw data and automatic mapping of them to type-safe objects.

๐Ÿ“„ Check out our documentation.

๐Ÿ’ธ If you like Orisai, please make a donation. Thank you!

use Orisai\ObjectMapper\MappedObject;
use Orisai\ObjectMapper\Attributes\Expect\MappedObjectValue;
use Orisai\ObjectMapper\Attributes\Expect\StringValue;

final class UserInput extends MappedObject
{

	/** @StringValue(notEmpty=true) */
	public string $firstName;

	/** @StringValue(notEmpty=true) */
	public string $lastName;

	/** @MappedObjectValue(UserAddressInput::class) */
	public UserAddressInput $address;

}
use Orisai\ObjectMapper\MappedObject;

final class UserAddressInput extends MappedObject
{
	// ...
}
use Orisai\ObjectMapper\Exception\InvalidData;
use Orisai\ObjectMapper\Printers\ErrorVisualPrinter;
use Orisai\ObjectMapper\Printers\TypeToStringConverter;
use Orisai\ObjectMapper\Processing\DefaultProcessor;

$processor = new DefaultProcessor(...);
$errorPrinter = new ErrorVisualPrinter(new TypeToStringConverter());

$data = [
	'firstName' => 'Tony',
	'lastName' => 'Stark',
	'address' => [],
];

try {
	$user = $processor->process($data, UserInput::class);
} catch (InvalidData $exception) {
	$error = $errorPrinter->printError($exception);
	throw new Exception("Validation failed due to following error:\n$error");
}

echo "User name is: {$user->firstName} {$user->lastName}";

Languages

PHP99.5%Makefile0.5%

Contributors

Mozilla Public License 2.0
Created July 17, 2021
Updated August 27, 2022
mrceperka/object-mapper | GitHunt