| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Form_Response; |
| 5 |
|
| 6 |
|
| 7 |
use Jet_Form_Builder\Form_Response\Types\Response_It; |
| 8 |
|
| 9 |
class Response { |
| 10 |
|
| 11 |
private $manager; |
| 12 |
private $query_args = array(); |
| 13 |
private $args = array(); |
| 14 |
|
| 15 |
public $default_args = array( |
| 16 |
'status' => 'success', |
| 17 |
'errors' => array() |
| 18 |
); |
| 19 |
|
| 20 |
public function __construct( Response_It $instance, $query_args = array() ) { |
| 21 |
$this->manager = $instance; |
| 22 |
$this->add_query_args( $query_args ); |
| 23 |
} |
| 24 |
|
| 25 |
public function send() { |
| 26 |
$this->manager->send( $this->query_args ); |
| 27 |
} |
| 28 |
|
| 29 |
private function init_query_args() { |
| 30 |
$this->add_query_args( |
| 31 |
array( 'status' => $this->manager->parse_status( $this->args['status'] ) ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function init( array $args ) { |
| 36 |
$this->args = wp_parse_args( $args, $this->default_args ); |
| 37 |
|
| 38 |
$this->init_query_args(); |
| 39 |
$this->call_on_status(); |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
private function call_on_status() { |
| 45 |
$callable = array( $this, 'on_' . $this->args['status'] ); |
| 46 |
|
| 47 |
if ( is_callable( $callable ) ) { |
| 48 |
call_user_func( $callable ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private function add_query_args( $args ) { |
| 53 |
$this->query_args = array_merge( $this->query_args, $args ); |
| 54 |
} |
| 55 |
|
| 56 |
public function on_validation_failed() { |
| 57 |
$this->add_query_args( array( |
| 58 |
'fields' => $this->manager->get_field_errors( $this->args['errors'] ) |
| 59 |
) ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
} |