Field.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Structure\Element\Dependency;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Field
  12. {
  13. /**
  14. * Values for dependence
  15. *
  16. * @var string[]
  17. */
  18. protected $_values;
  19. /**
  20. * Id of the dependent field
  21. *
  22. * @var string
  23. */
  24. protected $_id;
  25. /**
  26. * Whether dependence is for negative comparison
  27. *
  28. * @var bool
  29. */
  30. protected $_isNegative = false;
  31. /**
  32. * @param array $fieldData
  33. * @param string $fieldPrefix
  34. */
  35. public function __construct(array $fieldData = [], $fieldPrefix = "")
  36. {
  37. if (isset($fieldData['separator'])) {
  38. $this->_values = explode($fieldData['separator'], $fieldData['value']);
  39. } else {
  40. $this->_values = [$fieldData['value']];
  41. }
  42. $fieldId = $fieldPrefix . (isset(
  43. $fieldData['dependPath']
  44. ) && is_array(
  45. $fieldData['dependPath']
  46. ) ? array_pop(
  47. $fieldData['dependPath']
  48. ) : '');
  49. $fieldData['dependPath'][] = $fieldId;
  50. $this->_id = implode('_', $fieldData['dependPath']);
  51. $this->_isNegative = isset($fieldData['negative']) && $fieldData['negative'];
  52. }
  53. /**
  54. * Check whether the value satisfy dependency
  55. *
  56. * @param string $value
  57. * @return bool
  58. */
  59. public function isValueSatisfy($value)
  60. {
  61. return in_array($value, $this->_values) xor $this->_isNegative;
  62. }
  63. /**
  64. * Get id of the dependent field
  65. *
  66. * @return string
  67. */
  68. public function getId()
  69. {
  70. return $this->_id;
  71. }
  72. /**
  73. * Get values for dependence
  74. *
  75. * @return string[]
  76. */
  77. public function getValues()
  78. {
  79. return $this->_values;
  80. }
  81. /**
  82. * Get negative indication of dependency
  83. *
  84. * @return bool
  85. */
  86. public function isNegative()
  87. {
  88. return $this->_isNegative;
  89. }
  90. }