respect-validation/library/Rules/AbstractComposite.php
Henrique Moody 167c97bb83
Ensure names are always string
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-05-27 16:12:05 +02:00

127 lines
3 KiB
PHP

<?php
/*
* 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.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
abstract class AbstractComposite extends AbstractRule
{
protected $rules = [];
public function __construct(...$rule)
{
$this->addRules($rule);
}
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);
}
public function addRule($validator, $arguments = [])
{
if (!$validator instanceof Validatable) {
$this->appendRule(Validator::buildRule($validator, $arguments));
} else {
$this->appendRule($validator);
}
return $this;
}
public function removeRules(): void
{
$this->rules = [];
}
public function addRules(array $validators)
{
foreach ($validators as $key => $spec) {
if ($spec instanceof Validatable) {
$this->appendRule($spec);
} elseif (is_numeric($key) && is_array($spec)) {
$this->addRules($spec);
} elseif (is_array($spec)) {
$this->addRule($key, $spec);
} else {
$this->addRule($spec);
}
}
return $this;
}
public function getRules()
{
return $this->rules;
}
public function hasRule($validator)
{
if (empty($this->rules)) {
return false;
}
if ($validator instanceof Validatable) {
return isset($this->rules[spl_object_hash($validator)]);
}
if (is_string($validator)) {
foreach ($this->rules as $rule) {
if (get_class($rule) == __NAMESPACE__.'\\'.$validator) {
return true;
}
}
}
return false;
}
protected function appendRule(Validatable $validator): void
{
if (!$validator->getName() && $this->getName()) {
$validator->setName($this->getName());
}
$this->rules[spl_object_hash($validator)] = $validator;
}
protected function validateRules($input)
{
$validators = $this->getRules();
$exceptions = [];
foreach ($validators as $v) {
try {
$v->assert($input);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
}
return $exceptions;
}
}