-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: implement casting form request #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request implements a comprehensive casting system for HTTP form requests, allowing automatic transformation of raw input data into typed objects and collections. The implementation adds support for casting to primitives, enums, data objects, and their array/collection counterparts.
- Introduces a
HasCasts
trait with casting logic for primitives, enums, data objects, and custom classes - Creates contract interfaces
Castable
andCastInputs
for extensible casting behavior - Adds specialized casting classes for enum arrays/collections and data object arrays/collections
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
tests/Foundation/Http/CustomCastingTest.php | Comprehensive test suite covering all casting scenarios including primitives, enums, data objects, and their collections |
src/foundation/src/Http/Traits/HasCasts.php | Core casting trait with methods for transforming input values to target types |
src/foundation/src/Http/FormRequest.php | Updated FormRequest to use HasCasts trait and merge cast definitions |
src/foundation/src/Http/Contracts/Castable.php | Interface for castable classes that can define their casting behavior |
src/foundation/src/Http/Contracts/CastInputs.php | Interface for custom casting implementations |
src/foundation/src/Http/Casts/AsEnumCollection.php | Caster for transforming arrays to enum collections |
src/foundation/src/Http/Casts/AsEnumArrayObject.php | Caster for transforming arrays to enum ArrayObjects |
src/foundation/src/Http/Casts/AsDataObjectCollection.php | Caster for transforming arrays to data object collections |
src/foundation/src/Http/Casts/AsDataObjectArray.php | Caster for transforming arrays to data object ArrayObjects |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
*/ | ||
public function getCasts(): array | ||
{ | ||
return $this->casts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getCasts()
method should also check for and merge the casts()
method to ensure both property-based and method-based cast definitions are available, similar to how it's done in the FormRequest constructor.
return $this->casts; | |
$casts = $this->casts; | |
if (method_exists($this, 'casts')) { | |
$methodCasts = $this->casts(); | |
if (is_array($methodCasts)) { | |
// Method-based casts take precedence | |
$casts = array_merge($casts, $methodCasts); | |
} | |
} | |
return $casts; |
Copilot uses AI. Check for mistakes.
|
||
namespace Hypervel\Foundation\Http\Casts; | ||
|
||
use Hyperf\Collection\Collection; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Hypervel\Support\Collection
instead
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
Mockery::close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant operation here?
This pull request introduces a set of new casting utilities for handling arrays and collections of data objects and enums in HTTP form requests. It also refactors the
FormRequest
class to support custom input casting via a new trait and contract interfaces. The main changes are grouped into new casting classes, contract interfaces, and updates to the form request lifecycle.