Fix otp xml message response parsing #48
Open
+133
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add XML Response Format Support
Summary
Due to recent changes in the Netgsm API response format, the package now supports both the new XML format and the legacy space-separated format. This change is backward compatible and does not affect existing applications.
Problem
The Netgsm API recently changed its response format and now returns responses in XML format like this:
The previously used format was:
Since the existing code only supported the space-separated format, responses in the new XML format could not be parsed correctly.
Solution
1. Updated Response Parsing Mechanism (
src/Sms/AbstractNetgsmMessage.php)Changes:
parseResponse()method: Now automatically detects the response format and calls the appropriate parser methodparseXmlResponse()method: Parses responses in XML formatsimplexml_load_string()codeandjobIDvalues from XMLparseLegacyResponse()method: Parses the old space-separated formatCode Flow:
2. Added Comprehensive Test Coverage (
tests/NetGsmMessageTest.php)New Tests:
it_can_parse_xml_response_successfully: Verifies that XML format is parsed successfullyit_can_parse_xml_response_with_single_digit_code: Tests that single-digit codes (0, 1, 2) are normalized correctlyit_can_parse_legacy_space_separated_response: Verifies that the old format still works (backward compatibility)it_throws_exception_for_invalid_xml_response_code: Tests that an exception is thrown for invalid codesit_throws_exception_for_missing_job_id_in_xml: Tests that an exception is thrown for empty jobIDTest Refactoring:
setResponseAndParse()- Moved repetitive reflection code into a common methodBackward Compatibility
✅ 100% Backward Compatible: Support for the old space-separated format is preserved. Existing applications will continue to work without any changes.
Tested Scenarios
Example Usage
No changes required in user code:
Changed Files
src/Sms/AbstractNetgsmMessage.php: Updated response parsing mechanismtests/NetGsmMessageTest.php: Added 5 new tests + helper methodNotes
ext-simplexmlextension for XML parsing (already a requirement incomposer.json)0,00,1,01,2,02Checklist