You are here

class HttpHeader in RESTful 7.2

Hierarchy

Expanded class hierarchy of HttpHeader

6 files declare their use of HttpHeader
CacheDecoratedResource.php in src/Plugin/resource/Decorators/CacheDecoratedResource.php
Contains \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource
DataProvider.php in src/Plugin/resource/DataProvider/DataProvider.php
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProvider.
FormatterManager.php in src/Formatter/FormatterManager.php
Contains \Drupal\restful\Formatter\FormatterManager
RateLimitManager.php in src/RateLimit/RateLimitManager.php
Contains \Drupal\restful\RateLimit\RateLimitManager
Resource.php in src/Plugin/resource/Resource.php
Contains \Drupal\restful\Plugin\resource\Resource.

... See full list

File

src/Http/HttpHeader.php, line 10
Contains \Drupal\restful\Http\HttpHeader

Namespace

Drupal\restful\Http
View source
class HttpHeader implements HttpHeaderInterface {

  /**
   * Header ID.
   *
   * @var string
   */
  protected $id;

  /**
   * Header name.
   *
   * @var string
   */
  protected $name;

  /**
   * Header values.
   *
   * @var array
   */
  protected $values = array();

  /**
   * Header extras.
   *
   * @var string
   */
  protected $extras;

  /**
   * Constructor.
   */
  public function __construct($name, array $values, $extras) {
    $this->name = $name;
    $this->id = static::generateId($name);
    $this->values = $values;
    $this->extras = $extras;
  }

  /**
   * {@inheritdoc}
   */
  public static function create($key, $value) {
    list($extras, $values) = self::parseHeaderValue($value);
    return new static($key, $values, $extras);
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this->values;
  }

  /**
   * {@inheritdoc}
   */
  public function getValueString() {
    $parts = array();
    $parts[] = implode(', ', $this->values);
    $parts[] = $this->extras;
    return implode('; ', array_filter($parts));
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Returns the string version of the header.
   *
   * @return string
   */
  public function __toString() {
    return $this->name . ': ' . $this
      ->getValueString();
  }

  /**
   * {@inheritdoc}
   */
  public function set($values) {
    $this->values = $values;
  }

  /**
   * {@inheritdoc}
   */
  public function append($value) {

    // Ignore the extras.
    list(, $values) = static::parseHeaderValue($value);
    foreach ($values as $value) {
      $this->values[] = $value;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public static function generateId($name) {
    return strtolower($name);
  }

  /**
   * Parses the values and extras from a header value string.
   *
   * @param string $value
   *
   * @return array
   *   The $extras and $values.
   */
  protected static function parseHeaderValue($value) {
    $extras = NULL;
    $parts = explode(';', $value);
    if (count($parts) > 1) {

      // Only consider the last element.
      $extras = array_pop($parts);
      $extras = trim($extras);

      // In case there were more than one ';' then put everything back.
      $value = implode(';', $parts);
    }
    $values = array_map('trim', explode(',', $value));
    return array(
      $extras,
      $values,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpHeader::$extras protected property Header extras.
HttpHeader::$id protected property Header ID.
HttpHeader::$name protected property Header name.
HttpHeader::$values protected property Header values.
HttpHeader::append public function Appends a value into a header. Overrides HttpHeaderInterface::append
HttpHeader::create public static function Creates a header object from the key and value strings. Overrides HttpHeaderInterface::create
HttpHeader::generateId public static function Generates the header ID based on the header name. Overrides HttpHeaderInterface::generateId
HttpHeader::get public function Gets the values of the header. Overrides HttpHeaderInterface::get
HttpHeader::getId public function Gets the header id. Overrides HttpHeaderInterface::getId
HttpHeader::getName public function Gets the header name. Overrides HttpHeaderInterface::getName
HttpHeader::getValueString public function Gets the contents of the header. Overrides HttpHeaderInterface::getValueString
HttpHeader::parseHeaderValue protected static function Parses the values and extras from a header value string.
HttpHeader::set public function Sets the values. Overrides HttpHeaderInterface::set
HttpHeader::__construct public function Constructor.
HttpHeader::__toString public function Returns the string version of the header.