A simple validation.
DEPRECATED: No personal interest anymore and based on stats nobody seems to use it. Feel free to create an issue if you disagree. Please take a look to chubbyphp-parsing its a different concept. But i believe parsing is the way to go instead of deserialze/validate.
- php: ^8.1
- psr/log: ^2.0|^3.0
- chubbyphp/chubbyphp-container: ^2.2
- pimple/pimple: ^3.5
- psr/container: ^2.0.2
Through Composer as chubbyphp/chubbyphp-validation.
composer require chubbyphp/chubbyphp-validation "^4.0"
- AllConstraint
- BlankConstraint
- CallbackConstraint
- ChoiceConstraint
- CoordinateArrayConstraint
- CoordinateConstraint
- CountConstraint
- DateTimeConstraint
- EmailConstraint
- FalseConstraint
- MapConstraint
- NotBlankConstraint
- NotNullConstraint
- NullConstraint
- NumericConstraint
- NumericRangeConstraint
- TrueConstraint
- TypeConstraint
- ValidConstraint
- CallableValidationMappingProvider
- LazyValidationMappingProvider
- ValidationClassMapping
- ValidationClassMappingBuilder
- ValidationMappingProviderRegistry
- ValidationPropertyMapping
- ValidationPropertyMappingBuilder
<?php
namespace MyProject\Model;
final class Model
{
/**
* @var array<\DateTime>
*/
private $dates;
}
<?php
namespace MyProject\Mapping\Validation;
use Chubbyphp\Validation\Constraint\AllConstraint;
use Chubbyphp\Validation\Constraint\DateTimeConstraint;
use Chubbyphp\Validation\Constraint\NotBlankConstraint;
use Chubbyphp\Validation\Constraint\NotNullConstraint;
use Chubbyphp\Validation\Mapping\ValidationClassMappingBuilder;
use Chubbyphp\Validation\Mapping\ValidationClassMappingInterface;
use Chubbyphp\Validation\Mapping\ValidationMappingProviderInterface;
use Chubbyphp\Validation\Mapping\ValidationPropertyMappingBuilder;
use Chubbyphp\Validation\Mapping\ValidationPropertyMappingInterface;
use MyProject\Model\Model;
final class ModelValidationMappingProvider implements ValidationMappingProviderInterface
{
/**
* @return string
*/
public function getClass(): string
{
return Model::class;
}
/**
* @param string $path
*
* @return ValidationClassMappingInterface
*/
public function getValidationClassMapping(string $path): ValidationClassMappingInterface
{
return ValidationClassMappingBuilder::create([])->getMapping();
}
/**
* @param string $path
* @param string|null $type
*
* @return ValidationPropertyMappingInterface[]
*/
public function getValidationPropertyMappings(string $path, string $type = null): array
{
return [
ValidationPropertyMappingBuilder::create('dates', [
new AllConstraint([
new NotNullConstraint(),
new NotBlankConstraint(),
new DateTimeConstraint('d.m.Y'),
]),
])->getMapping(),
];
}
}
<?php
namespace MyProject;
use Chubbyphp\Validation\Mapping\ValidationMappingProviderRegistry;
use Chubbyphp\Validation\ValidatorContextInterface;
use Chubbyphp\Validation\Validator;
use MyProject\Mapping\Validation\ModelValidationMappingProvider;
use MyProject\Model\Model;
$logger = ...;
$validator = new Validator(
new ValidationMappingProviderRegistry([
new ModelValidationMappingProvider()
]),
$logger
);
$model = new Model;
/** @var ValidatorContextInterface $context */
$context = ...;
$errors = $validator->validate(
$model,
$context
);
2024 Dominik Zogg