ArrayNodeConfig.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Dom;
  7. /**
  8. * Configuration of nodes that represent numeric or associative arrays
  9. */
  10. class ArrayNodeConfig
  11. {
  12. /**
  13. * @var NodePathMatcher
  14. */
  15. private $nodePathMatcher;
  16. /**
  17. * Format: array('/associative/array/path' => '<array_key_attribute>', ...)
  18. *
  19. * @var array
  20. */
  21. private $assocArrays = [];
  22. /**
  23. * Format: array('/numeric/array/path', ...)
  24. *
  25. * @var array
  26. */
  27. private $numericArrays = [];
  28. /**
  29. * @param NodePathMatcher $nodePathMatcher
  30. * @param array $assocArrayAttributes
  31. * @param array $numericArrays
  32. */
  33. public function __construct(
  34. NodePathMatcher $nodePathMatcher,
  35. array $assocArrayAttributes,
  36. array $numericArrays = []
  37. ) {
  38. $this->nodePathMatcher = $nodePathMatcher;
  39. $this->assocArrays = $assocArrayAttributes;
  40. $this->numericArrays = $numericArrays;
  41. }
  42. /**
  43. * Whether a node is a numeric array or not
  44. *
  45. * @param string $nodeXpath
  46. * @return bool
  47. */
  48. public function isNumericArray($nodeXpath)
  49. {
  50. foreach ($this->numericArrays as $pathPattern) {
  51. if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. /**
  58. * Retrieve name of array key attribute, if a node is an associative array
  59. *
  60. * @param string $nodeXpath
  61. * @return string|null
  62. */
  63. public function getAssocArrayKeyAttribute($nodeXpath)
  64. {
  65. foreach ($this->assocArrays as $pathPattern => $keyAttribute) {
  66. if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
  67. return $keyAttribute;
  68. }
  69. }
  70. return null;
  71. }
  72. }