lexal / form-submitter
Form submitter
Requires
- php: >=8.1
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-latest
- webimpress/coding-standard: ^1.3
This package is auto-updated.
Last update: 2024-10-27 00:05:13 UTC
README
With this package you can submit a form entity, e.g. save it into the database. The package can be used with the Stepped Form package on FormFinished event.
Requirements
PHP: >=8.1
Installation
Via Composer
composer require lexal/form-submitter
Usage
-
Create a Form Submitter for the specific entity. Form Submitter can return data that will be passed back where
submit
method has been called.use Lexal\FormSubmitter\FormSubmitterInterface; final class CustomerFormSubmitter implements FormSubmitterInterface { public function supportsSubmitting(mixed $entity): bool { return $entity instanceof Customer; } public function submit(mixed $entity): mixed { // save entity to the database return $entity; } }
-
Use Form Submitter in your application.
$entity = new Customer(); $formSubmitter = new CustomerFormSubmitter(); if ($formSubmitter->supportsSubmitting($entity)) { $formSubmitter->submit($entity); }
You can use the following build-in Form Submitters:
-
FormSubmitter
- contains array of submitters and submit a form entity to the first one that supports submitting.use Lexal\FormSubmitter\FormSubmitter; $formSubmitter = new FormSubmitter( new CustomerFormSubmitter(), ); $formSubmitter->submit(new Customer());
-
TransactionalFormSubmitter
- submits a form entity in the transaction (e.g. database transaction).use Lexal\FormSubmitter\FormSubmitter; use Lexal\FormSubmitter\Transaction\TransactionInterface; use Lexal\FormSubmitter\TransactionalFormSubmitter; final class DatabaseTransaction implements TransactionInterface { public function start(): void { // start transaction } public function commit(): void { // commit transaction } public function rollback(): void { // rollback transaction } } $submitter = new TransactionalFormSubmitter( new FormSubmitter(new CustomerFormSubmitter()), new DatabaseTransaction(), ); $submitter->submit(new Customer());
License
Form Submitter is licensed under the MIT License. See LICENSE for the full license text.