$values Stored values keyed by field key. * @param array $fields Fields of the form, in display order. * @param ValueFormatter $formatter Decoder for stored values. */ public function __construct( private Form $form, private int $submission_id, private array $values, private array $fields, private ValueFormatter $formatter ) {} /** * Returns the form. */ public function form(): Form { return $this->form; } /** * Returns the submission ID. * * @return int The ID, 0 for a test delivery. */ public function submission_id(): int { return $this->submission_id; } /** * Returns the fields of the form. * * @return array The fields. */ public function fields(): array { return $this->fields; } /** * Returns one answer as readable text. * * @param string $key Field key. * * @return string The answer, or an empty string when the field was not answered. */ public function value( string $key ): string { if ( '' === $key || ! array_key_exists( $key, $this->values ) ) { return ''; } $raw = $this->values[ $key ]; return $this->formatter->raw_to_text( is_scalar( $raw ) ? (string) $raw : (string) wp_json_encode( $raw ) ); } /** * Returns an answer only when it is a usable email address. * * @param string $key Field key. * * @return string The address, or an empty string. */ public function email( string $key ): string { $value = trim( $this->value( $key ) ); return is_email( $value ) ? $value : ''; } /** * Returns every answer as text, keyed by field key. * * @return array Answers keyed by field key. */ public function all(): array { $values = array(); foreach ( array_keys( $this->values ) as $key ) { $values[ (string) $key ] = $this->value( (string) $key ); } return $values; } /** * Returns every answer as text, keyed by the label the visitor saw. * * @return array Answers keyed by label. */ public function labelled(): array { $values = array(); foreach ( $this->fields as $field ) { if ( ! $field instanceof Field ) { continue; } $key = $field->field_key(); if ( ! array_key_exists( $key, $this->values ) ) { continue; } $label = '' !== $field->label() ? $field->label() : $key; $values[ $label ] = $this->value( $key ); } return $values; } /** * Returns the link to the stored entry. * * @return string The admin URL, or an empty string for a test delivery. */ public function entry_url(): string { if ( $this->submission_id <= 0 ) { return ''; } return add_query_arg( array( 'page' => 'formpilot-entries', 'entry' => $this->submission_id, ), admin_url( 'admin.php' ) ); } }