2014-05-07 05:51:54 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2014-05-07 05:51:54 +02:00
|
|
|
namespace Respect\Validation;
|
|
|
|
|
2018-02-04 00:59:18 +01:00
|
|
|
use ReflectionClass;
|
2018-05-24 07:47:09 +02:00
|
|
|
use ReflectionException;
|
2018-02-04 00:59:18 +01:00
|
|
|
use ReflectionObject;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Exceptions\InvalidClassException;
|
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2018-05-24 07:47:09 +02:00
|
|
|
use function lcfirst;
|
2019-02-17 19:41:55 +01:00
|
|
|
use function sprintf;
|
|
|
|
use function trim;
|
|
|
|
use function ucfirst;
|
2014-05-07 05:51:54 +02:00
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
/**
|
|
|
|
* Factory of objects.
|
|
|
|
*
|
2018-11-25 17:20:02 +01:00
|
|
|
* @author Augusto Pascutti <augusto@phpsp.org.br>
|
2018-01-06 14:49:38 +01:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
final class Factory
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
/**
|
|
|
|
* Default instance of the Factory.
|
|
|
|
*
|
|
|
|
* @var Factory
|
|
|
|
*/
|
|
|
|
private static $defaultInstance;
|
2016-02-16 05:21:17 +01:00
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2019-05-12 00:54:33 +02:00
|
|
|
private $rulesNamespaces = ['Respect\\Validation\\Rules'];
|
2018-01-06 14:49:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2019-05-12 00:54:33 +02:00
|
|
|
private $exceptionsNamespaces = ['Respect\\Validation\\Exceptions'];
|
2016-02-16 05:21:17 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
2019-05-12 00:54:33 +02:00
|
|
|
private $translator = 'strval';
|
2018-05-24 07:47:09 +02:00
|
|
|
|
2019-05-12 00:54:33 +02:00
|
|
|
public function withRuleNamespace(string $rulesNamespace): self
|
|
|
|
{
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->rulesNamespaces[] = trim($rulesNamespace, '\\');
|
|
|
|
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withExceptionNamespace(string $exceptionsNamespace): self
|
|
|
|
{
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->exceptionsNamespaces[] = trim($exceptionsNamespace, '\\');
|
|
|
|
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withTranslator(callable $translator): self
|
|
|
|
{
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->translator = $translator;
|
|
|
|
|
|
|
|
return $clone;
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
/**
|
|
|
|
* Define the default instance of the Factory.
|
|
|
|
*/
|
|
|
|
public static function setDefaultInstance(self $defaultInstance): void
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2018-01-06 14:49:38 +01:00
|
|
|
self::$defaultInstance = $defaultInstance;
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
/**
|
|
|
|
* Returns the default instance of the Factory.
|
|
|
|
*/
|
|
|
|
public static function getDefaultInstance(): self
|
2014-05-07 05:51:54 +02:00
|
|
|
{
|
2019-02-09 13:28:08 +01:00
|
|
|
if (self::$defaultInstance === null) {
|
2019-05-12 00:54:33 +02:00
|
|
|
self::$defaultInstance = new self();
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
return self::$defaultInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a rule.
|
|
|
|
*
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param mixed[] $arguments
|
2018-01-06 14:49:38 +01:00
|
|
|
*
|
|
|
|
* @throws ComponentException
|
|
|
|
*/
|
|
|
|
public function rule(string $ruleName, array $arguments = []): Validatable
|
|
|
|
{
|
|
|
|
foreach ($this->rulesNamespaces as $namespace) {
|
2018-07-25 10:02:14 +02:00
|
|
|
try {
|
2019-02-04 00:44:08 +01:00
|
|
|
/** @var Validatable $rule */
|
|
|
|
$rule = $this
|
2018-07-25 10:02:14 +02:00
|
|
|
->createReflectionClass($namespace.'\\'.ucfirst($ruleName), Validatable::class)
|
|
|
|
->newInstanceArgs($arguments);
|
2019-02-04 00:44:08 +01:00
|
|
|
|
|
|
|
return $rule;
|
2018-07-25 10:02:14 +02:00
|
|
|
} catch (ReflectionException $exception) {
|
2014-05-07 05:51:54 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-06 14:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an exception.
|
|
|
|
*
|
|
|
|
* @param mixed $input
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param mixed[] $extraParams
|
2018-01-06 14:49:38 +01:00
|
|
|
*
|
|
|
|
* @throws ComponentException
|
|
|
|
*/
|
|
|
|
public function exception(Validatable $validatable, $input, array $extraParams = []): ValidationException
|
|
|
|
{
|
|
|
|
$reflection = new ReflectionObject($validatable);
|
|
|
|
$ruleName = $reflection->getShortName();
|
2018-03-22 19:28:07 +01:00
|
|
|
$params = ['input' => $input] + $extraParams + $this->extractPropertiesValues($validatable, $reflection);
|
2018-05-24 07:47:09 +02:00
|
|
|
$id = lcfirst($ruleName);
|
|
|
|
if ($validatable->getName()) {
|
|
|
|
$id = $params['name'] = $validatable->getName();
|
|
|
|
}
|
2018-01-06 14:49:38 +01:00
|
|
|
foreach ($this->exceptionsNamespaces as $namespace) {
|
2018-07-25 10:02:14 +02:00
|
|
|
try {
|
|
|
|
return $this->createValidationException($namespace.'\\'.$ruleName.'Exception', $id, $input, $params);
|
|
|
|
} catch (ReflectionException $exception) {
|
2018-01-06 14:49:38 +01:00
|
|
|
continue;
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 10:02:14 +02:00
|
|
|
return new ValidationException($input, $id, $params, $this->translator);
|
2018-01-06 14:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a reflection based on class name.
|
|
|
|
*
|
|
|
|
* @throws InvalidClassException
|
2018-05-24 07:47:09 +02:00
|
|
|
* @throws ReflectionException
|
2018-01-06 14:49:38 +01:00
|
|
|
*/
|
|
|
|
private function createReflectionClass(string $name, string $parentName): ReflectionClass
|
|
|
|
{
|
|
|
|
$reflection = new ReflectionClass($name);
|
2018-03-22 19:28:07 +01:00
|
|
|
if (!$reflection->isSubclassOf($parentName) && $parentName !== $name) {
|
2018-01-06 14:49:38 +01:00
|
|
|
throw new InvalidClassException(sprintf('"%s" must be an instance of "%s"', $name, $parentName));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$reflection->isInstantiable()) {
|
|
|
|
throw new InvalidClassException(sprintf('"%s" must be instantiable', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $reflection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Validation exception.
|
|
|
|
*
|
2018-05-24 07:47:09 +02:00
|
|
|
* @param mixed $input
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param mixed[] $params
|
2018-01-06 14:49:38 +01:00
|
|
|
*
|
2018-05-24 07:47:09 +02:00
|
|
|
* @throws InvalidClassException
|
|
|
|
* @throws ReflectionException
|
2018-01-06 14:49:38 +01:00
|
|
|
*/
|
2019-02-03 18:58:30 +01:00
|
|
|
private function createValidationException(
|
|
|
|
string $exceptionName,
|
|
|
|
string $id,
|
|
|
|
$input,
|
|
|
|
array $params
|
|
|
|
): ValidationException {
|
2019-02-04 00:29:13 +01:00
|
|
|
/** @var ValidationException $exception */
|
2018-05-24 07:47:09 +02:00
|
|
|
$exception = $this->createReflectionClass($exceptionName, ValidationException::class)
|
|
|
|
->newInstance($input, $id, $params, $this->translator);
|
2018-01-06 14:49:38 +01:00
|
|
|
if (isset($params['template'])) {
|
2018-05-24 07:47:09 +02:00
|
|
|
$exception->updateTemplate($params['template']);
|
2018-01-06 14:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-07 21:22:10 +01:00
|
|
|
* @return mixed[]
|
2018-01-06 14:49:38 +01:00
|
|
|
*/
|
2018-01-23 21:59:49 +01:00
|
|
|
private function extractPropertiesValues(Validatable $validatable, ReflectionClass $reflection): array
|
2018-01-06 14:49:38 +01:00
|
|
|
{
|
|
|
|
$values = [];
|
|
|
|
foreach ($reflection->getProperties() as $property) {
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
$propertyValue = $property->getValue($validatable);
|
2019-02-09 13:28:08 +01:00
|
|
|
if ($propertyValue === null) {
|
2018-05-24 07:47:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$values[$property->getName()] = $propertyValue;
|
2018-01-06 14:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 12:15:13 +01:00
|
|
|
$parentReflection = $reflection->getParentClass();
|
2019-02-09 13:28:08 +01:00
|
|
|
if ($parentReflection !== false) {
|
2018-01-23 21:59:49 +01:00
|
|
|
return $values + $this->extractPropertiesValues($validatable, $parentReflection);
|
|
|
|
}
|
|
|
|
|
2018-01-06 14:49:38 +01:00
|
|
|
return $values;
|
2014-05-07 05:51:54 +02:00
|
|
|
}
|
|
|
|
}
|