2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
use Respect\Validation\Factory;
|
|
|
|
use Respect\Validation\Validatable;
|
|
|
|
|
|
|
|
/**
|
2024-01-31 22:15:08 +08:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2022-12-24 22:10:40 +08:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Nick Lombard <github@jigsoft.co.za>
|
|
|
|
* @author Vicente Mendoza <vicentemmor@yahoo.com.mx>
|
|
|
|
*/
|
|
|
|
abstract class AbstractRule implements Validatable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2024-01-31 22:15:08 +08:00
|
|
|
protected $default;
|
|
|
|
/**
|
|
|
|
* @var bool|false
|
|
|
|
*/
|
|
|
|
protected $defaultType;
|
2022-12-24 22:10:40 +08:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2024-01-31 22:15:08 +08:00
|
|
|
protected $name;
|
2022-12-24 22:10:40 +08:00
|
|
|
|
|
|
|
/**
|
2024-01-31 22:15:08 +08:00
|
|
|
* @var string|null
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
2024-01-31 22:15:08 +08:00
|
|
|
protected $template;
|
2022-12-24 22:10:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function assert($input): void
|
|
|
|
{
|
|
|
|
if ($this->validate($input)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $this->reportError($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function check($input): void
|
|
|
|
{
|
|
|
|
$this->assert($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getDefault(): ?string
|
|
|
|
{
|
|
|
|
return $this->default;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getDefaultType(): ?bool
|
|
|
|
{
|
|
|
|
return $this->defaultType;
|
|
|
|
}
|
|
|
|
public function setDefault(string $default, bool $defaultType=false): Validatable
|
|
|
|
{
|
|
|
|
$this->default = $default;
|
|
|
|
$this->defaultType = $defaultType;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed$input
|
|
|
|
*/
|
|
|
|
public function __invoke($input): bool
|
|
|
|
{
|
|
|
|
return $this->validate($input);
|
|
|
|
}
|
2022-12-24 22:10:40 +08:00
|
|
|
}
|