*/ private array $titles = array(); /** * Resolved label maps, keyed by form ID. * * @var array> */ private array $labels = array(); /** * Constructor. * * @param FormRepository $forms Repository reading forms. * @param FieldRepository $fields Repository reading fields. */ public function __construct( private FormRepository $forms, private FieldRepository $fields ) {} /** * Returns the title of a form. * * @param int $form_id Form ID. * * @return string The title, or an empty string when the form is gone. */ public function title( int $form_id ): string { if ( ! isset( $this->titles[ $form_id ] ) ) { $form = $this->forms->find( $form_id ); $this->titles[ $form_id ] = $form instanceof Form ? $form->title() : ''; } return $this->titles[ $form_id ]; } /** * Returns the field labels of a form, keyed by field key. * * A field with no label falls back to its key, so a column never renders * blank. * * @param int $form_id Form ID. * * @return array Labels keyed by field key. */ public function field_labels( int $form_id ): array { if ( isset( $this->labels[ $form_id ] ) ) { return $this->labels[ $form_id ]; } $labels = array(); foreach ( $this->fields->for_form( $form_id ) as $field ) { // A page break is a marker in the field list, never an answer, so it // must not become a column in the entry list or the export. if ( FieldTypes::STEP === $field->type() ) { continue; } $labels[ $field->field_key() ] = '' !== $field->label() ? $field->label() : $field->field_key(); } $this->labels[ $form_id ] = $labels; return $labels; } /** * Forgets everything resolved so far. */ public function flush(): void { $this->titles = array(); $this->labels = array(); } }