PathValidator.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config;
  7. use Magento\Framework\Exception\ValidatorException;
  8. /**
  9. * Validates the config path by config structure schema.
  10. * @api
  11. * @since 101.0.0
  12. */
  13. class PathValidator
  14. {
  15. /**
  16. * The config structure.
  17. *
  18. * @var Structure
  19. */
  20. private $structure;
  21. /**
  22. * @param Structure $structure The config structure
  23. */
  24. public function __construct(Structure $structure)
  25. {
  26. $this->structure = $structure;
  27. }
  28. /**
  29. * Checks whether the config path present in configuration structure.
  30. *
  31. * @param string $path The config path
  32. * @return true The result of validation
  33. * @throws ValidatorException If provided path is not valid
  34. * @since 101.0.0
  35. */
  36. public function validate($path)
  37. {
  38. $allPaths = $this->structure->getFieldPaths();
  39. if (!array_key_exists($path, $allPaths)) {
  40. throw new ValidatorException(__('The "%1" path doesn\'t exist. Verify and try again.', $path));
  41. }
  42. return true;
  43. }
  44. }