Skip to content

Commit

Permalink
Handle inbound birthday date parsing in StringUtilities only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Oct 31, 2025
1 parent 9830997 commit 70e91bd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,13 @@ protected function resultsToEntityData(
'valid_through' => $result->valid_through
];

if(!empty($result->date_of_birth) && empty($eidata['date_of_birth'])) {
if(empty($eidata['date_of_birth'])) {
// We take the first DoB we see. Multiple rows should have the same
// DoB, if they don't that's a problem in the data that needs to be fixed.

// We have to convert the DateTime back to a string
$eidata['date_of_birth'] = $result->date_of_birth->format('Y-m-d');
$dob = StringUtilities::handleInboundDateOfBirth($result->date_of_birth);
if (!empty($dob)) {
$eidata['date_of_birth'] = $dob;
}
}

// MVEAs that have a foreign key to EIR get attached to the EIR
Expand Down
29 changes: 29 additions & 0 deletions app/src/Lib/Util/StringUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,35 @@ public static function foreignKeyToController(string $s): string {
return Inflector::underscore(Inflector::pluralize(substr($s, 0, strlen($s)-3)));
}

/**
* Handles date of birth conversion from various input formats.
*
* @param string|DateTimeImmutable $dateOfBirth Date of birth as string or DateTimeImmutable
* @return \string|null Date of birth as string or null if invalid/empty
* @since COmanage Registry v5.2.0
*/
public static function handleInboundDateOfBirth(mixed $dateOfBirth): ?string
{
if(!empty($dateOfBirth)) {
return match(true) {
is_string($dateOfBirth) => (static function ($dateOfBirth) {
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $dateOfBirth);
if ($dob === false) {
$dob = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dateOfBirth);
}
return $dob ? $dob->format('Y-m-d') : null;
})($dateOfBirth),
$dateOfBirth instanceof \DateTimeImmutable => $dateOfBirth->format('Y-m-d'),
default => (static function ($dateOfBirth) {
$this->llog('warning', __d('error', 'dob.invalid', [$dateOfBirth]));;
return null;
})($dateOfBirth)
};
}

return null;
}

/**
* Localize a controller name, accounting for plugins.
*
Expand Down
11 changes: 3 additions & 8 deletions app/src/Model/Table/PeopleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace App\Model\Table;

use App\Lib\Util\StringUtilities;
use App\Model\Entity\Person;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
Expand Down Expand Up @@ -777,14 +778,8 @@ public function saveAttributeCollectorPetitionAttributes(int $personId, array $f
foreach ($fields as $field) {
$attribute = $field->enrollment_attribute->attribute;
$value = $field->value;
if($attribute === 'date_of_birth' && is_string($value)) {
// While we're here, make sure it's in YYYY-MM-DD format. This should fail
// if the inbound attribute is invalid.
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $value);

if($dob) {
$value = $dob->format('Y-m-d');
}
if($attribute === 'date_of_birth') {
$value = StringUtilities::handleInboundDateOfBirth($value);
}
$person->$attribute = $value;
}
Expand Down
11 changes: 3 additions & 8 deletions app/src/Model/Table/PipelinesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,15 +898,10 @@ protected function mapAttributesToCO(
// Make sure source_key is a string
'source_key' => (string)$eisAttributes['source_key']
];

if(!empty($eisAttributes['date_of_birth'])) {
// While we're here, make sure it's in YYYY-MM-DD format. This should fail
// if the inbound attribute is invalid.
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $eisAttributes['date_of_birth']);

if($dob) {
$ret['date_of_birth'] = $dob->format('Y-m-d');
}
$dob = StringUtilities::handleInboundDateOfBirth($eisAttributes['date_of_birth']);
if($dob) {
$ret['date_of_birth'] = $dob;
}

foreach([
Expand Down

0 comments on commit 70e91bd

Please sign in to comment.