resource; return [ 'id' => $value->id, 'custom_field_valuable_type' => ModelIdentityMap::publicType($value->custom_field_valuable_type), 'custom_field_valuable_id' => $value->custom_field_valuable_id, 'type' => $value->type, 'boolean_answer' => $value->boolean_answer, 'date_answer' => $value->date_answer, 'time_answer' => $value->time_answer, 'string_answer' => $value->string_answer, 'number_answer' => $value->number_answer, 'date_time_answer' => $value->date_time_answer, 'custom_field_id' => $value->custom_field_id, 'company_id' => $value->company_id, 'default_answer' => $value->defaultAnswer, 'default_formatted_answer' => $this->dateTimeFormat(), 'custom_field' => $this->when( $value->customField()->exists(), fn () => new CustomFieldResource($value->customField) ), 'company' => $this->when( $value->company()->exists(), fn () => new CompanyResource($value->company) ), ]; } /** * The stored answer rendered the way a reader should see it. * * The field's type decides the treatment. A moment in time is fixed to a * minute in an ISO-looking layout that ignores the company's preferences * entirely; a plain date goes through the owning company's configured date * format; everything else -- text, numbers, switches, times, and any type * the mapping does not recognise -- is handed back exactly as stored. * * An answer that reads as empty short-circuits to null before any of that, * which sweeps up more than blanks: a switch turned off and a numeric zero * are both falsy, so neither ever reaches this key. Two rough edges are * preserved as they are. The column the type maps to is resolved before * the emptiness check, so a row carrying no type at all fails here rather * than returning null; and a company with no date format on file hands a * null format down to the formatter, which rejects it -- a dated answer * belonging to such a company cannot be serialised at all. */ public function dateTimeFormat() { $value = $this->resource; $column = getCustomFieldValueKey($value->type); $answer = $value->default_answer; if (! $answer) { return null; } return match ($column) { 'date_time_answer' => Carbon::parse($answer)->format('Y-m-d H:i'), 'date_answer' => Carbon::parse($answer)->format( CompanySetting::getSetting('carbon_date_format', $value->company_id) ), default => $answer, }; } }