constructor<T> static method
Trying to consolidate dynamic constructors into a single factory, that has now turned into a static method, but functions the same way. The input can be a String, a dart DateTime, a FhirDateTimeBase, or an other.
Implementation
static FhirDateTimeBase constructor<T>(
dynamic inValue, [
DateTimePrecision? precision,
]) {
String? input;
String? exception;
Map<String, int?>? dateTimeMap;
bool regexpValid = true;
if (inValue is String) {
input = _cleanInput(inValue);
final String replaced = input.replaceAll(
T == FhirDateTime
? dateTimeExp
: T == FhirInstant
? instantExp
: dateExp,
'');
if (replaced.isNotEmpty &&
(replaced != 'T' || (T == FhirDate && replaced == 'T')) &&
replaced.trim().isNotEmpty) {
regexpValid = false;
}
} else if (inValue is DateTime) {
input = inValue.toIso8601String();
if (inValue.isUtc) {
input += 'Z';
} else if (inValue.timeZoneOffset.inHours != 0) {
input += timeZoneOffsetToString(inValue.timeZoneOffset.inHours);
} else {
precision ??= DateTimePrecision.yyyy_MM_dd_T_HH_mm_ss_SSS;
}
} else if (inValue is FhirDateTimeBase) {
return constructor<T>(inValue.input, precision);
} else {
exception =
"$T cannot be constructed from '$inValue' (unsupported type).";
}
String? output;
if (input != null) {
dateTimeMap = formatDateTimeString<T>(input);
}
if (dateTimeMap != null) {
if (precision == null) {
precision = precisionFromMap(dateTimeMap);
} else {
output = precision.dateTimeMapToString<T>(dateTimeMap);
}
}
return _constructor<T>(
dateTimeMap, precision, exception, output ?? inValue, regexpValid);
}