AdditionalClasses.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config\Converter;
  7. use Magento\Framework\ObjectManager\Config\Reader\Dom;
  8. use Magento\Ui\Config\Converter;
  9. use Magento\Ui\Config\ConverterInterface;
  10. use Magento\Ui\Config\ConverterUtils;
  11. /**
  12. * Converter for 'settings/additionalClasses' configuration settings
  13. */
  14. class AdditionalClasses implements ConverterInterface
  15. {
  16. /**
  17. * @var ConverterUtils
  18. */
  19. private $converterUtils;
  20. /**
  21. * @param ConverterUtils $converterUtils
  22. */
  23. public function __construct(ConverterUtils $converterUtils)
  24. {
  25. $this->converterUtils = $converterUtils;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function convert(\DOMNode $node, array $data = [])
  31. {
  32. if ($node->nodeType !== XML_ELEMENT_NODE) {
  33. return [];
  34. }
  35. return $this->toArray($node);
  36. }
  37. /**
  38. * Convert nodes and child nodes to array
  39. *
  40. * @param \DOMNode $node
  41. * @return array
  42. */
  43. private function toArray(\DOMNode $node)
  44. {
  45. $result = [
  46. 'name' => $this->converterUtils->getComponentName($node),
  47. Dom::TYPE_ATTRIBUTE => 'array'
  48. ];
  49. if ($this->hasChildNodes($node)) {
  50. /** @var \DOMNode $childNode */
  51. foreach ($node->childNodes as $childNode) {
  52. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  53. $result['item'][$this->converterUtils->getComponentName($childNode)] = [
  54. 'name' => $childNode->getAttribute('name'),
  55. Dom::TYPE_ATTRIBUTE => 'boolean',
  56. 'value' => trim($childNode->nodeValue)
  57. ];
  58. }
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Check is DOMNode has child DOMElements
  65. *
  66. * @param \DOMNode $node
  67. * @return bool
  68. */
  69. private function hasChildNodes(\DOMNode $node)
  70. {
  71. if (!$node->hasChildNodes()) {
  72. return false;
  73. }
  74. foreach ($node->childNodes as $child) {
  75. if ($child->nodeType == XML_ELEMENT_NODE) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. }