2010-09-24 04:30:16 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-09-26 07:16:02 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
2010-09-24 04:30:16 +02:00
|
|
|
|
2018-06-01 10:34:21 +02:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2018-01-06 14:49:38 +01:00
|
|
|
use Respect\Validation\Factory;
|
2015-10-18 03:44:47 +02:00
|
|
|
use Respect\Validation\Validatable;
|
2010-10-07 00:21:01 +02:00
|
|
|
|
2018-11-25 17:56:04 +01: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
|
2010-09-24 04:30:16 +02:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2011-02-06 23:21:15 +01:00
|
|
|
protected $name;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2015-10-18 19:56:08 +02:00
|
|
|
protected $template;
|
2010-12-07 13:11:14 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 17:16:53 +01:00
|
|
|
public function assert($input): void
|
2011-02-06 23:21:15 +01:00
|
|
|
{
|
2015-10-17 20:40:46 +02:00
|
|
|
if ($this->validate($input)) {
|
2018-01-28 17:16:53 +01:00
|
|
|
return;
|
2015-01-08 03:15:35 +01:00
|
|
|
}
|
2018-01-06 14:49:38 +01:00
|
|
|
|
2013-01-23 08:40:12 +01:00
|
|
|
throw $this->reportError($input);
|
2011-02-06 23:21:15 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 16:14:00 +01:00
|
|
|
public function check($input): void
|
2010-10-07 00:21:01 +02:00
|
|
|
{
|
2018-01-28 16:14:00 +01:00
|
|
|
$this->assert($input);
|
2010-10-07 00:21:01 +02:00
|
|
|
}
|
2012-05-06 02:06:13 +02:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-05-24 08:07:55 +02:00
|
|
|
public function getName(): ?string
|
2010-12-06 20:02:21 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
return $this->name;
|
2010-12-06 20:02:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-06-01 10:34:21 +02:00
|
|
|
public function reportError($input, array $extraParams = []): ValidationException
|
2011-02-06 23:21:15 +01:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
return Factory::getDefaultInstance()->exception($this, $input, $extraParams);
|
2011-02-06 23:21:15 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-05-24 08:07:55 +02:00
|
|
|
public function setName(string $name): Validatable
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-06-01 10:37:45 +02:00
|
|
|
public function setTemplate(string $template): Validatable
|
2011-04-17 17:29:48 +02:00
|
|
|
{
|
|
|
|
$this->template = $template;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-04-17 17:29:48 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2020-08-27 15:29:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed$input
|
|
|
|
*/
|
|
|
|
public function __invoke($input): bool
|
|
|
|
{
|
|
|
|
return $this->validate($input);
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|