Context.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Http;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Context data for requests
  11. */
  12. class Context
  13. {
  14. /**
  15. * Currency cache context
  16. */
  17. const CONTEXT_CURRENCY = 'current_currency';
  18. /**
  19. * Data storage
  20. *
  21. * @var array
  22. */
  23. protected $data = [];
  24. /**
  25. * @var array
  26. */
  27. protected $default = [];
  28. /**
  29. * @var Json
  30. */
  31. private $serializer;
  32. /**
  33. * @param array $data
  34. * @param array $default
  35. * @param Json|null $serializer
  36. */
  37. public function __construct(array $data = [], array $default = [], Json $serializer = null)
  38. {
  39. $this->data = $data;
  40. $this->default = $default;
  41. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  42. }
  43. /**
  44. * Data setter
  45. *
  46. * @param string $name
  47. * @param mixed $value
  48. * @param mixed $default
  49. * @return \Magento\Framework\App\Http\Context
  50. */
  51. public function setValue($name, $value, $default)
  52. {
  53. if ($default !== null) {
  54. $this->default[$name] = $default;
  55. }
  56. $this->data[$name] = $value;
  57. return $this;
  58. }
  59. /**
  60. * Unset data from vary array
  61. *
  62. * @param string $name
  63. * @return null
  64. */
  65. public function unsValue($name)
  66. {
  67. unset($this->data[$name]);
  68. return $this;
  69. }
  70. /**
  71. * Data getter
  72. *
  73. * @param string $name
  74. * @return mixed|null
  75. */
  76. public function getValue($name)
  77. {
  78. return $this->data[$name] ?? ($this->default[$name] ?? null);
  79. }
  80. /**
  81. * Return all data
  82. *
  83. * @return array
  84. */
  85. public function getData()
  86. {
  87. $data = [];
  88. foreach ($this->data as $name => $value) {
  89. if ($value && $value != $this->default[$name]) {
  90. $data[$name] = $value;
  91. }
  92. }
  93. return $data;
  94. }
  95. /**
  96. * Return vary string to be used as a part of page cache identifier
  97. *
  98. * @return string|null
  99. */
  100. public function getVaryString()
  101. {
  102. $data = $this->getData();
  103. if (!empty($data)) {
  104. ksort($data);
  105. return sha1($this->serializer->serialize($data));
  106. }
  107. return null;
  108. }
  109. /**
  110. * Get data and default data in "key-value" format
  111. *
  112. * @return array
  113. */
  114. public function toArray()
  115. {
  116. return [
  117. 'data' => $this->data,
  118. 'default' => $this->default
  119. ];
  120. }
  121. }