ValueChecker.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Design\Config;
  7. use Magento\Framework\App\Config as AppConfig;
  8. use Magento\Framework\App\ScopeFallbackResolverInterface;
  9. class ValueChecker
  10. {
  11. /**
  12. * @var ScopeFallbackResolverInterface
  13. */
  14. protected $fallbackResolver;
  15. /**
  16. * @var AppConfig
  17. */
  18. protected $appConfig;
  19. /**
  20. * @var ValueProcessor
  21. */
  22. protected $valueProcessor;
  23. /**
  24. * @param ScopeFallbackResolverInterface $fallbackResolver
  25. * @param AppConfig $appConfig
  26. * @param \Magento\Theme\Model\Design\Config\ValueProcessor $valueProcessor
  27. */
  28. public function __construct(
  29. ScopeFallbackResolverInterface $fallbackResolver,
  30. AppConfig $appConfig,
  31. ValueProcessor $valueProcessor
  32. ) {
  33. $this->fallbackResolver = $fallbackResolver;
  34. $this->appConfig = $appConfig;
  35. $this->valueProcessor = $valueProcessor;
  36. }
  37. /**
  38. * Check whether value differs from parent scope's one
  39. *
  40. * @param string $value
  41. * @param string $scope
  42. * @param int $scopeId
  43. * @param array $fieldConfig
  44. * @return bool
  45. */
  46. public function isDifferentFromDefault($value, $scope, $scopeId, array $fieldConfig)
  47. {
  48. list($scope, $scopeId) = $this->fallbackResolver->getFallbackScope($scope, $scopeId);
  49. if ($scope) {
  50. return !$this->isEqual(
  51. $this->valueProcessor->process(
  52. $value,
  53. $scope,
  54. $scopeId,
  55. $fieldConfig
  56. ),
  57. $this->valueProcessor->process(
  58. $this->appConfig->getValue($fieldConfig['path'], $scope, $scopeId),
  59. $scope,
  60. $scopeId,
  61. $fieldConfig
  62. )
  63. );
  64. }
  65. return true;
  66. }
  67. /**
  68. * Compare two variables
  69. *
  70. * @param mixed $value
  71. * @param mixed $defaultValue
  72. * @return bool
  73. */
  74. protected function isEqual($value, $defaultValue)
  75. {
  76. switch (gettype($value)) {
  77. case 'array':
  78. return $this->isEqualArrays($value, $defaultValue);
  79. default:
  80. return $value === $defaultValue;
  81. }
  82. }
  83. /**
  84. * Compare two multidimensional arrays
  85. *
  86. * @param array $value
  87. * @param array $defaultValue
  88. * @return bool
  89. */
  90. protected function isEqualArrays(array $value, array $defaultValue)
  91. {
  92. $result = true;
  93. if (count($value) !== count($defaultValue)) {
  94. return false;
  95. }
  96. foreach ($value as $key => $elem) {
  97. if (is_array($elem)) {
  98. if (isset($defaultValue[$key])) {
  99. $result = $result && $this->isEqualArrays($elem, $defaultValue[$key]);
  100. } else {
  101. return false;
  102. }
  103. } else {
  104. if (isset($defaultValue[$key])) {
  105. $result = $result && ($defaultValue[$key] == $elem);
  106. } else {
  107. return false;
  108. }
  109. }
  110. }
  111. return $result;
  112. }
  113. }