Skip to content

ipedis/validation-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validation-handler

This library is useful for file validation built on top of symfony/validator component.

Get Started

First you have to install dependency of this library.

make install
make test

How to use?

Prepare your data inside DataWrapper and create validation chain with validators.

use Ipedis\ValidationHandler\Data\DataWrapper;
use Ipedis\ValidationHandler\Data\Constraints\FileSize;
use Ipedis\ValidationHandler\Data\Constraints\MimeTypes;
use Ipedis\ValidationHandler\Validator\Result\ValidationResult;
use Ipedis\ValidationHandler\ConstraintFactory;

$file = __DIR__."/../tests/data/265kb.pdf";
$data = new DataWrapper(new SplFileInfo($file));

/**
 * build validator with list of constraints.
 */
$validator = ConstraintFactory::build(constraints: [
    new FileSize('100', 'k'),
    new MimeTypes(['application/pdf'])
]);
// run validations
$result = $validation->handle();

var_dump($result->isFailed(), $result->getErrorMessage());

/*
 * don't want to search for mimetype string? you can use built-in mimetypes helper 
 */
// example of mimetypes helper
$validator = ConstraintFactory::build(constraints: [
    new FileSize('1', 'M'),
    MimeTypes::with(PdfMimeType::class)
]);
/** @var ValidationResult $result */
$result = $validator->handle($data);

// should pass.
var_dump($result->isFailed(), $result->getErrorMessage());

/**
* If you do not concern about filesize but only want to check if file is image or pdf
 */
var_dump(ValidationHelper::isImage($data));
var_dump(ValidationHelper::isPdf($data));

Result will be instanced of ValidationResult, isFailed() is helper to know if validation failed or not. getError() will give you instance of symfony's ConstraintViolationInterface.

Adding more validator

Each validator class extends HandlerAbstract. You can create new class and perform your own required validations.

Limitations and TODO

  • For now validation is only for uploaded files, we can expand it to use other types.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors