Skip to content

Commit

Permalink
Refactor Form::get_instance into FormFactory class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtashjian committed Jul 30, 2024
1 parent b3f5090 commit 7488574
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 34 deletions.
63 changes: 30 additions & 33 deletions includes/Plugin/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
use OmniForm\BlockLibrary\Blocks\Fieldset;
use OmniForm\Dependencies\Dflydev\DotAccessData;
use OmniForm\Dependencies\Respect\Validation;
use OmniForm\Exceptions\FormNotFoundException;
use OmniForm\Exceptions\InvalidFormIdException;

/**
* The Form class.
*/
class Form {
/**
* Form content.
*
* @var string
*/
protected $content;

/**
* Form ID.
*
Expand Down Expand Up @@ -54,40 +59,32 @@ class Form {
protected $groups = array();

/**
* Retrieve Form instance.
*
* @param int $form_id Form ID.
*
* @return Form Form object.
* Form constructor.
*
* @throws InvalidFormIdException If the form ID is invalid.
* @throws FormNotFoundException If the form is not found.
* @param Validation\Validator $validator Validator object.
*/
public function get_instance( $form_id ) {
$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 ) )
);
}

$this->id = $_form->ID;
$this->post_data = $_form;
public function __construct( Validation\Validator $validator ) {
$this->validator = $validator;
}

$this->validator = new Validation\Validator();
/**
* Set the form's content.
*
* @param string $content The form content.
*/
public function set_content( $content ) {
$this->content = $content;
}

return $this;
/**
* Set the form's post data.
*
* @param \WP_Post $post_data The form post data.
*/
public function set_post_data( \WP_Post $post_data ) {
$this->id = $post_data->ID;
$this->post_data = $post_data;
$this->content = $post_data->post_content;
}

/**
Expand Down Expand Up @@ -132,7 +129,7 @@ public function get_title() {
* @return string
*/
public function get_content() {
return $this->post_data->post_content;
return $this->content;
}

/**
Expand Down
92 changes: 92 additions & 0 deletions includes/Plugin/FormFactory.php
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. &#8220;%s&#8221; 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 &#8220;%d&#8221; does not exist.', 'omniform' ), $form_id ) )
);
}

$form = $this->container->getNew( Form::class );
$form->set_post_data( $_form );

return $form;
}
}
21 changes: 20 additions & 1 deletion includes/Plugin/PluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OmniForm\Analytics\AnalyticsManager;
use OmniForm\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use OmniForm\Dependencies\League\Container\ServiceProvider\BootableServiceProviderInterface;
use OmniForm\Dependencies\Respect\Validation;
use OmniForm\Plugin\Facades\DB;
use OmniForm\Plugin\Support\Number;

Expand All @@ -27,6 +28,7 @@ class PluginServiceProvider extends AbstractServiceProvider implements BootableS
public function provides( string $id ): bool {
$services = array(
Form::class,
FormFactory::class,
QueryBuilder::class,
QueryBuilderFactory::class,
);
Expand All @@ -40,7 +42,24 @@ public function provides( string $id ): bool {
* @return void
*/
public function register(): void {
$this->getContainer()->addShared( Form::class );
$this->getContainer()->addShared(
Form::class,
function () {
return new Form(
new Validation\Validator()
);
}
);

$this->getContainer()->add(
FormFactory::class,
function () {
return new FormFactory(
$this->getContainer(),
new Validation\Validator()
);
}
);

$this->getContainer()->add(
QueryBuilder::class,
Expand Down

0 comments on commit 7488574

Please sign in to comment.