metadata = $metadata; $this->fieldUtil = $fieldUtil; $this->factory = $factory; } public function check(Entity $entity, string $field, string $type, ?StdClass $data = null) : bool { if (!$data) { $data = $data ?? (object) []; } $entityType = $entity->getEntityType(); $fieldType = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, 'type'); $validationValue = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, $type); $mandatoryValidationList = $this->metadata->get(['fields', $fieldType, 'mandatoryValidationList'], []); if (!in_array($type, $mandatoryValidationList)) { if (is_null($validationValue) || $validationValue === false) { return true; } } $result = $this->processFieldCheck($entityType, $type, $entity, $field, $validationValue); if (!$result) { return false; } $resultRaw = $this->processFieldRawCheck($entityType, $type, $data, $field, $validationValue); if (!$resultRaw) { return false; } return true; } private function processFieldCheck( string $entityType, string $type, Entity $entity, string $field, $validationValue ) : bool { $checker = $this->getFieldTypeChecker($entityType, $field); if (!$checker) { return true; } $methodName = 'check' . ucfirst($type); if (!method_exists($checker, $methodName)) { return true; } return $checker->$methodName($entity, $field, $validationValue); } private function processFieldRawCheck( string $entityType, string $type, StdClass $data, string $field, $validationValue ) : bool { $checker = $this->getFieldTypeChecker($entityType, $field); if (!$checker) { return true; } $methodName = 'rawCheck' . ucfirst($type); if (!method_exists($checker, $methodName)) { return true; } return $checker->$methodName($data, $field, $validationValue); } private function getFieldTypeChecker(string $entityType, string $field) : ?object { $key = $entityType . '_' . $field; if (!array_key_exists($key, $this->checkerCache)) { $this->loadFieldTypeChecker($entityType, $field); } return $this->checkerCache[$key]; } private function loadFieldTypeChecker(string $entityType, string $field) : void { $key = $entityType . '_' . $field; if (!$this->factory->isCreatable($entityType, $field)) { $this->checkerCache[$key] = null; return; } $this->checkerCache[$key] = $this->factory->create($entityType, $field); } }