Validator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Scope;
  7. use InvalidArgumentException;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\ScopeResolverPool;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Phrase;
  13. /**
  14. * Class Validator validates scope and scope code.
  15. */
  16. class Validator implements ValidatorInterface
  17. {
  18. /**
  19. * @var ScopeResolverPool
  20. */
  21. private $scopeResolverPool;
  22. /**
  23. * @param ScopeResolverPool $scopeResolverPool
  24. */
  25. public function __construct(ScopeResolverPool $scopeResolverPool)
  26. {
  27. $this->scopeResolverPool = $scopeResolverPool;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function isValid($scope, $scopeCode = null)
  33. {
  34. if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && empty($scopeCode)) {
  35. return true;
  36. }
  37. if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && !empty($scopeCode)) {
  38. throw new LocalizedException(new Phrase(
  39. 'The "%1" scope can\'t include a scope code. Try again without entering a scope code.',
  40. [ScopeConfigInterface::SCOPE_TYPE_DEFAULT]
  41. ));
  42. }
  43. if (empty($scope)) {
  44. throw new LocalizedException(new Phrase('A scope is missing. Enter a scope and try again.'));
  45. }
  46. $this->validateScopeCode($scopeCode);
  47. try {
  48. $scopeResolver = $this->scopeResolverPool->get($scope);
  49. $scopeResolver->getScope($scopeCode)->getId();
  50. } catch (InvalidArgumentException $e) {
  51. throw new LocalizedException(
  52. new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scope])
  53. );
  54. } catch (NoSuchEntityException $e) {
  55. throw new LocalizedException(
  56. new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scopeCode])
  57. );
  58. }
  59. return true;
  60. }
  61. /**
  62. * Validate scope code
  63. * Throw exception if not valid.
  64. *
  65. * @param string $scopeCode
  66. * @return void
  67. * @throws LocalizedException if scope code is empty or has a wrong format
  68. */
  69. private function validateScopeCode($scopeCode)
  70. {
  71. if (empty($scopeCode)) {
  72. throw new LocalizedException(new Phrase('A scope code is missing. Enter a code and try again.'));
  73. }
  74. if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $scopeCode)) {
  75. throw new LocalizedException(new Phrase(
  76. 'The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores (_). '
  77. . 'Also, the first character must be a letter.'
  78. ));
  79. }
  80. }
  81. }