Download the PHP package krubio/perfect-validation without Composer
On this page you can find all versions of the php package krubio/perfect-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package perfect-validation
Validator Class Documentation
The Validator
class is responsible for validating data using different validation strategies. It allows you to set and switch between different validation strategies based on your specific requirements.
Class Signature
Constructor
__construct(ValidationStrategy $strategy)
The constructor accepts a ValidationStrategy
object as a parameter, which determines the specific validation strategy to use. The initial validation strategy is set during object creation.
Public Methods
setValidationStrategy(ValidationStrategy $strategy): void
This method allows you to set a new validation strategy by providing a ValidationStrategy
object as a parameter. It replaces the existing validation strategy with the new one.
validateData($data): mixed
The validateData
method is used to perform data validation using the current validation strategy. It accepts the data to be validated as a parameter and returns the validation result.
validateDataWithErrors($data): array
The validateDataWithErrors
method performs data validation and returns an array containing the validation status and any errors, if applicable. If the strategy object has a getErrors
method, the errors will be included in the array.
-
Parameters:
$data
: The data to be validated.
- Return:
- Returns the validation result, which depends on the specific validation strategy being used. It can be a boolean value (
true
for a successful validation,false
otherwise) or other data types based on the strategy implementation or an array with the validation status and errors, if any.
- Returns the validation result, which depends on the specific validation strategy being used. It can be a boolean value (
Usage Example
Here's an example that demonstrates the basic usage of the Validator
class:
In the example above:
- The
Validator
class is instantiated with an initial validation strategy ($requiredFieldsStrategy
). - If needed, you can use the
setValidationStrategy
method to switch to a different validation strategy. - The
validateData
andvalidateDataWithErrors
methods are called with the data to be validated ($data
). - The returned results are checked to determine if the validation passed or failed.
- Based on the validation results, you can perform appropriate actions or display error messages.
Note: The specific validation strategies used ($requiredFieldsStrategy
and $customValidationStrategy
) are not shown in the example, as they depend on your specific implementation.