Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
use Espo\Core\Utils\Metadata;
use Espo\Core\InjectableFactory;
use RuntimeException;
class CheckerFactory
{
/** @var array<string, ?class-string> */
private $classNameCache = [];
public function __construct(
private Metadata $metadata,
private InjectableFactory $injectableFactory
) {}
/**
* Whether a field validator can be created.
*/
public function isCreatable(string $entityType, string $field): bool
{
return (bool) $this->getClassName($entityType, $field);
}
/**
* Create a validator (checker).
*
* @throws RuntimeException
*/
public function create(string $entityType, string $field): object
{
$className = $this->getClassName($entityType, $field);
if (!$className) {
throw new RuntimeException("Validator for '{$entityType}.{$field}' does not exist.");
}
return $this->injectableFactory->create($className);
}
/**
* @return ?class-string
*/
private function getClassName(string $entityType, string $field): ?string
{
$key = $entityType . '_' . $field;
if (!array_key_exists($key, $this->classNameCache)) {
$this->classNameCache[$key] = $this->getClassNameNoCache($entityType, $field);
}
return $this->classNameCache[$key];
}
/**
* @return ?class-string
*/
private function getClassNameNoCache(string $entityType, string $field): ?string
{
$className1 = $this->metadata
->get(['entityDefs', $entityType, 'fields', $field, 'validatorClassName']);
if ($className1) {
return $className1;
}
$fieldType = $this->metadata
->get(['entityDefs', $entityType, 'fields', $field, 'type']);
$className2 = $this->metadata
->get(['fields', $fieldType, 'validatorClassName']);
if ($className2) {
return $className2;
}
$className3 = 'Espo\\Classes\\FieldValidators\\' . ucfirst($fieldType) . 'Type';
if (class_exists($className3)) {
return $className3;
}
return null;
}
}

View File

@@ -0,0 +1,101 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation\Exceptions;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error\Body;
use Espo\Core\Exceptions\HasLogMessage;
use Espo\Core\FieldValidation\Failure;
use LogicException;
class ValidationError extends BadRequest implements HasLogMessage
{
private ?Failure $failure = null;
/**
* Create with a body (supposed to be sent to the frontend).
* Body object is supported since v8.1.
*/
public static function createWithBody(string $reason, string|Body $body): self
{
if ($body instanceof Body) {
$body = $body->encode();
}
$exception = parent::createWithBody($reason, $body);
if (!$exception instanceof self) {
throw new LogicException();
}
return $exception;
}
public static function create(Failure $failure): self
{
$exception = self::createWithBody(
'validationFailure',
Body::create()
->withMessageTranslation('validationFailure', null, [
'field' => $failure->getField(),
'type' => $failure->getType(),
])
->encode()
);
$exception->failure = $failure;
return $exception;
}
public function getFailure(): Failure
{
if (!$this->failure) {
throw new LogicException();
}
return $this->failure;
}
public function getLogMessage(): string
{
if (!$this->failure) {
return "Field validation failure.";
}
$entityType = $this->failure->getEntityType();
$field = $this->failure->getField();
$type = $this->failure->getType();
return "Field validation failure; " .
"entityType: {$entityType}, field: {$field}, type: {$type}.";
}
}

View File

@@ -0,0 +1,54 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
class Failure
{
public function __construct(
private string $entityType,
private string $field,
private string $type
) {}
public function getEntityType(): string
{
return $this->entityType;
}
public function getField(): string
{
return $this->field;
}
public function getType(): string
{
return $this->type;
}
}

View File

