diff --git a/web/modules/weather_data/src/Service/Test/TryParsingDescriptionText.php.test b/web/modules/weather_data/src/Service/Test/TryParsingDescriptionText.php.test new file mode 100644 index 000000000..38f87506f --- /dev/null +++ b/web/modules/weather_data/src/Service/Test/TryParsingDescriptionText.php.test @@ -0,0 +1,52 @@ + + "Snow expected. Total snow accumulations of 5 to 10\ninches.", + "where" => "Eastern San Juan Mountains Above 10000 Feet.", + "when" => "From 11 PM this evening to 11 PM MST Thursday.", + "impacts" => + "Travel could be very difficult. The hazardous\nconditions may impact travel over Wolf Creek Pass.", + ]; + $this->assertEquals($expected, $parsedDescription); + } + + public function testNotMatching(): void + { + $rawDescription = + "* This is an alert with\n\nmultiple newlines, but it has nothing "; + $rawDescription .= "to do with the what WHERE WHEN\n format we are concerned with."; + $parsedDescription = WeatherAlertTrait::tryParsingDescriptionText( + $rawDescription, + ); + $expected = $rawDescription; + + $this->assertEquals($expected, $parsedDescription); + } +} diff --git a/web/modules/weather_data/src/Service/Test/WeatherAlertTrait.php.test b/web/modules/weather_data/src/Service/Test/WeatherAlertTrait.php.test index c51a17678..2e0ada747 100644 --- a/web/modules/weather_data/src/Service/Test/WeatherAlertTrait.php.test +++ b/web/modules/weather_data/src/Service/Test/WeatherAlertTrait.php.test @@ -34,18 +34,23 @@ final class WeatherAlertTraitTest extends Base "onset" => "Sunday, 10/01, 2:32 PM MDT", "ends" => "Sunday, 10/01, 4:32 PM MDT", "expires" => "Sunday, 10/01, 6:32 PM MDT", + "usesParsedDescription" => false, ], (object) [ "areaDesc" => false, "event" => "volcano warning", - "description" => - "* WHAT...Bad weather\n\n* WHERE...Places\n\n* FAKE...This goes away", + "description" => [ + "what" => "Bad weather", + "where" => "Places", + "fake" => "This goes away", + ], "geometry" => [], "instruction" => "Duck! And coooover!\n\nJust duck! And cooooover!", "onset" => false, "ends" => false, "expires" => false, + "usesParsedDescription" => true, ], (object) [ "areaDesc" => "place 1", @@ -57,6 +62,7 @@ final class WeatherAlertTraitTest extends Base "instruction" => false, "ends" => false, "expires" => false, + "usesParsedDescription" => false, ], (object) [ "event" => "air quality alert", @@ -67,6 +73,7 @@ final class WeatherAlertTraitTest extends Base "ends" => false, "expires" => false, "onset" => "Tuesday, 01/09, 12:00 PM MST", + "usesParsedDescription" => false, ], (object) [ "event" => "air quality alert", @@ -77,6 +84,7 @@ final class WeatherAlertTraitTest extends Base "ends" => false, "expires" => false, "onset" => "Tuesday, 01/09, 1:00 PM MST", + "usesParsedDescription" => false, ], (object) [ "event" => "air quality alert", @@ -87,6 +95,7 @@ final class WeatherAlertTraitTest extends Base "ends" => false, "expires" => false, "onset" => "Tuesday, 01/09, 1:00 PM MST", + "usesParsedDescription" => false, ], (object) [ "event" => "air quality alert", @@ -97,6 +106,7 @@ final class WeatherAlertTraitTest extends Base "ends" => false, "expires" => false, "onset" => "Tuesday, 01/09, 2:00 PM MST", + "usesParsedDescription" => false, ], ]; } diff --git a/web/modules/weather_data/src/Service/WeatherAlertTrait.php b/web/modules/weather_data/src/Service/WeatherAlertTrait.php index 483319e1d..210eeeb41 100644 --- a/web/modules/weather_data/src/Service/WeatherAlertTrait.php +++ b/web/modules/weather_data/src/Service/WeatherAlertTrait.php @@ -33,6 +33,27 @@ protected static function turnToDate($str, $timezone) return $str; } + public static function tryParsingDescriptionText($str) + { + // look for any word in all caps followed by ellipses, and use that as the label, and + // all text until the next pair of newlines is the content + // + // https://regexper.com/#%2F%5C*%5Cs%2B%28%5BA-Za-z%5Cs%5D%2B%29%5C.%5C.%5C.%28.*%29%28%5Cn%7B2%7D%7C%24%29%2F + $regex = "/\*\s+(?