Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContentValidator.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Downloadable\Model\File\ContentValidator as FileContentValidator;
11 use Magento\Framework\Url\Validator as UrlValidator;
12 
14 {
18  protected $urlValidator;
19 
24 
29  public function __construct(
30  FileContentValidator $fileContentValidator,
31  UrlValidator $urlValidator
32  ) {
33  $this->fileContentValidator = $fileContentValidator;
34  $this->urlValidator = $urlValidator;
35  }
36 
45  public function isValid(SampleInterface $sample, $validateSampleContent = true)
46  {
47  if (filter_var($sample->getSortOrder(), FILTER_VALIDATE_INT) === false || $sample->getSortOrder() < 0) {
48  throw new InputException(__('Sort order must be a positive integer.'));
49  }
50 
51  if ($validateSampleContent) {
52  $this->validateSampleResource($sample);
53  }
54  return true;
55  }
56 
64  protected function validateSampleResource(SampleInterface $sample)
65  {
66  $sampleFile = $sample->getSampleFileContent();
67  if ($sample->getSampleType() == 'file'
68  && (!$sampleFile || !$this->fileContentValidator->isValid($sampleFile))
69  ) {
70  throw new InputException(__('Provided file content must be valid base64 encoded data.'));
71  }
72 
73  if ($sample->getSampleType() == 'url'
74  && !$this->urlValidator->isValid($sample->getSampleUrl())
75  ) {
76  throw new InputException(__('Sample URL must have valid format.'));
77  }
78  }
79 }
__()
Definition: __.php:13
__construct(FileContentValidator $fileContentValidator, UrlValidator $urlValidator)
isValid(SampleInterface $sample, $validateSampleContent=true)