You are here

class HttpHeaderBag in RESTful 7.2

Hierarchy

Expanded class hierarchy of HttpHeaderBag

2 files declare their use of HttpHeaderBag
ResourceFieldEntityReference.php in src/Plugin/resource/Field/ResourceFieldEntityReference.php
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldEntityReference.
RestfulHalJsonTestCase.test in tests/RestfulHalJsonTestCase.test
Contains RestfulHalJsonTestCase

File

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

Namespace

Drupal\restful\Http
View source
class HttpHeaderBag implements HttpHeaderBagInterface, \Iterator {

  /**
   * The header objects keyed by ID.
   *
   * @var array
   */
  protected $values = array();

  /**
   * Constructor
   *
   * @param array $headers
   *   Array of key value pairs.
   */
  public function __construct($headers = array()) {
    foreach ($headers as $key => $value) {
      $header = HttpHeader::create($key, $value);
      $this->values[$header
        ->getId()] = $header;
    }
  }

  /**
   * Returns the header bag as a string.
   *
   * @return string
   *   The string representation.
   */
  public function __toString() {
    $headers = array();
    foreach ($this->values as $key => $header) {

      /* @var HttpHeader $header */
      $headers[] = $header
        ->__toString();
    }
    return implode("\r\n", $headers);
  }

  /**
   * {@inheritdoc}
   */
  public function get($key) {

    // Assume that $key is an ID.
    if (array_key_exists($key, $this->values)) {
      return $this->values[$key];
    }

    // Test if key was a header name.
    $key = HttpHeader::generateId($key);
    if (array_key_exists($key, $this->values)) {
      return $this->values[$key];
    }

    // Return a NULL object on which you can still call the HttpHeaderInterface
    // methods.
    return HttpHeaderNull::create(NULL, NULL);
  }

  /**
   * {@inheritdoc}
   */
  public function has($key) {
    return !empty($this->values[$key]);
  }

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

  /**
   * {@inheritdoc}
   */
  public function add(HttpHeaderInterface $header) {
    $this->values[$header
      ->getId()] = $header;
  }

  /**
   * {@inheritdoc}
   */
  public function append(HttpHeaderInterface $header) {
    if (!$this
      ->has($header
      ->getId())) {
      $this
        ->add($header);
      return;
    }
    $existing_header = $this
      ->get($header
      ->getId());

    // Append all the values in the passed in header to the existing header
    // values.
    foreach ($header
      ->get() as $value) {
      $existing_header
        ->append($value);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function remove($key) {

    // Assume that $key is an ID.
    if (!array_key_exists($key, $this->values)) {

      // Test if key was a header name.
      $key = HttpHeader::generateId($key);
      if (!array_key_exists($key, $this->values)) {
        return;
      }
    }
    unset($this->values[$key]);
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function valid() {
    $key = key($this->values);
    return $key !== NULL && $key !== FALSE;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
HttpHeaderBag::$values protected property The header objects keyed by ID.
HttpHeaderBag::add public function Add a header to the bag. Overrides HttpHeaderBagInterface::add
HttpHeaderBag::append public function Appends the values of the passed in header to if the header already exists. Overrides HttpHeaderBagInterface::append
HttpHeaderBag::current public function
HttpHeaderBag::get public function Get the the header object for a header name or ID. Overrides HttpHeaderBagInterface::get
HttpHeaderBag::getValues public function Returns all the headers set on the bag. Overrides HttpHeaderBagInterface::getValues
HttpHeaderBag::has public function Checks the existence of a header in the bag. Overrides HttpHeaderBagInterface::has
HttpHeaderBag::key public function
HttpHeaderBag::next public function
HttpHeaderBag::remove public function Removes a header from the bag. Overrides HttpHeaderBagInterface::remove
HttpHeaderBag::rewind public function
HttpHeaderBag::valid public function
HttpHeaderBag::__construct public function Constructor
HttpHeaderBag::__toString public function Returns the header bag as a string.