Validator.php 3.0 KB

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