krubio / perfect-validation
SOLID Compliant OOP Validation
Requires
- php: >=8.0.0
Requires (Dev)
- codeception/codeception: ^5.0
- codeception/module-asserts: *
- nunomaduro/phpinsights: ^2.8
- phpstan/phpstan: ^1.10
- vimeo/psalm: ^5.12
This package is auto-updated.
Last update: 2024-12-22 01:55:00 UTC
README
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
class Validator { private ValidationStrategy $strategy; public function __construct(ValidationStrategy $strategy); public function setValidationStrategy(ValidationStrategy $strategy): void; public function validateData($data): mixed; public function validateDataWithErrors($data): array; }
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:
// Create a validator with a specific validation strategy $validator = new Validator($requiredFieldsStrategy); // Set a new validation strategy if needed $validator->setValidationStrategy($customValidationStrategy); // Perform data validation $data = $_POST; // Example data to be validated $result = $validator->validateData($data); // Perform data validation with errors $resultWithErrors = $validator->validateDataWithErrors($data); // Check the validation result if ($result === true) { echo "Validation passed! The data is valid."; } elseif (isset($resultWithErrors['errors'])) { echo "Validation failed! Errors: " . implode(', ', $resultWithErrors['errors']); } else { echo "Validation failed! Please check the data for errors."; }
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.