Original file: src/Fsm/Data/ReplayStatisticsRequest.php
The ReplayStatisticsRequest.php file defines the ReplayStatisticsRequest class, which serves as a Data Transfer Object (DTO) for retrieving FSM (Finite State Machine) transition statistics. This class structures and validates the input parameters necessary for analyzing and obtaining statistics related to FSM usage patterns.
By encapsulating the data and validation rules in this class, the codebase ensures that only valid requests are processed for analytics, promoting better data integrity and maintainability.
public function __construct(string|array $modelClass, string $modelId = '', string $columnName = '');
The constructor initializes a new instance of the ReplayStatisticsRequest class. It processes the input parameters, either as individual strings or as a single associative array, and prepares them for later validation and usage.
string|array $modelClass: The name of the model class as a string, or an associative array representing the attributes to initialize the object. If an array is provided with associative keys, the keys must correspond to snake_case naming convention attributes.string $modelId (optional): The identifier of the model instance for which statistics are requested. Defaults to an empty string.string $columnName (optional): The specific column name for which statistics are requested. Defaults to an empty string.$modelClass parameter is an array and contains associative keys (this is determined using static::isAssociative()).snake_case to camelCase) using static::prepareAttributes($modelClass).Dto is called with the prepared data.Here is an example of how to invoke this constructor:
$request = new ReplayStatisticsRequest([
'modelClass' => 'App\Models\User',
'modelId' => '123',
'columnName' => 'status',
]);
public static function rules(): array;
The rules method defines the validation rules for the parameters of the ReplayStatisticsRequest class. This method ensures that all incoming data complies with the expected format and type restrictions, which protects the application from invalid input.
ReplayStatisticsRequest class:
Illuminate\Database\Eloquent\Model.The custom validation for modelClass performs two key checks:
class_exists().is_subclass_of().Here's an example of how this method could be used in a validation context:
$rules = ReplayStatisticsRequest::rules();
// Expected $rules structure:
// [
// 'modelClass' => [
// 'required',
// 'string',
// function ($attribute, $value, $fail) { /* ... */ },
// ],
// 'modelId' => ['required', 'string'],
// 'columnName' => ['required', 'string'],
// ]
The use of this structured approach enhances code readability and maintains separation of concerns, ensuring that requests are properly validated before being processed further.
How can I help you explore Laravel packages today?