@@ -0,0 +1,504 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
use Espo\Core\Utils\Log;
use Espo\ORM\Defs;
use Espo\ORM\Entity;
use Espo\Core\FieldValidation\Exceptions\ValidationError;
use Espo\Core\FieldValidation\Validator\Data;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\FieldUtil;
use Espo\Tools\DynamicLogic\ConditionCheckerFactory;
use Espo\Tools\DynamicLogic\Exceptions\BadCondition;
use Espo\Tools\DynamicLogic\Item as LogicItem;
use LogicException;
use stdClass;
use ReflectionClass;
/**
* A field validation manager.
*/
class FieldValidationManager
{
/** @var array<string, ?object> */
private array $checkerCache = [];
/** @var array<string, ?Validator<Entity>> */
private array $validatorCache = [];
private CheckerFactory $checkerFactory;
public function __construct(
private Metadata $metadata,
private FieldUtil $fieldUtil,
CheckerFactory $factory,
private ValidatorFactory $validatorFactory,
private Defs $defs,
private ConditionCheckerFactory $conditionCheckerFactory,
private Log $log,
) {
$this->checkerFactory = $factory;
}
/**
* Process validation.
*
* @param Entity $entity An entity.
* @param ?stdClass $data Raw request payload data.
* @param ?FieldValidationParams $params Validation additional parameters.
*
* @throws ValidationError On the first invalid check.
*/
public function process(Entity $entity, ?stdClass $data = null, ?FieldValidationParams $params = null): void
{
$this->processInternal($entity, $data, $params, true);
}
/**
* Process validation w/o exception throwing.
*
* @param Entity $entity An entity.
* @param ?stdClass $data Raw request payload data.
* @param ?FieldValidationParams $params Validation additional parameters.
* @return Failure[] A list of validation failures.
*/
public function processAll(Entity $entity, ?stdClass $data = null, ?FieldValidationParams $params = null): array
{
try {
return $this->processInternal($entity, $data, $params, false);
} catch (ValidationError) {
throw new LogicException();
}
}
/**
* @return Failure[]
* @throws ValidationError On the first invalid check.
*/
private function processInternal(
Entity $entity,
?stdClass $data,
?FieldValidationParams $params,
bool $throw
): array {
$dataIsSet = $data !== null;
$entityType = $entity->getEntityType();
$data ??= (object) [];
$params ??= new FieldValidationParams();
$failureList = [];
$fieldList = $this->getFieldList($entityType, $params);
$entityDefs = $this->defs->getEntity($entityType);
foreach ($fieldList as $field) {
$typeList = null;
if (
!$entity->isNew() &&
$dataIsSet &&
!$entityDefs->tryGetField($field)?->getParam('forceValidation') &&
!$this->isFieldSetInData($entityType, $field, $data)
) {
if (!$this->hasRequiredLogic($entityType, $field)) {
continue;
}
$typeList = [Type::REQUIRED];
}
$itemFailureList = $this->processField(
entity: $entity,
field: $field,
params: $params,
data: $data,
throw: $throw,
typeList: $typeList,
);
$failureList = array_merge($failureList, $itemFailureList);
}
return $failureList;
}
/**
* @return string[]
*/
private function getMandatoryValidationList(string $entityType, string $field): array
{
/** @var ?string $fieldType */
$fieldType = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, 'type');
return
$this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'mandatoryValidationList']) ??
$this->metadata->get(['fields', $fieldType ?? '', 'mandatoryValidationList']) ?? [];
}
/**
* @return string[]
*/
private function getValidationList(string $entityType, string $field): array
{
/** @var ?string $fieldType */
$fieldType = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, 'type');
return
$this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'validationList']) ??
$this->metadata->get(['fields', $fieldType ?? '', 'validationList']) ?? [];
}
/**
* Check a specific field for a specific validation type.
*
* @param Entity $entity An entity to check.
* @param string $field A field to check.
* @param string $type A validation type.
* @param ?stdClass $data A payload.
* @param mixed $value To override a validation value.
*/
public function check(
Entity $entity,
string $field,
string $type,
?stdClass $data = null,
mixed $value = null
): bool {
$data ??= (object) [];
$entityType = $entity->getEntityType();
$result = $this->processValidator($entity, $field, $type, new Data($data));
if (!$result) {
return false;
}
$validationValue = $value ?? $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, $type);
$isMandatory = in_array($type, $this->getMandatoryValidationList($entityType, $field));
if (
$type === Type::REQUIRED &&
!$validationValue &&
$this->checkDynamicLogicRequired($entity, $field)
) {
$validationValue = true;
}
$skip = !$isMandatory && (is_null($validationValue) || $validationValue === false);
if ($skip) {
return true;
}
$result1 = $this->processFieldCheck($entityType, $type, $entity, $field, $validationValue);
if (!$result1) {
return false;
}
$result2 = $this->processFieldRawCheck($entityType, $type, $data, $field, $validationValue);
if (!$result2) {
return false;
}
return true;
}
private function processValidator(Entity $entity, string $field, string $type, Data $data): bool
{
$validator = $this->getValidator($entity->getEntityType(), $field, $type);
if (!$validator) {
return true;
}
$failure = $validator->validate($entity, $field, $data);
if ($failure) {
return false;
}
return true;
}
/**
* @return ?Validator<Entity>
*/
private function getValidator(string $entityType, string $field, string $type): ?Validator
{
$key = $entityType . '_' . $field . '_' . $type;
if (array_key_exists($key, $this->validatorCache)) {
return $this->validatorCache[$key];
}
if (!$this->validatorFactory->isCreatable($entityType, $field, $type)) {
$this->validatorCache[$key] = null;
return null;
}
$validator = $this->validatorFactory->create($entityType, $field, $type);
$this->validatorCache[$key] = $validator;
return $validator;
}
/**
* @param ?string[] $typeList
* @return Failure[]
* @throws ValidationError
*/
private function processField(
Entity $entity,
string $field,
FieldValidationParams $params,
stdClass $data,
bool $throw,
?array $typeList = null,
): array {
$validationList = $this->getAllValidationList($entity->getEntityType(), $field, $params);
foreach ($validationList as $type) {
if ($typeList !== null && !in_array($type, $typeList)) {
continue;
}
$result = $this->check($entity, $field, $type, $data);
if ($result) {
continue;
}
$failure = new Failure($entity->getEntityType(), $field, $type);
if ($throw) {
throw ValidationError::create($failure);
}
return [$failure];
}
$failure = $this->checkAdditional($entity, $field, new Data($data));
if (!$failure) {
return [];
}
if ($throw) {
throw ValidationError::create($failure);
}
return [$failure];
}
/**
* @return string[]
*/
private function getAllValidationList(string $entityType, string $field, FieldValidationParams $params): array
{
$validationList = array_unique(array_merge(
$this->getValidationList($entityType, $field),
$this->getMandatoryValidationList($entityType, $field)
));
/** @var string[] $suppressList */
$suppressList = $this->metadata->get("entityDefs.$entityType.fields.$field.suppressValidationList") ?? [];
$validationList = array_filter(
$validationList,
fn ($type) => !in_array($type, $suppressList)
);
$validationList = array_filter(
$validationList,
fn ($type) => !in_array($field, $params->getTypeSkipFieldList($type))
);
return array_values($validationList);
}
/**
* @param mixed $validationValue
*/
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);
}
/**
* @param mixed $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->checkerFactory->isCreatable($entityType, $field)) {
$this->checkerCache[$key] = null;
return;
}
$this->checkerCache[$key] = $this->checkerFactory->create($entityType, $field);
}
private function isFieldSetInData(string $entityType, string $field, stdClass $data): bool
{
$attributeList = $this->fieldUtil->getActualAttributeList($entityType, $field);
$isSet = false;
foreach ($attributeList as $attribute) {
if (property_exists($data, $attribute)) {
$isSet = true;
break;
}
}
return $isSet;
}
private function checkAdditional(Entity $entity, string $field, Data $data): ?Failure
{
$validatorList = $this->validatorFactory->createAdditionalList($entity->getEntityType(), $field);
foreach ($validatorList as $validator) {
$itemFailure = $validator->validate($entity, $field, $data);
if (!$itemFailure) {
continue;
}
$type = lcfirst((new ReflectionClass($validator))->getShortName());
return new Failure($entity->getEntityType(), $field, $type);
}
return null;
}
private function checkDynamicLogicRequired(Entity $entity, string $field): bool
{
$entityType = $entity->getEntityType();
/** @var stdClass[] $group */
$group = $this->metadata->getObjects("logicDefs.$entityType.fields.$field.required.conditionGroup");
if (!is_array($group)) {
return false;
}
$checker = $this->conditionCheckerFactory->create($entity);
try {
$item = LogicItem::fromGroupDefinition($group);
return $checker->check($item);
} catch (BadCondition $e) {
$this->log->warning("Bad logic condition for $entityType $field.", ['exception' => $e]);
}
return false;
}
/**
* @return string[]
*/
private function getFieldList(string $entityType, FieldValidationParams $params): array
{
return array_filter(
$this->fieldUtil->getEntityTypeFieldList($entityType),
fn($field) => !in_array($field, $params->getSkipFieldList())
);
}
private function hasRequiredLogic(string $entityType, string $field): bool
{
return (bool) $this->metadata->get("logicDefs.$entityType.fields.$field.required");
}
}

