2017 © Pedro Peláez
 

library validator

Validator implementation lib

image

star/validator

Validator implementation lib

  • Saturday, February 14, 2015
  • by yvoyer
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 1 Versions
  • 40 % Grown

The README.md

Validator

Build Status, (*1)

Validation tool that can be connected on any object you want to validate., (*2)

Usage

Validators

Validators are classes that will determines if the value was valid. In the following example, the YourNonEmptyValidator would make sure the name on the SomeObject cannot be empty., (*3)

// YourNonEmptyValidator.php
class YourNonEmptyValidator implements Validator
{
    /**
     * @var SomeObject
     */
    private $object;

    /**
     * @param SomeObject $object
     */
    public function __construct(SomeObject $object)
    {
        $this->object = $object;
    }

    /**
     * @param NotificationHandler $handler
     *
     * @return ValidationResult
     */
    public function validate(NotificationHandler $handler)
    {
        $name = $this->object->getName();

        if (empty($name)) {
            $handler->notifyError(new StringMessage('Name cannot be empty.'));
        }

        return $handler->createResult();
    }
}

The validators will return a ValidationResult to determine whether any constraints have failed., (*4)

NotificationHandler

The NotificationHandler are responsible to notify the user about an error., (*5)

The only supported strategy currently implemented are:, (*6)

  • ExceptionNotificationHandler: Will throw a ValidationErrorException on the first error (recommended for development).
  • DeferredNotificationHandler: Will keep the trace of all errors for future use by the ValidationResult.

Example

Given you created a class that needs to be validated:, (*7)

// SomeObject.php
class SomeObject
{
    /**
     * @var string
     */
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

You will need to implement a validation method in which you will put all the validators to push in the ValidationHandler., (*8)

// SomeObject.php
...
/**
 * @param NotificationHandler $handler
 *
 * @return ValidationResult
 */
public function validate(NotificationHandler $handler)
{
    $validator = new YourNonEmptyValidator($this); // This is your custom validator implementing Validator interface

    return $validator->validate($handler);
}
...

Creating the validator for your object (or using built-in validators, you can get the result from the validation., (*9)

By using the structure, validating your code will look something like this., (*10)

// Using the Exception handler on a valid object
$validObject = new SomeObject();
$validObject->setName('non-empty');
$result = $validObject->validate(new ExceptionNotificationHandler());
$result->hasErrors(); // Returns false
$result->getErrors()); // Returns empty array(), since there was no errors

// Using the Exception handler on a invalid object
$invalidObject = new SomeObject();
$invalidObject->validate(new ExceptionNotificationHandler()); // Would throw an exception on the first error (because of the handler).

// Using the deferred handler on a invalid object
$result = $invalidObject->validate(new DeferredNotificationHandler());
$result->hasErrors(); // Returns true
$result->getErrors()); // Returns empty array('Name cannot be empty.')

The Versions

14/02 2015

dev-master

9999999-dev

Validator implementation lib

  Sources   Download

MIT

The Requires

 

The Development Requires