value = $this->formatEmbedFieldValue($value); } /** * Get the instance as an array. * * @return array */ public function toArray(): array { return [ 'name' => $this->name, 'value' => $this->value, 'inline' => $this->inline, ]; } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Format embed value to circumvent exceptions caused by empty or null values. * * @param mixed $value * @return string */ protected function formatEmbedFieldValue(mixed $value): string { // Use description for enums if (is_object($value) && enum_exists(get_class($value))) { return $value->localize(); } // Use 'Y-m-d' format for dates if ($value instanceof Carbon) { return $value->format(AllowedDateFormat::YMD->value); } // Pretty print booleans if (is_bool($value)) { return $value ? 'true' : 'false'; } // Encode to json for all other non-empty scalar values if (is_scalar($value) && Str::length($value) > 0) { return strval($value); } return self::DEFAULT_FIELD_VALUE; } }