View File

@@ -0,0 +1,96 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
class FieldValidationParams
{
/** @var string[] */
private $skipFieldList = [];
/** @var array<string, string[]> */
private $typeSkipFieldListData = [];
public function __construct() {}
/**
* A field list that will be skipped when validating.
*
* @return string[] A field list.
*/
public function getSkipFieldList(): array
{
return $this->skipFieldList;
}
/**
* A field list that will be skipped in validation for a specific validation type.
*
* @param string $type A validation type.
* @return string[] A field list.
*/
public function getTypeSkipFieldList(string $type): array
{
return $this->typeSkipFieldListData[$type] ?? [];
}
/**
* Clone with a specified field list that will be skipped when validating.
*
* @param string[] $list A field list.
*/
public function withSkipFieldList(array $list): self
{
$obj = clone $this;
$obj->skipFieldList = $list;
return $obj;
}
/**
* Clone with a specified field list that will be skipped in validation for a specific validation type.
*
* @param string $type A validation type.
* @param string[] $list A field list.
*/
public function withTypeSkipFieldList(string $type, array $list): self
{
$obj = clone $this;
$obj->typeSkipFieldListData[$type] = $list;
return $obj;
}
/**
* Create an empty instance.
*/
public static function create(): self
{
return new self();
}
}

