respect-validation/library/Rules/AbstractRule.php

102 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Factory;
2015-10-18 03:44:47 +02:00
use Respect\Validation\Validatable;
2010-10-07 00:21:01 +02:00
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author Vicente Mendoza <vicentemmor@yahoo.com.mx>
*/
2010-10-07 00:21:01 +02:00
abstract class AbstractRule implements Validatable
{
/**
* @var string|null
*/
protected $name;
/**
* @var string|null
*/
protected $template;
/**
* {@inheritDoc}
*/
public function assert($input): void
{
if ($this->validate($input)) {
return;
}
2013-01-23 08:40:12 +01:00
throw $this->reportError($input);
}
/**
* {@inheritDoc}
*/
public function check($input): void
2010-10-07 00:21:01 +02:00
{
$this->assert($input);
2010-10-07 00:21:01 +02:00
}
2012-05-06 02:06:13 +02:00
/**
* {@inheritDoc}
*/
public function getName(): ?string
{
return $this->name;
}
/**
* {@inheritDoc}
*/
public function reportError($input, array $extraParams = []): ValidationException
{
return Factory::getDefaultInstance()->exception($this, $input, $extraParams);
}
/**
* {@inheritDoc}
*/
public function setName(string $name): Validatable
{
$this->name = $name;
return $this;
}
/**
* {@inheritDoc}
*/
public function setTemplate(string $template): Validatable
{
$this->template = $template;
return $this;
}
/**
* @param mixed$input
*/
public function __invoke($input): bool
{
return $this->validate($input);
}
}