Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Sales\Model;
8 
11 
17 class Validator
18 {
22  private $objectManager;
23 
27  private $validatorResultFactory;
28 
35  public function __construct(
36  ObjectManagerInterface $objectManager,
37  ValidatorResultInterfaceFactory $validatorResult
38  ) {
39  $this->objectManager = $objectManager;
40  $this->validatorResultFactory = $validatorResult;
41  }
42 
50  public function validate($entity, array $validators, $context = null)
51  {
52  $messages = [];
53  $validatorArguments = [];
54  if ($context !== null) {
55  $validatorArguments['context'] = $context;
56  }
57 
58  foreach ($validators as $validatorName) {
59  $validator = $this->objectManager->create($validatorName, $validatorArguments);
60  if (!$validator instanceof ValidatorInterface) {
62  __('The "%1" validator is not an instance of the general validator interface.', $validatorName)
63  );
64  }
65  $messages = array_merge($messages, $validator->validate($entity));
66  }
67  $validationResult = $this->validatorResultFactory->create();
68  foreach ($messages as $message) {
69  $validationResult->addMessage($message);
70  }
71 
72  return $validationResult;
73  }
74 }
__construct(ObjectManagerInterface $objectManager, ValidatorResultInterfaceFactory $validatorResult)
Definition: Validator.php:35
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$message
$entity
Definition: element.phtml:22
validate($entity, array $validators, $context=null)
Definition: Validator.php:50