$value ) { if ( property_exists( $this, $key ) ) { $this->$key = $value; } } // Decode JSON fields. if ( is_string( $this->draft_data ) ) { $this->draft_data = json_decode( $this->draft_data, true ); } if ( is_string( $this->files_data ) ) { $this->files_data = json_decode( $this->files_data, true ); } } /** * Convert to array for database * * @return array Database-ready data. */ public function to_array() { return array( 'user_id' => $this->user_id, 'form_id' => $this->form_id, 'post_id' => $this->post_id, 'draft_data' => is_array( $this->draft_data ) ? wp_json_encode( $this->draft_data ) : $this->draft_data, 'files_data' => is_array( $this->files_data ) ? wp_json_encode( $this->files_data ) : $this->files_data, ); } /** * Validate model data * * @return bool|array True if valid, array of errors otherwise. */ public function validate() { $errors = array(); if ( empty( $this->user_id ) ) { $errors[] = __( 'User ID is required.', 'airy-frontend-forms' ); } if ( empty( $this->form_id ) ) { $errors[] = __( 'Form ID is required.', 'airy-frontend-forms' ); } if ( empty( $this->draft_data ) ) { $errors[] = __( 'Draft data is required.', 'airy-frontend-forms' ); } return empty( $errors ) ? true : $errors; } }