View File

@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
/**
* @since 9.1.0
*/
class Type
{
public const REQUIRED = 'required';
}

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
use Espo\ORM\Entity;
use Espo\Core\FieldValidation\Validator\Data;
use Espo\Core\FieldValidation\Validator\Failure;
/**
* A field validator. Checks whether field values are valid. Raw payload values are available in the `data`.
* Values that managed to be set to the Entity can be obtained from the `entity`.
*
* Note: Validators are not supposed to perform any access control checks.
*
* @template TEntity of Entity
*/
interface Validator
{
/**
* @param TEntity $entity
*/
public function validate(Entity $entity, string $field, Data $data): ?Failure;
}

View File

@@ -0,0 +1,54 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation\Validator;
use Espo\Core\Utils\ObjectUtil;
use stdClass;
class Data
{
public function __construct(private stdClass $data)
{}
public function has(string $name): bool
{
return property_exists($this->data, $name);
}
public function get(string $name): mixed
{
return $this->getClonedData()->$name ?? null;
}
private function getClonedData(): stdClass
{
return ObjectUtil::clone($this->data);
}
}

View File

@@ -0,0 +1,41 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation\Validator;
class Failure
{
private function __construct()
{}
public static function create(): self
{
return new self();
}
}

View File

@@ -0,0 +1,97 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldValidation;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\FieldUtil;
use Espo\ORM\Entity;
use RuntimeException;
class ValidatorFactory
{
public function __construct(
private InjectableFactory $injectableFactory,
private Metadata $metadata,
private FieldUtil $fieldUtil
) {}
public function isCreatable(string $entityType, string $field, string $type): bool
{
return $this->getClassName($entityType, $field, $type) !== null;
}
/**
* @return Validator<Entity>
*/
public function create(string $entityType, string $field, string $type): Validator
{
$className = $this->getClassName($entityType, $field, $type);
if (!$className) {
throw new RuntimeException("No validator.");
}
return $this->injectableFactory->create($className);
}
/**
* @return ?class-string<Validator<Entity>>
*/
private function getClassName(string $entityType, string $field, string $type): ?string
{
/** @var ?string $fieldType */
$fieldType = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, 'type');
return
$this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'validatorClassNameMap', $type]) ??
$this->metadata->get(['fields', $fieldType ?? '', 'validatorClassNameMap', $type]);
}
/**
* @return Validator<Entity>[]
*/
public function createAdditionalList(string $entityType, string $field): array
{
/** @var class-string<Validator<Entity>>[] $classNameList */
$classNameList = $this->metadata
->get(['entityDefs', $entityType, 'fields', $field, 'validatorClassNameList']) ?? [];
$list = [];
foreach ($classNameList as $className) {
$list[] = $this->injectableFactory->create($className);
}
return $list;
}
}