builder.php
3 years ago
manager.php
3 years ago
msg-router.php
3 years ago
status-info.php
3 years ago
status-info.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Messages; |
| 5 | |
| 6 | |
| 7 | class Status_Info { |
| 8 | |
| 9 | private $raw = ''; |
| 10 | private $is_dynamic; |
| 11 | private $is_success; |
| 12 | private $parsed; |
| 13 | |
| 14 | public function __construct( string $status ) { |
| 15 | $this->raw = $status; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @return string |
| 20 | */ |
| 21 | public function get_raw_message(): string { |
| 22 | return $this->raw; |
| 23 | } |
| 24 | |
| 25 | public function is_dynamic(): bool { |
| 26 | if ( is_null( $this->is_dynamic ) ) { |
| 27 | $this->set_dynamic_and_success(); |
| 28 | } |
| 29 | |
| 30 | return $this->is_dynamic; |
| 31 | } |
| 32 | |
| 33 | public function is_success(): bool { |
| 34 | if ( is_null( $this->is_success ) ) { |
| 35 | $this->set_dynamic_and_success(); |
| 36 | } |
| 37 | |
| 38 | return $this->is_success; |
| 39 | } |
| 40 | |
| 41 | public function get_parsed(): array { |
| 42 | if ( ! is_array( $this->parsed ) ) { |
| 43 | $this->parsed = Manager::parse_message( $this->raw ); |
| 44 | } |
| 45 | |
| 46 | return $this->parsed; |
| 47 | } |
| 48 | |
| 49 | public function get_message(): string { |
| 50 | $parsed = $this->get_parsed(); |
| 51 | |
| 52 | if ( $this->is_dynamic() ) { |
| 53 | return $parsed[1] ?? $parsed[0]; |
| 54 | } |
| 55 | |
| 56 | return $parsed[0]; |
| 57 | } |
| 58 | |
| 59 | private function set_dynamic_and_success(): Status_Info { |
| 60 | $dynamic = Manager::dynamic_types(); |
| 61 | $message = $this->get_parsed(); |
| 62 | $this->is_dynamic = isset( $dynamic[ $message[0] ] ) && ! empty( $message[1] ); |
| 63 | |
| 64 | if ( $this->is_dynamic ) { |
| 65 | $this->is_success = ( 'success' === ( $dynamic[ $message[0] ]['type'] ?? '' ) ); |
| 66 | } else { |
| 67 | $this->is_success = in_array( $message[0], array( 'success' ), true ); |
| 68 | } |
| 69 | |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | public function get_css_class(): string { |
| 75 | return $this->is_success() ? 'success' : 'error'; |
| 76 | } |
| 77 | |
| 78 | } |