ContentValidator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Link;
  7. use Magento\Downloadable\Api\Data\LinkInterface;
  8. use Magento\Downloadable\Model\File\ContentValidator as FileContentValidator;
  9. use Magento\Framework\Exception\InputException;
  10. use Magento\Framework\Url\Validator as UrlValidator;
  11. class ContentValidator
  12. {
  13. /**
  14. * @var FileContentValidator
  15. */
  16. protected $fileContentValidator;
  17. /**
  18. * @var UrlValidator
  19. */
  20. protected $urlValidator;
  21. /**
  22. * @param FileContentValidator $fileContentValidator
  23. * @param UrlValidator $urlValidator
  24. */
  25. public function __construct(
  26. FileContentValidator $fileContentValidator,
  27. UrlValidator $urlValidator
  28. ) {
  29. $this->fileContentValidator = $fileContentValidator;
  30. $this->urlValidator = $urlValidator;
  31. }
  32. /**
  33. * Check if link content is valid
  34. *
  35. * @param LinkInterface $link
  36. * @param bool $validateLinkContent
  37. * @param bool $validateSampleContent
  38. * @return bool
  39. * @throws InputException
  40. */
  41. public function isValid(LinkInterface $link, $validateLinkContent = true, $validateSampleContent = true)
  42. {
  43. if (!is_numeric($link->getPrice()) || $link->getPrice() < 0) {
  44. throw new InputException(__('Link price must have numeric positive value.'));
  45. }
  46. if (filter_var($link->getNumberOfDownloads(), FILTER_VALIDATE_INT) === false
  47. || $link->getNumberOfDownloads() < 0) {
  48. throw new InputException(__('Number of downloads must be a positive integer.'));
  49. }
  50. if (filter_var($link->getSortOrder(), FILTER_VALIDATE_INT) === false
  51. || $link->getSortOrder() < 0) {
  52. throw new InputException(__('Sort order must be a positive integer.'));
  53. }
  54. if ($validateLinkContent) {
  55. $this->validateLinkResource($link);
  56. }
  57. if ($validateSampleContent) {
  58. $this->validateSampleResource($link);
  59. }
  60. return true;
  61. }
  62. /**
  63. * Validate link resource (file or URL)
  64. *
  65. * @param LinkInterface $link
  66. * @throws InputException
  67. * @return void
  68. */
  69. protected function validateLinkResource(LinkInterface $link)
  70. {
  71. if ($link->getLinkType() == 'url'
  72. && !$this->urlValidator->isValid($link->getLinkUrl())
  73. ) {
  74. throw new InputException(__('Link URL must have valid format.'));
  75. }
  76. if ($link->getLinkType() == 'file'
  77. && (!$link->getLinkFileContent()
  78. || !$this->fileContentValidator->isValid($link->getLinkFileContent()))
  79. ) {
  80. throw new InputException(__('Provided file content must be valid base64 encoded data.'));
  81. }
  82. }
  83. /**
  84. * Validate sample resource (file or URL)
  85. *
  86. * @param LinkInterface $link
  87. * @throws InputException
  88. * @return void
  89. */
  90. protected function validateSampleResource(LinkInterface $link)
  91. {
  92. if ($link->getSampleType() == 'url'
  93. && !$this->urlValidator->isValid($link->getSampleUrl())
  94. ) {
  95. throw new InputException(__('Sample URL must have valid format.'));
  96. }
  97. if ($link->getSampleType() == 'file'
  98. && (!$link->getSampleFileContent()
  99. || !$this->fileContentValidator->isValid($link->getSampleFileContent()))
  100. ) {
  101. throw new InputException(__('Provided file content must be valid base64 encoded data.'));
  102. }
  103. }
  104. }