本文整理汇总了PHP中Symfony\Component\Validator\Validator\ValidatorInterface类的典型用法代码示例。如果您正苦于以下问题:PHP ValidatorInterface类的具体用法?PHP ValidatorInterface怎么用?PHP ValidatorInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ValidatorInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1:
function it_does_not_validate_method_with_no_constraints(MethodInterface $method, ValidatorInterface $validator)
{
$constraints = [];
$method->getValidationConstraints()->shouldBeCalled()->willReturn($constraints);
$validator->validate([], $constraints)->shouldNotBeCalled();
$this->validate($method);
}
示例2: apply
public function apply(Request $request, ParamConverter $configuration)
{
$name = $configuration->getName();
$snakeCasedName = $this->camelCaseToSnakeCase($name);
$class = $configuration->getClass();
$json = $request->getContent();
$object = json_decode($json, true);
if (!isset($object[$snakeCasedName]) || !is_array($object[$snakeCasedName])) {
throw new BadJsonRequestException([sprintf("Missing parameter '%s'", $name)]);
}
$object = $object[$snakeCasedName];
$convertedObject = new $class();
$errors = [];
foreach ($object as $key => $value) {
$properlyCasedKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
if (!property_exists($convertedObject, $properlyCasedKey)) {
$errors[] = sprintf("Unknown property '%s.%s'", $snakeCasedName, $key);
continue;
}
$convertedObject->{$properlyCasedKey} = $value;
}
$violations = $this->validator->validate($convertedObject);
if (count($errors) + count($violations) > 0) {
throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, $errors);
}
$request->attributes->set($name, $convertedObject);
}
示例3: testExecuteWithoutViolations
public function testExecuteWithoutViolations()
{
$list = new ConstraintViolationList([]);
$this->validator->shouldReceive('validate')->once()->andReturn($list);
$this->middleware->execute(new FakeCommand(), function () {
});
}
示例4: validateCommand
/**
* @param mixed $command
*
* @return array
*/
private function validateCommand($command)
{
if ($this->validator) {
return $this->validator->validate($command);
}
return null;
}
示例5: validate
/**
*
* @param object $object
* @throws ValidationException
*/
public function validate($object)
{
$violations = $this->validator->validate($object);
if (count($violations) > 0) {
throw new ValidationException($violations);
}
}
示例6: testValidationConfiguration
public function testValidationConfiguration()
{
$valid = $this->validator->validate(new ProductCollection([]));
$this->assertCount(1, $valid);
$productCollectionWithWrongProduct = new ProductCollection([new Product()]);
$this->assertCount(3, $this->validator->validate($productCollectionWithWrongProduct));
}
示例7: validate
/**
* @param string $dataClass Parent entity class name
* @param File|Attachment $entity File entity
* @param string $fieldName Field name where new file/image field was added
*
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/
public function validate($dataClass, $entity, $fieldName = '')
{
/** @var Config $entityAttachmentConfig */
if ($fieldName === '') {
$entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass);
$mimeTypes = $this->getMimeArray($entityAttachmentConfig->get('mimetypes'));
if (!$mimeTypes) {
$mimeTypes = array_merge($this->getMimeArray($this->config->get('oro_attachment.upload_file_mime_types')), $this->getMimeArray($this->config->get('oro_attachment.upload_image_mime_types')));
}
} else {
$entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass, $fieldName);
/** @var FieldConfigId $fieldConfigId */
$fieldConfigId = $entityAttachmentConfig->getId();
if ($fieldConfigId->getFieldType() === 'file') {
$configValue = 'upload_file_mime_types';
} else {
$configValue = 'upload_image_mime_types';
}
$mimeTypes = $this->getMimeArray($this->config->get('oro_attachment.' . $configValue));
}
$fileSize = $entityAttachmentConfig->get('maxsize') * 1024 * 1024;
foreach ($mimeTypes as $id => $value) {
$mimeTypes[$id] = trim($value);
}
return $this->validator->validate($entity->getFile(), [new FileConstraint(['maxSize' => $fileSize, 'mimeTypes' => $mimeTypes])]);
}
示例8: validate
/**
* @param Validatable $comment
*
* @throws ValidationError
* @return void
*/
public function validate(Validatable $comment)
{
$errors = $this->validator->validate($comment);
if ($errors->count()) {
throw new ValidationError($errors);
}
}
示例9: testCreateUserWithDisabledValidationWillProceedImmediatelyToSave
public function testCreateUserWithDisabledValidationWillProceedImmediatelyToSave()
{
$user = $this->createExampleUser();
$this->validatorMock->expects($this->never())->method('validate');
$this->repositoryMock->expects($this->once())->method('save')->with($user);
$this->useCase->createUser($user, false);
}
示例10: tryValidate
private function tryValidate(Article $article)
{
$errors = $this->validator->validate($article);
if (count($errors)) {
throw new \Exception(implode('\\n', $errors));
}
}
示例11: validate
/**
* @inheritdoc
*/
public function validate($value, Param $param)
{
$constraint = $this->getRequirementsConstraint($value, $param);
if (null !== $constraint) {
$constraint = [$constraint];
if ($param->allowBlank === false) {
$constraint[] = new NotBlank();
}
if ($param->nullable === false) {
$constraint[] = new NotNull();
}
} else {
$constraint = [];
}
if ($param->array) {
$constraint = [new All(['constraints' => $constraint])];
}
if ($param->incompatibles) {
$constraint[] = new IncompatibleParams($param->incompatibles);
}
if (!count($constraint)) {
return new ConstraintViolationList();
}
return $this->validator->validate($value, $constraint);
}
示例12: validate
public function validate($value, Constraint $constraint)
{
if (!$value instanceof OrderInterface) {
$this->context->buildViolation('Value should implements OrderInterface')->addViolation();
}
if ($value->getUser() === null) {
$emailErrors = $this->validator->validate($value->getEmail(), [new NotNull(), new Email()]);
foreach ($emailErrors as $error) {
$this->context->buildViolation($error->getMessage())->addViolation();
}
}
$shippingAddressErrors = $this->validator->validate($value->getShippingAddress());
if (count($shippingAddressErrors)) {
/** @var ConstraintViolation $error */
foreach ($shippingAddressErrors as $error) {
$this->context->buildViolation($error->getMessage())->addViolation();
}
}
if ($value->isDifferentBillingAddress()) {
$billingAddressErrors = $this->validator->validate($value->getBillingAddress());
if (count($billingAddressErrors)) {
/** @var ConstraintViolation $error */
foreach ($billingAddressErrors as $error) {
$this->context->buildViolation($error->getMessage())->addViolation();
}
}
}
}
示例13: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (empty($options['data_class'])) {
return;
}
$className = $options['data_class'];
if (!$this->doctrineHelper->isManageableEntity($className)) {
return;
}
if (!$this->entityConfigProvider->hasConfig($className)) {
return;
}
$uniqueKeys = $this->entityConfigProvider->getConfig($className)->get('unique_key');
if (empty($uniqueKeys)) {
return;
}
/* @var \Symfony\Component\Validator\Mapping\ClassMetadata $validatorMetadata */
$validatorMetadata = $this->validator->getMetadataFor($className);
foreach ($uniqueKeys['keys'] as $uniqueKey) {
$fields = $uniqueKey['key'];
$labels = array_map(function ($fieldName) use($className) {
$label = $this->entityConfigProvider->getConfig($className, $fieldName)->get('label');
return $this->translator->trans($label);
}, $fields);
$constraint = new UniqueEntity(['fields' => $fields, 'errorPath' => '', 'message' => $this->translator->transChoice('oro.entity.validation.unique_field', sizeof($fields), ['%field%' => implode(', ', $labels)])]);
$validatorMetadata->addConstraint($constraint);
}
}
示例14: process
/**
* {@inheritdoc}
*/
public function process($item)
{
$entity = $this->findOrCreateObject($item);
try {
$this->updater->update($entity, $item);
} catch (\InvalidArgumentException $exception) {
$this->skipItemWithMessage($item, $exception->getMessage(), $exception);
}
$violations = $this->validator->validate($entity);
if ($violations->count() > 0) {
$this->objectDetacher->detach($entity);
$this->skipItemWithConstraintViolations($item, $violations);
}
$rawParameters = $entity->getRawParameters();
if (!empty($rawParameters)) {
$job = $this->jobRegistry->get($entity->getJobName());
$parameters = $this->jobParamsFactory->create($job, $rawParameters);
$violations = $this->jobParamsValidator->validate($job, $parameters);
if ($violations->count() > 0) {
$this->objectDetacher->detach($entity);
$this->skipItemWithConstraintViolations($item, $violations);
}
}
return $entity;
}
示例15: validate
/**
* {@inheritdoc}
*/
public function validate(ServiceReference $service, array $arguments)
{
$validationResult = array();
$parameterCount = 0;
$validatedCount = 0;
$hasStrictFailure = false;
foreach ($arguments as $name => $value) {
if (strpos($name, '__internal__') !== false) {
continue;
}
$constraints = $service->getParameterConstraints($name);
$validationGroups = $service->getParameterValidationGroups($name);
$isStrictValidation = $service->isStrictParameterValidation($name);
if (!empty($constraints)) {
$violations = $this->validator->validate($value, $constraints, $validationGroups);
if (count($violations)) {
$validationResult[$name] = $violations;
if ($isStrictValidation) {
$hasStrictFailure = true;
}
}
$validatedCount++;
}
$parameterCount++;
}
if ($this->strict && $parameterCount !== $validatedCount) {
throw new StrictArgumentValidationException();
}
if (!empty($validationResult)) {
throw new ArgumentValidationException(new ArgumentValidationResult($validationResult), $hasStrictFailure);
}
}