diff --git a/docroot/modules/custom/va_gov_batch/src/cbo_scripts/RemoveNonNumericalCharactersFromExtensions.php b/docroot/modules/custom/va_gov_batch/src/cbo_scripts/RemoveNonNumericalCharactersFromExtensions.php index 7e17af32bc..d04c3907b9 100644 --- a/docroot/modules/custom/va_gov_batch/src/cbo_scripts/RemoveNonNumericalCharactersFromExtensions.php +++ b/docroot/modules/custom/va_gov_batch/src/cbo_scripts/RemoveNonNumericalCharactersFromExtensions.php @@ -72,16 +72,21 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { if (empty($original_extension)) { return "No extension found for paragraph id $item"; } + $phone_parent_field_name = $phone_paragraph->get('parent_field_name')->value; + // Don't try to change 'field_phone_numbers_paragraph'. + if ($phone_parent_field_name === 'field_phone_numbers_paragraph') { + return "The phone data from 'field_phone_numbers_paragraph' '$item' on 'health_care_local_health_service' has already been migrated to the Service location paragraph previously. This is a vestigial field that is unused."; + } $number_only_extension = $this->replaceNonNumerals($original_extension); if ($original_extension === $number_only_extension) { - return "No change to extension $original_extension in paragraph id $item"; + return "No change to extension '$original_extension' in paragraph id $item"; } $phone_paragraph->set(name: 'field_phone_extension', value: $number_only_extension); $phone_paragraph->save(); return "Extension updated for paragraph id $item from '$original_extension' to '$number_only_extension'"; } catch (\Exception $e) { - $message = "Exception during update of paragraph id $item with extension: $original_extension"; + $message = "Exception during update of paragraph id $item with extension: '$original_extension'"; return $message; } @@ -98,7 +103,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { */ public static function replaceNonNumerals(string $extension): string { - $pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/'; + $pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i'; // If the extension contains two numbers separate numbers, don't change it. if (preg_match($pattern_to_ignore, $extension) > 0) { @@ -106,7 +111,7 @@ public static function replaceNonNumerals(string $extension): string { } // Remove non-numerical characters from the extension. - $just_numbers = preg_replace('/[^0-9]/', '', $extension); + $just_numbers = trim(preg_replace('/[^0-9]/', '', $extension)); return $just_numbers; } diff --git a/docroot/modules/custom/va_gov_batch/src/cbo_scripts/SplitExtensionWithTwoNumbers.php b/docroot/modules/custom/va_gov_batch/src/cbo_scripts/SplitExtensionWithTwoNumbers.php index 26cc155267..7305fb2084 100644 --- a/docroot/modules/custom/va_gov_batch/src/cbo_scripts/SplitExtensionWithTwoNumbers.php +++ b/docroot/modules/custom/va_gov_batch/src/cbo_scripts/SplitExtensionWithTwoNumbers.php @@ -86,6 +86,10 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { // Do the extension work. $original_extension = $phone_paragraph->get('field_phone_extension')->value; $separated_extensions = $this->splitExtensions($original_extension); + // There is something amiss with the extensions. + if (empty($separated_extensions)) { + return "Either no change is necessary for paragraph $item or '$original_extension' cannot be successfully separated."; + } $original_label = $phone_paragraph->get('field_phone_label')->value; try { @@ -96,7 +100,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { } $phone_paragraph->set(name: 'field_phone_extension', value: $separated_extensions[0]); $phone_paragraph->save(); - $message = "1st extension for paragraph $item changed from '$original_extension' to $separated_extensions[0]' ."; + $message = "1st extension for paragraph $item changed from '$original_extension' to '$separated_extensions[0]'. "; } // Create the second extension and phone. @@ -116,7 +120,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { $phone_parent_paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->load($phone_parent_id); $phone_parent_paragraph->get($phone_parent_field_name)->appendItem($second_phone); $phone_parent_paragraph->save(); - $message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to $separated_extensions[1]'." . PHP_EOL; + $message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to '$separated_extensions[1]'."; } } catch (\Exception $e) { @@ -133,20 +137,25 @@ public function processOne(string $key, mixed $item, array &$sandbox): string { * @param string $dual_extension * The two-part extension. * - * @return array + * @return array|bool * Array with two extensions (or empty). * E.g. '2132,2995' becomes ['2132','2995'] */ public static function splitExtensions(string $dual_extension): array { - if (!preg_match('/[^0-9]/', $dual_extension)) { + $pattern = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i'; + if (!preg_match($pattern, $dual_extension)) { return []; } $first_extension = []; preg_match('/^\d+/', $dual_extension, $first_extension); $second_extension = []; preg_match('/\d+$/', $dual_extension, $second_extension); + // If either of these are empty, we want to bail out of this. + if (empty($first_extension[0]) or empty($second_extension[0])) { + return []; + } - return [$first_extension[0], $second_extension[0]]; + return [trim($first_extension[0]), trim($second_extension[0])]; } }