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