Skip to content

Conversation

chinfuyang
Copy link
Contributor

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.

@albertcht albertcht added the feature New feature or request label Oct 3, 2025
@albertcht albertcht changed the title Form request casting feat: implement casting form request Oct 3, 2025
@albertcht albertcht requested a review from Copilot October 3, 2025 15:14
Copy link

@Copilot Copilot AI left a 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 and CastInputs 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;
Copy link
Preview

Copilot AI Oct 3, 2025

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.

Suggested change
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;
Copy link
Member

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant operation here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants