0
0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2024-11-24 00:29:35 +00:00
salesagility_SuiteCRM/Api/V8/Factory/ValidatorFactory.php
2023-07-18 15:53:08 +01:00

68 lines
1.6 KiB
PHP

<?php
namespace Api\V8\Factory;
use Symfony\Component\Validator\Validator\ValidatorInterface;
#[\AllowDynamicProperties]
class ValidatorFactory
{
/**
* @var ValidatorInterface
*/
protected $validator;
/**
* @param ValidatorInterface $validator
*/
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
/**
* @param \Symfony\Component\Validator\Constraint[] $constraints
* @param boolean $allowNull
*
* @return \Closure
*/
public function createClosure(array $constraints, $allowNull = false)
{
return function ($value) use ($constraints, $allowNull) {
if ($allowNull && $value === null) {
return true;
}
$violations = $this->validator->validate($value, $constraints);
return !$violations->count();
};
}
/**
* @param \Symfony\Component\Validator\Constraint[] $constraints
* @param boolean $allowNull
*
* @return \Closure
*/
public function createClosureForIterator(array $constraints, $allowNull = false)
{
return function ($value) use ($constraints, $allowNull) {
if ($allowNull && $value === null) {
return true;
}
if (!is_array($value) && !$value instanceof \Iterator) {
return false;
}
foreach ($value as $v) {
if ($this->validator->validate($v, $constraints)->count()) {
return false;
}
}
return true;
};
}
}