Successfully implemented Phase 2 of the type hinting improvements, adding strongly-typed Data Transfer Objects (DTOs) for API responses.
- PaystackResponse - Generic wrapper for all API responses with type parameter support
- PaystackResponseException - Exception for failed API responses
- PaginationMeta - Structured pagination information with helper methods
- PaginatedResponse - Generic paginated collection response
- CustomerData - Strongly-typed customer object with properties like:
id,customer_code,email,first_name,last_name- Helper methods:
getFullName(),hasAuthorizations(),hasSubscriptions(), etc.
- CustomerListResponse - Paginated customer list with typed CustomerData objects
- TransactionData - Complete transaction information with:
- Status checking:
isSuccessful(),isPending(),isFailed(),isAbandoned() - Amount helpers:
getAmountInMajorUnit(),getFormattedAmount() - Customer info:
getCustomerEmail(),getCustomerName()
- Status checking:
- TransactionInitializeResponse - Transaction initialization with:
authorization_url,access_code,reference- Helper:
isInitialized()
- PaymentRequestData - Payment request/invoice data with:
- Status checking:
isPending(),isPaid(),isPartiallyPaid(),isCancelled() - Calculations:
getLineItemsTotal(),getTaxTotal(),getFormattedAmount() - Due date helpers:
hasDueDate(),isOverdue(),getDueDateAsDateTime()
- Status checking:
Added typed response methods to:
- Customer API:
createTyped(),allTyped() - Transaction API:
initializeTyped(),verifyTyped(),allTyped() - PaymentRequest API:
createTyped(),allTyped()
- ResponseFactory - Factory methods for creating typed responses
- ResponseMediator - Extended with typed response methods while maintaining backward compatibility
src/Response/
├── PaystackResponse.php
├── PaystackResponseException.php
├── PaginationMeta.php
├── PaginatedResponse.php
├── ResponseFactory.php
├── Customer/
│ ├── CustomerData.php
│ └── CustomerListResponse.php
├── Transaction/
│ ├── TransactionData.php
│ └── TransactionInitializeResponse.php
└── PaymentRequest/
└── PaymentRequestData.php
examples/typed_responses_demo.php- Comprehensive demonstration- Updated
examples/README.mdwith Phase 2 information
src/HttpClient/Message/ResponseMediator.php- Added typed response methodssrc/API/Customer.php- AddedcreateTyped(),allTyped()src/API/Transaction.php- AddedinitializeTyped(),verifyTyped(),allTyped()src/API/PaymentRequest.php- AddedcreateTyped(),allTyped()
- Type Safety - Catch errors at development time, not runtime
- IDE Support - Full autocomplete for all response properties
- Helper Methods - Convenient methods for common operations
- Structured Data - DateTimeImmutable for dates, proper types throughout
- Documentation - Inline PHPDoc visible in IDE tooltips
- No Array Key Typos - Properties are strongly typed
- Refactoring Support - IDEs can track usage and rename safely
- Clear Contracts - Response structure is explicitly defined
- Future-Proof - Easy to extend with new methods and properties
✅ 100% Backward Compatible
- All existing array-based methods remain unchanged
- Old code continues to work without modifications
- New typed methods use
*Typed()suffix pattern - Gradual migration path available
✅ All Tests Pass
- 144 tests, 496 assertions
- Zero regressions
- Both old and new methods work correctly
$response = $paystack->customers()->create([...]);
if ($response['status']) {
$email = $response['data']['email']; // No autocomplete
}$response = $paystack->customers()->createTyped([...]);
if ($response->isSuccessful()) {
$customer = $response->getData(); // CustomerData object
$email = $customer->email; // Full autocomplete!
$name = $customer->getFullName(); // Helper method
}- New Code: Use
*Typed()methods immediately - Existing Code: No changes required, works as-is
- Gradual Migration: Convert to typed methods during refactoring
- Both Supported: Use whichever approach fits your needs
Phase 3 will introduce Request DTOs for type-safe API parameters:
- Strongly-typed request objects instead of arrays
- Validation at creation time
- Builder patterns for complex requests
- Full IDE support for required/optional parameters
Run the comprehensive demonstration:
php examples/typed_responses_demo.phpThis shows:
- Side-by-side comparison of old vs new approaches
- All DTO features and helper methods
- Pagination support
- Backward compatibility
- Real-world usage patterns
Phase 2 successfully delivers strongly-typed response DTOs that significantly improve the developer experience while maintaining complete backward compatibility. The implementation provides immediate value through better IDE support, type safety, and convenient helper methods.