HighSpeaker/vendor/workerman/validation/library/Rules/AbstractComposite.php

151 lines
3.7 KiB
PHP
Raw Permalink Normal View History

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\NestedValidationException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use function array_filter;
use function array_map;
/**
* Abstract class for rules that are composed by other rules.
*
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 Wojciech Frącz <fraczwojciech@gmail.com>
*/
abstract class AbstractComposite extends AbstractRule
{
/**
* @var Validatable[]
*/
private $rules = [];
/**
* Initializes the rule adding other rules to the stack.
*/
public function __construct(Validatable ...$rules)
{
$this->rules = $rules;
}
/**
* {@inheritDoc}
*/
public function setName(string $name): Validatable
{
$parentName = $this->getName();
foreach ($this->rules as $rule) {
$ruleName = $rule->getName();
if ($ruleName && $parentName !== $ruleName) {
continue;
}
$rule->setName($name);
}
return parent::setName($name);
}
2024-01-31 22:15:08 +08:00
public function setDefault(string $default, bool $defaultType=false): Validatable
{
$parentDefault = $this->getDefault();
foreach ($this->rules as $rule) {
$ruleDefault = $rule->getDefault();
if ($ruleDefault && $parentDefault !== $ruleDefault) {
continue;
}
$rule->setDefault($default, $defaultType);
}
return parent::setDefault($default, $defaultType);
}
2022-12-24 22:10:40 +08:00
/**
* Append a rule into the stack of rules.
*
* @return AbstractComposite
*/
public function addRule(Validatable $rule): self
{
if ($this->shouldHaveNameOverwritten($rule) && $this->getName() !== null) {
$rule->setName($this->getName());
}
$this->rules[] = $rule;
return $this;
}
/**
* Returns all the rules in the stack.
*
* @return Validatable[]
*/
public function getRules(): array
{
return $this->rules;
}
/**
* Returns all the exceptions throw when asserting all rules.
*
* @param mixed $input
*
* @return ValidationException[]
*/
protected function getAllThrownExceptions($input): array
{
return array_filter(
array_map(
function (Validatable $rule) use ($input): ?ValidationException {
try {
$rule->assert($input);
} catch (ValidationException $exception) {
$this->updateExceptionTemplate($exception);
return $exception;
}
return null;
},
$this->getRules()
)
);
}
private function shouldHaveNameOverwritten(Validatable $rule): bool
{
return $this->hasName($this) && !$this->hasName($rule);
}
private function hasName(Validatable $rule): bool
{
return $rule->getName() !== null;
}
private function updateExceptionTemplate(ValidationException $exception): void
{
if ($this->template === null || $exception->hasCustomTemplate()) {
return;
}
$exception->updateTemplate($this->template);
if (!$exception instanceof NestedValidationException) {
return;
}
foreach ($exception->getChildren() as $childException) {
$this->updateExceptionTemplate($childException);
}
}
}