orisai / nette-object-mapper
Nette 对象映射器集成 Orisai
0.1.1
2024-06-21 22:22 UTC
Requires
- php: 7.4 - 8.3
- nette/caching: ^3.1.0
- nette/di: ^3.1.0
- orisai/exceptions: ^1.0.0
- orisai/nette-di: ^1.2.0
- orisai/object-mapper: ^0.1.0|^0.2.0
Requires (Dev)
- brianium/paratest: ^6.3.0
- doctrine/annotations: ^1.12.1|^2.0.0
- infection/infection: ^0.26.0|^0.27.0|^0.28.0|^0.29.0
- orisai/coding-standard: ^3.0.0
- phpstan/extension-installer: ^1.0.0
- phpstan/phpstan: ^1.0.0
- phpstan/phpstan-deprecation-rules: ^1.0.0
- phpstan/phpstan-phpunit: ^1.0.0
- phpstan/phpstan-strict-rules: ^1.0.0
- phpunit/phpunit: ^9.5.0
- staabm/annotate-pull-request-from-checkstyle: ^1.7.0
README
Nette 对象映射器
Nette 对象映射器集成 Orisai
📄 查看我们的 文档.
💸 如果您喜欢 Orisai,请 捐赠。谢谢!
use Orisai\ObjectMapper\MappedObject; use Orisai\ObjectMapper\Rules\MappedObjectValue; use Orisai\ObjectMapper\Rules\StringValue; final class UserInput implements MappedObject { /** @StringValue(notEmpty=true) */ public string $firstName; /** @StringValue(notEmpty=true) */ public string $lastName; /** @MappedObjectValue(UserAddressInput::class) */ public UserAddressInput $address; }
use Orisai\ObjectMapper\MappedObject; use Orisai\ObjectMapper\Rules\StringValue; final class UserAddressInput implements MappedObject { /** @StringValue(notEmpty=true) */ public string $street; // ... }
use Orisai\ObjectMapper\Exception\InvalidData; use Orisai\ObjectMapper\Printers\ErrorVisualPrinter; use Orisai\ObjectMapper\Printers\TypeToStringConverter; use Orisai\ObjectMapper\Processing\Processor; $processor = $container->getByType(Processor::class); $errorPrinter = new ErrorVisualPrinter(new TypeToStringConverter()); $data = [ 'firstName' => 'Tony', 'lastName' => 'Stark', 'address' => [ 'street' => '10880 Malibu Point', ], ]; 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}";