24 $propertyResult = $this->validateByPropertyAttributes($object);
25 $result->addErrors($propertyResult->getErrors());
27 $classResult = $this->validateByClassAttributes($object);
28 $result->addErrors($classResult->getErrors());
37 $attributes = $this->getValidationAttributes($parameter);
39 $name = $parameter->getName();
41 $generator = $this->validateValue($value,
$name, $attributes);
42 $errors = iterator_to_array($generator);
51 $class =
new ReflectionClass($object);
52 $attributes = $class->getAttributes(ClassValidationAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF);
54 if (empty($attributes))
59 foreach ($attributes as $attribute)
61 $attributeInstance = $attribute->newInstance();
62 $attributeErrors = $attributeInstance->validateObject($object)->getErrors();
63 $result->addErrors($attributeErrors);
69 private function validateByPropertyAttributes(
object $object): ValidationResult
71 $result =
new ValidationResult();
73 $properties = (
new ReflectionClass($object))->getProperties();
74 foreach ($properties as $property)
76 if ($property->isInitialized($object))
78 $generator = $this->validateProperty($property, $object);
79 $errors = iterator_to_array($generator);
85 $type = $property->getType();
91 if (
$type->allowsNull())
96 if (!
$type->allowsNull())
98 $result->addError(
new ValidationError(
99 new LocalizableMessage(
'MAIN_VALIDATION_EMPTY_PROPERTY'),
108 private function validateProperty(ReflectionProperty $property,
object $object): Generator
110 $attributes = $this->getValidationAttributes($property);
112 $name = $property->getName();
113 $value = $property->getValue($object);
115 yield from $this->validateValue($value,
$name, $attributes);
118 private function validateValue(mixed $value,
string $name,
array $attributes): Generator
120 foreach ($attributes as $attribute)
122 $attributeInstance = $attribute->newInstance();
124 if ($attributeInstance instanceof Validatable)
126 yield from $this->setErrorCodes(
128 $this->validateValidatableProperty($value, $attributeInstance)
131 elseif ($attributeInstance instanceof PropertyValidationAttributeInterface)
133 yield from $this->setErrorCodes(
135 $attributeInstance->validateProperty($value)->getErrors()
141 private function validateValidatableProperty(mixed $value, Validatable $attributeInstance): Generator
148 if (!$attributeInstance->iterable)
150 if (!is_object($value))
152 throw new ArgumentException(
'Only objects can be marked as Validatable');
155 yield from $this->
validate($value)->getErrors();
160 if (!is_iterable($value))
162 throw new ArgumentException(
'Only iterable values can be marked as Validatable when "iterable" is true');
165 foreach ($value as
$i => $item)
167 if (!is_object($item))
169 throw new ArgumentException(
'Only objects can be Validatable inside an iterable');
172 $attributeErrors = $this->
validate($item)->getErrors();
174 yield from $this->setErrorCodes((
string)
$i, $attributeErrors);
178 private function setErrorCodes(
string $name, iterable
$errors): Generator
182 if (
$error instanceof ValidationError)
191 private function getValidationAttributes(ReflectionParameter|ReflectionProperty $parameter):
array
194 $parameter->getAttributes(Validatable::class, ReflectionAttribute::IS_INSTANCEOF),
195 $parameter->getAttributes(PropertyValidationAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF)
validateParameter(ReflectionParameter $parameter, mixed $value)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)