2010-12-06 01:44:05 +01: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-12-06 01:44:05 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2019-02-04 00:29:13 +01:00
|
|
|
use Respect\Validation\Exceptions\NestedValidationException;
|
2010-12-06 01:44:05 +01:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2015-10-18 03:44:47 +02:00
|
|
|
use Respect\Validation\Validatable;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2018-05-24 08:07:55 +02:00
|
|
|
use function is_scalar;
|
2010-12-06 01:44:05 +01:00
|
|
|
|
2018-11-25 17:56:04 +01:00
|
|
|
/**
|
|
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Nick Lombard <github@jigsoft.co.za>
|
|
|
|
*/
|
2013-01-23 08:58:53 +01:00
|
|
|
abstract class AbstractRelated extends AbstractRule
|
2010-12-06 01:44:05 +01:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2019-05-05 16:22:44 +02:00
|
|
|
private $mandatory = true;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2019-05-05 16:22:44 +02:00
|
|
|
private $reference;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var Validatable|null
|
|
|
|
*/
|
2019-05-05 16:22:44 +02:00
|
|
|
private $rule;
|
2010-12-06 01:44:05 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
2018-03-16 16:29:21 +01:00
|
|
|
abstract public function hasReference($input): bool;
|
2011-02-07 11:52:18 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-02-10 21:49:48 +01:00
|
|
|
abstract public function getReferenceValue($input);
|
2011-02-07 11:52:18 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $reference
|
|
|
|
*/
|
2019-05-05 16:22:44 +02:00
|
|
|
public function __construct($reference, ?Validatable $rule = null, bool $mandatory = true)
|
2010-12-06 01:44:05 +01:00
|
|
|
{
|
|
|
|
$this->reference = $reference;
|
2019-05-05 16:22:44 +02:00
|
|
|
$this->rule = $rule;
|
2010-12-06 02:17:12 +01:00
|
|
|
$this->mandatory = $mandatory;
|
2019-06-19 08:44:42 +02:00
|
|
|
|
2020-07-22 14:02:39 +02:00
|
|
|
if ($rule && $rule->getName() !== null) {
|
2019-06-19 08:44:42 +02:00
|
|
|
$this->setName($rule->getName());
|
|
|
|
} elseif (is_scalar($reference)) {
|
|
|
|
$this->setName((string) $reference);
|
|
|
|
}
|
2015-06-30 15:33:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getReference()
|
|
|
|
{
|
|
|
|
return $this->reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isMandatory(): bool
|
|
|
|
{
|
|
|
|
return $this->mandatory;
|
|
|
|
}
|
|
|
|
|
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
|
2015-06-30 15:33:19 +02:00
|
|
|
{
|
|
|
|
parent::setName($name);
|
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
if ($this->rule instanceof Validatable) {
|
|
|
|
$this->rule->setName($name);
|
2015-06-30 15:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2010-12-06 01:44:05 +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
|
2010-12-06 01:44:05 +01:00
|
|
|
{
|
2013-01-23 09:09:22 +01:00
|
|
|
$hasReference = $this->hasReference($input);
|
2015-01-08 03:15:35 +01:00
|
|
|
if ($this->mandatory && !$hasReference) {
|
2015-10-18 03:44:47 +02:00
|
|
|
throw $this->reportError($input, ['hasReference' => false]);
|
2015-01-08 03:15:35 +01:00
|
|
|
}
|
2011-04-27 16:07:17 +02:00
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
if ($this->rule === null || !$hasReference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-27 16:07:17 +02:00
|
|
|
try {
|
2019-05-05 16:22:44 +02:00
|
|
|
$this->rule->assert($this->getReferenceValue($input));
|
2019-02-04 00:29:13 +01:00
|
|
|
} catch (ValidationException $validationException) {
|
|
|
|
/** @var NestedValidationException $nestedValidationException */
|
|
|
|
$nestedValidationException = $this->reportError($this->reference, ['hasReference' => true]);
|
|
|
|
$nestedValidationException->addChild($validationException);
|
|
|
|
|
|
|
|
throw $nestedValidationException;
|
2011-04-27 16:07:17 +02:00
|
|
|
}
|
2010-12-06 01:44:05 +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-12-06 01:44:05 +01:00
|
|
|
{
|
2011-04-27 16:07:17 +02:00
|
|
|
$hasReference = $this->hasReference($input);
|
2015-01-08 03:15:35 +01:00
|
|
|
if ($this->mandatory && !$hasReference) {
|
2015-10-18 03:44:47 +02:00
|
|
|
throw $this->reportError($input, ['hasReference' => false]);
|
2015-01-08 03:15:35 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
if ($this->rule === null || !$hasReference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rule->check($this->getReferenceValue($input));
|
2010-12-06 01:44:05 +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 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2015-02-20 13:26:50 +01:00
|
|
|
$hasReference = $this->hasReference($input);
|
2015-01-08 03:15:35 +01:00
|
|
|
if ($this->mandatory && !$hasReference) {
|
2011-02-07 11:52:18 +01:00
|
|
|
return false;
|
2015-01-08 03:15:35 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
if ($this->rule === null || !$hasReference) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-16 16:29:21 +01:00
|
|
|
|
2019-05-05 16:22:44 +02:00
|
|
|
return $this->rule->validate($this->getReferenceValue($input));
|
2018-03-16 16:29:21 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|