|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / vendor / magento / framework / App / Scope / |
Filename | /home/dev2.destoffenstraat.com/vendor/magento/framework/App/Scope/Validator.php |
Size | 2.84 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 07-Jan-2021 21:08 |
Last accessed | 23-Aug-2025 03:56 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Scope;
use InvalidArgumentException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeResolverPool;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
/**
* Class Validator validates scope and scope code.
*/
class Validator implements ValidatorInterface
{
/**
* @var ScopeResolverPool
*/
private $scopeResolverPool;
/**
* @param ScopeResolverPool $scopeResolverPool
*/
public function __construct(ScopeResolverPool $scopeResolverPool)
{
$this->scopeResolverPool = $scopeResolverPool;
}
/**
* {@inheritdoc}
*/
public function isValid($scope, $scopeCode = null)
{
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && empty($scopeCode)) {
return true;
}
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && !empty($scopeCode)) {
throw new LocalizedException(new Phrase(
'The "%1" scope can\'t include a scope code. Try again without entering a scope code.',
[ScopeConfigInterface::SCOPE_TYPE_DEFAULT]
));
}
if (empty($scope)) {
throw new LocalizedException(new Phrase('A scope is missing. Enter a scope and try again.'));
}
$this->validateScopeCode($scopeCode);
try {
$scopeResolver = $this->scopeResolverPool->get($scope);
$scopeResolver->getScope($scopeCode)->getId();
} catch (InvalidArgumentException $e) {
throw new LocalizedException(
new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scope])
);
} catch (NoSuchEntityException $e) {
throw new LocalizedException(
new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scopeCode])
);
}
return true;
}
/**
* Validate scope code
* Throw exception if not valid.
*
* @param string $scopeCode
* @return void
* @throws LocalizedException if scope code is empty or has a wrong format
*/
private function validateScopeCode($scopeCode)
{
if (empty($scopeCode)) {
throw new LocalizedException(new Phrase('A scope code is missing. Enter a code and try again.'));
}
if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $scopeCode)) {
throw new LocalizedException(new Phrase(
'The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores (_). '
. 'Also, the first character must be a letter.'
));
}
}
}
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Scope;
use InvalidArgumentException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeResolverPool;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
/**
* Class Validator validates scope and scope code.
*/
class Validator implements ValidatorInterface
{
/**
* @var ScopeResolverPool
*/
private $scopeResolverPool;
/**
* @param ScopeResolverPool $scopeResolverPool
*/
public function __construct(ScopeResolverPool $scopeResolverPool)
{
$this->scopeResolverPool = $scopeResolverPool;
}
/**
* {@inheritdoc}
*/
public function isValid($scope, $scopeCode = null)
{
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && empty($scopeCode)) {
return true;
}
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && !empty($scopeCode)) {
throw new LocalizedException(new Phrase(
'The "%1" scope can\'t include a scope code. Try again without entering a scope code.',
[ScopeConfigInterface::SCOPE_TYPE_DEFAULT]
));
}
if (empty($scope)) {
throw new LocalizedException(new Phrase('A scope is missing. Enter a scope and try again.'));
}
$this->validateScopeCode($scopeCode);
try {
$scopeResolver = $this->scopeResolverPool->get($scope);
$scopeResolver->getScope($scopeCode)->getId();
} catch (InvalidArgumentException $e) {
throw new LocalizedException(
new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scope])
);
} catch (NoSuchEntityException $e) {
throw new LocalizedException(
new Phrase('The "%1" value doesn\'t exist. Enter another value and try again.', [$scopeCode])
);
}
return true;
}
/**
* Validate scope code
* Throw exception if not valid.
*
* @param string $scopeCode
* @return void
* @throws LocalizedException if scope code is empty or has a wrong format
*/
private function validateScopeCode($scopeCode)
{
if (empty($scopeCode)) {
throw new LocalizedException(new Phrase('A scope code is missing. Enter a code and try again.'));
}
if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $scopeCode)) {
throw new LocalizedException(new Phrase(
'The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores (_). '
. 'Also, the first character must be a letter.'
));
}
}
}