I am a beginner in PHP. Here is my trait. I want to inside these trait function in several ways. So I have created a trait and ass the function. The purpose of reducing code duplications happen.
<?php
namespace App\Validation\Common;
use App\Entity\AbstractCustomField;
use Doctrine\ORM\EntityManagerInterface;
use SimitiveTranslationsBundle\Provider\TranslationsProvider;
trait ValueValidationTrait{
public function __construct(
protected EntityManagerInterface $entityManager,
private TranslationsProvider $translationsProvider,
) {
}
public function validateCustomFieldTypesWithValues(string $type, string $value): void
{
switch ($type) {
case AbstractCustomField::TYPE_BOOLEAN:
if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null) {
$this->addViolation($this->translationsProvider->translate(
TranslationsProvider::DEFAULT_LANGUAGE,
'entity.user_custom_field_value.message.invalid_boolean_value'
));
break;
}
break;
case AbstractCustomField::TYPE_NUMERIC_INTEGER:
if (
filter_var($value, FILTER_VALIDATE_INT) !== 0 &&
!(filter_var($value, FILTER_VALIDATE_INT))
) {
$this->addViolation(
$this->translationsProvider->translate(
TranslationsProvider::DEFAULT_LANGUAGE,
'entity.user_custom_field_value.message.invalid_numeric_value'
)
);
break;
}
break;
default:
$this->addViolation(
$this->translationsProvider->translate(
TranslationsProvider::DEFAULT_LANGUAGE,
'entity.user_custom_field_value.message.invalid_custom_field_type'
)
);
break;
}
}
}
and this is my class which I want to call function, I have created function inside trait.
?php
namespace App\Validation\User;
use App\Entity\AbstractCustomField;
use App\Entity\CustomField\UserCustomFieldValue;
use App\Entity\CustomField\UserCustomFieldValueOption;
use Doctrine\ORM\EntityManagerInterface;
use SimitiveTranslationsBundle\Provider\TranslationsProvider;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use ValueValidationTrait;
/**
* Validates user custom field values.
*/
class UserCustomFieldValueConstraintValidator extends ConstraintValidator
{
public function validate($entity, Constraint $constraint): void
{
if (!$constraint instanceof UserCustomFieldValueConstraint) {
throw new UnexpectedTypeException(
$constraint,
UserCustomFieldValueConstraint::class
);
}
$userCustomFieldValues = $entity->getUserCustomFieldValues();
if (!empty($userCustomFieldValues)) {
foreach ($userCustomFieldValues as $userCustomFieldValue) {
$type = $userCustomFieldValue->getUserCustomField()->getType();
$value = $userCustomFieldValue->getValue();
$userCustomFieldValueOption = $userCustomFieldValue->getUserCustomFieldValueOption();
if (
in_array(
$type,
AbstractCustomField::getCustomFieldTypesWithValues()
) &&
!empty($value)
) {
$this->validateCustomFieldTypesWithValues($type, $value);
}
if (
in_array($type, AbstractCustomField::getCustomFieldsTypesWithOptions()) &&
!empty($userCustomFieldValueOption)
) {
$this->validateUserCustomFieldTypesWithOptions(
$userCustomFieldValueOption,
$userCustomFieldValue
);
}
}
}
}
}
If I call$this->validateCustomFieldTypesWithValues($type, $value);
as inside trait validateCustomFieldTypesWithValues(string $type, string $value)
function. It gives an error. Can you suggest to me the way to call the trait function
CodePudding user response:
I have found the solution by existing code bases. trait should be use inside a class.
CodePudding user response:
You need to include trait in the class.
class UserCustomFieldValueConstraintValidator extends ConstraintValidator
{
use ValueValidationTrait;
//other functions
}