generated from jrtashjian/pluginwp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Form::get_instance into FormFactory class
- Loading branch information
1 parent
b3f5090
commit 7488574
Showing
3 changed files
with
142 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* The FormFactory class. | ||
* | ||
* @package OmniForm | ||
*/ | ||
|
||
namespace OmniForm\Plugin; | ||
|
||
use OmniForm\Dependencies\League\Container\Container; | ||
use OmniForm\Dependencies\Respect\Validation\Validator; | ||
use OmniForm\Exceptions\FormNotFoundException; | ||
use OmniForm\Exceptions\InvalidFormIdException; | ||
|
||
/** | ||
* The FormFactory class. | ||
*/ | ||
class FormFactory { | ||
/** | ||
* The Container object. | ||
* | ||
* @var Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* The Validator object. | ||
* | ||
* @var Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* The FormFactory constructor. | ||
* | ||
* @param Container $container The Container object. | ||
* @param Validator $validator The Validator object. | ||
*/ | ||
public function __construct( Container $container, Validator $validator ) { | ||
$this->container = $container; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* Create a new instance of the Form class. | ||
* | ||
* @param string $form_content The form content. | ||
* | ||
* @return Form The newly created Form instance. | ||
*/ | ||
public function create_with_content( $form_content ): Form { | ||
$form = $this->container->getNew( Form::class ); | ||
$form->set_content( $form_content ); | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* Create a new instance of the Form class. | ||
* | ||
* @param int $form_id The form ID. | ||
* | ||
* @return Form The newly created Form instance. | ||
* | ||
* @throws FormNotFoundException If the form does not exist. | ||
* @throws InvalidFormIdException If the form ID is invalid. | ||
*/ | ||
public function create_with_id( $form_id ): Form { | ||
$form_id = (int) $form_id; | ||
|
||
if ( ! $form_id ) { | ||
throw new InvalidFormIdException( | ||
/* translators: %d: Form ID. */ | ||
esc_attr( sprintf( __( 'Form ID must be an integer. “%s” is not a valid integer.', 'omniform' ), $form_id ) ) | ||
); | ||
} | ||
|
||
$_form = get_post( $form_id ); | ||
|
||
if ( ! $_form || 'omniform' !== $_form->post_type ) { | ||
throw new FormNotFoundException( | ||
/* translators: %d: Form ID. */ | ||
esc_attr( sprintf( __( 'Form ID “%d” does not exist.', 'omniform' ), $form_id ) ) | ||
); | ||
} | ||
|
||
$form = $this->container->getNew( Form::class ); | ||
$form->set_post_data( $_form ); | ||
|
||
return $form; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters