CookieMetadata.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\Cookie;
  7. /**
  8. * Class CookieMetadata
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class CookieMetadata
  13. {
  14. /**#@+
  15. * Constant for metadata value key.
  16. */
  17. const KEY_DOMAIN = 'domain';
  18. const KEY_PATH = 'path';
  19. const KEY_SECURE = 'secure';
  20. const KEY_HTTP_ONLY = 'http_only';
  21. const KEY_DURATION = 'duration';
  22. /**#@-*/
  23. /**#@-*/
  24. private $metadata;
  25. /**
  26. * @param array $metadata
  27. */
  28. public function __construct($metadata = [])
  29. {
  30. if (!is_array($metadata)) {
  31. $metadata = [];
  32. }
  33. $this->metadata = $metadata;
  34. }
  35. /**
  36. * Returns an array representation of this metadata.
  37. *
  38. * If a value has not yet been set then the key will not show up in the array.
  39. *
  40. * @return array
  41. */
  42. public function __toArray()
  43. {
  44. return $this->metadata;
  45. }
  46. /**
  47. * Set the domain for the cookie
  48. *
  49. * @param string $domain
  50. * @return $this
  51. */
  52. public function setDomain($domain)
  53. {
  54. return $this->set(self::KEY_DOMAIN, $domain);
  55. }
  56. /**
  57. * Get the domain for the cookie
  58. *
  59. * @return string|null
  60. */
  61. public function getDomain()
  62. {
  63. return $this->get(self::KEY_DOMAIN);
  64. }
  65. /**
  66. * Set path of the cookie
  67. *
  68. * @param string $path
  69. * @return $this
  70. */
  71. public function setPath($path)
  72. {
  73. return $this->set(self::KEY_PATH, $path);
  74. }
  75. /**
  76. * Get the path of the cookie
  77. *
  78. * @return string|null
  79. */
  80. public function getPath()
  81. {
  82. return $this->get(self::KEY_PATH);
  83. }
  84. /**
  85. * Get a value from the metadata storage.
  86. *
  87. * @param string $name
  88. * @return int|float|string|bool|null
  89. */
  90. protected function get($name)
  91. {
  92. if (isset($this->metadata[$name])) {
  93. return $this->metadata[$name];
  94. }
  95. return null;
  96. }
  97. /**
  98. * Set a value to the metadata storage.
  99. *
  100. * @param string $name
  101. * @param int|float|string|bool|null $value
  102. * @return $this
  103. */
  104. protected function set($name, $value)
  105. {
  106. $this->metadata[$name] = $value;
  107. return $this;
  108. }
  109. /**
  110. * Get HTTP Only flag
  111. *
  112. * @return bool|null
  113. */
  114. public function getHttpOnly()
  115. {
  116. return $this->get(self::KEY_HTTP_ONLY);
  117. }
  118. /**
  119. * Get whether the cookie is only available under HTTPS
  120. *
  121. * @return bool|null
  122. */
  123. public function getSecure()
  124. {
  125. return $this->get(self::KEY_SECURE);
  126. }
  127. }