Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SourceValidator.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Framework\Validation\ValidationResultFactory;
13 
18 {
22  private $validationResultFactory;
23 
27  private $sourceRepository;
28 
32  private $sourceCodes = [];
33 
38  public function __construct(
39  ValidationResultFactory $validationResultFactory,
40  SourceRepositoryInterface $sourceRepository
41  ) {
42  $this->validationResultFactory = $validationResultFactory;
43  $this->sourceRepository = $sourceRepository;
44  $this->loadSourceCodes();
45  }
46 
50  public function validate(array $rowData, int $rowNumber)
51  {
52  $errors = [];
53 
54  if (!isset($rowData[Sources::COL_SOURCE_CODE])) {
55  $errors[] = __('Missing required column "%column"', ['column' => Sources::COL_SOURCE_CODE]);
56  } elseif (!$this->isExistingSource($rowData[Sources::COL_SOURCE_CODE])) {
57  $errors[] = __('Source code "%code" does not exists', ['code' => $rowData[Sources::COL_SOURCE_CODE]]);
58  }
59 
60  return $this->validationResultFactory->create(['errors' => $errors]);
61  }
62 
69  private function isExistingSource($sourceCode): bool
70  {
71  return isset($this->sourceCodes[$sourceCode]);
72  }
73 
79  private function loadSourceCodes()
80  {
81  $sources = $this->sourceRepository->getList();
82  foreach ($sources->getItems() as $source) {
83  $sourceCode = $source->getSourceCode();
84  $this->sourceCodes[$sourceCode] = $sourceCode;
85  }
86  }
87 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
__()
Definition: __.php:13
$sourceCode
Definition: inventory.phtml:11
$sourceRepository
Definition: source.php:20
$errors
Definition: overview.phtml:9
__construct(ValidationResultFactory $validationResultFactory, SourceRepositoryInterface $sourceRepository)