123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Stdlib\Cookie;
- /**
- * Class CookieMetadata
- * @api
- * @since 100.0.2
- */
- class CookieMetadata
- {
- /**#@+
- * Constant for metadata value key.
- */
- const KEY_DOMAIN = 'domain';
- const KEY_PATH = 'path';
- const KEY_SECURE = 'secure';
- const KEY_HTTP_ONLY = 'http_only';
- const KEY_DURATION = 'duration';
- /**#@-*/
- /**#@-*/
- private $metadata;
- /**
- * @param array $metadata
- */
- public function __construct($metadata = [])
- {
- if (!is_array($metadata)) {
- $metadata = [];
- }
- $this->metadata = $metadata;
- }
- /**
- * Returns an array representation of this metadata.
- *
- * If a value has not yet been set then the key will not show up in the array.
- *
- * @return array
- */
- public function __toArray()
- {
- return $this->metadata;
- }
- /**
- * Set the domain for the cookie
- *
- * @param string $domain
- * @return $this
- */
- public function setDomain($domain)
- {
- return $this->set(self::KEY_DOMAIN, $domain);
- }
- /**
- * Get the domain for the cookie
- *
- * @return string|null
- */
- public function getDomain()
- {
- return $this->get(self::KEY_DOMAIN);
- }
- /**
- * Set path of the cookie
- *
- * @param string $path
- * @return $this
- */
- public function setPath($path)
- {
- return $this->set(self::KEY_PATH, $path);
- }
- /**
- * Get the path of the cookie
- *
- * @return string|null
- */
- public function getPath()
- {
- return $this->get(self::KEY_PATH);
- }
- /**
- * Get a value from the metadata storage.
- *
- * @param string $name
- * @return int|float|string|bool|null
- */
- protected function get($name)
- {
- if (isset($this->metadata[$name])) {
- return $this->metadata[$name];
- }
- return null;
- }
- /**
- * Set a value to the metadata storage.
- *
- * @param string $name
- * @param int|float|string|bool|null $value
- * @return $this
- */
- protected function set($name, $value)
- {
- $this->metadata[$name] = $value;
- return $this;
- }
- /**
- * Get HTTP Only flag
- *
- * @return bool|null
- */
- public function getHttpOnly()
- {
- return $this->get(self::KEY_HTTP_ONLY);
- }
- /**
- * Get whether the cookie is only available under HTTPS
- *
- * @return bool|null
- */
- public function getSecure()
- {
- return $this->get(self::KEY_SECURE);
- }
- }
|