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 {
19 
23  protected $urlValidator;
24 
29  public function __construct(
30  FileContentValidator $fileContentValidator,
31  UrlValidator $urlValidator
32  ) {
33  $this->fileContentValidator = $fileContentValidator;
34  $this->urlValidator = $urlValidator;
35  }
36 
46  public function isValid(LinkInterface $link, $validateLinkContent = true, $validateSampleContent = true)
47  {
48  if (!is_numeric($link->getPrice()) || $link->getPrice() < 0) {
49  throw new InputException(__('Link price must have numeric positive value.'));
50  }
51  if (filter_var($link->getNumberOfDownloads(), FILTER_VALIDATE_INT) === false
52  || $link->getNumberOfDownloads() < 0) {
53  throw new InputException(__('Number of downloads must be a positive integer.'));
54  }
55  if (filter_var($link->getSortOrder(), FILTER_VALIDATE_INT) === false
56  || $link->getSortOrder() < 0) {
57  throw new InputException(__('Sort order must be a positive integer.'));
58  }
59 
60  if ($validateLinkContent) {
61  $this->validateLinkResource($link);
62  }
63  if ($validateSampleContent) {
64  $this->validateSampleResource($link);
65  }
66  return true;
67  }
68 
77  {
78  if ($link->getLinkType() == 'url'
79  && !$this->urlValidator->isValid($link->getLinkUrl())
80  ) {
81  throw new InputException(__('Link URL must have valid format.'));
82  }
83  if ($link->getLinkType() == 'file'
84  && (!$link->getLinkFileContent()
85  || !$this->fileContentValidator->isValid($link->getLinkFileContent()))
86  ) {
87  throw new InputException(__('Provided file content must be valid base64 encoded data.'));
88  }
89  }
90 
99  {
100  if ($link->getSampleType() == 'url'
101  && !$this->urlValidator->isValid($link->getSampleUrl())
102  ) {
103  throw new InputException(__('Sample URL must have valid format.'));
104  }
105  if ($link->getSampleType() == 'file'
106  && (!$link->getSampleFileContent()
107  || !$this->fileContentValidator->isValid($link->getSampleFileContent()))
108  ) {
109  throw new InputException(__('Provided file content must be valid base64 encoded data.'));
110  }
111  }
112 }
__()
Definition: __.php:13