class Header

Represents a header and all of the values stored by that header

Hierarchy

  • class \Guzzle\Http\Message\Header implements \Guzzle\Http\Message\IteratorAggregate, \Guzzle\Http\Message\Countable, ToArrayInterface

Expanded class hierarchy of Header

5 string references to 'Header'
bartik.info.yml in drupal/core/themes/bartik/bartik.info.yml
drupal/core/themes/bartik/bartik.info.yml
block_test_theme.info.yml in drupal/core/modules/block/tests/themes/block_test_theme/block_test_theme.info.yml
drupal/core/modules/block/tests/themes/block_test_theme/block_test_theme.info.yml
template_preprocess_views_ui_view_preview_section in drupal/core/modules/views_ui/views_ui.theme.inc
Theme preprocess for theme_views_ui_view_preview_section().
ViewExecutable::viewsHandlerTypes in drupal/core/modules/views/lib/Drupal/views/ViewExecutable.php
Provide a list of views handler types used in a view, with some information about them.
_system_rebuild_theme_data in drupal/core/modules/system/system.module
Helper function to scan and collect theme .info.yml data and their engines.

File

drupal/core/vendor/guzzle/http/Guzzle/Http/Message/Header.php, line 10

Namespace

Guzzle\Http\Message
View source
class Header implements ToArrayInterface, \IteratorAggregate, \Countable {
  protected $values = array();
  protected $header;
  protected $glue;
  protected $stringCache;
  protected $arrayCache;

  /**
   * Construct a new header object
   *
   * @param string $header Name of the header
   * @param array  $values Values of the header
   * @param string $glue   Glue used to combine multiple values into a string
   */
  public function __construct($header, $values = array(), $glue = ', ') {
    $this->header = $header;
    $this->glue = $glue;
    if (null !== $values) {
      foreach ((array) $values as $key => $value) {
        if (is_numeric($key)) {
          $key = $header;
        }
        if ($value === null) {
          $this
            ->add($value, $key);
        }
        else {
          foreach ((array) $value as $v) {
            $this
              ->add($v, $key);
          }
        }
      }
    }
  }

  /**
   * Convert the header to a string
   *
   * @return string
   */
  public function __toString() {
    if (!$this->stringCache) {
      $this->stringCache = implode($this->glue, $this
        ->toArray());
    }
    return $this->stringCache;
  }

  /**
   * Add a value to the list of header values
   *
   * @param string $value  Value to add
   * @param string $header The exact header casing to add with. Defaults to the name of the header.
   *
   * @return Header
   */
  public function add($value, $header = null) {
    if (!$header) {
      $header = $this
        ->getName();
    }
    if (!array_key_exists($header, $this->values)) {
      $this->values[$header] = array(
        $value,
      );
    }
    else {
      $this->values[$header][] = $value;
    }
    $this
      ->clearCache();
    return $this;
  }

  /**
   * Get the name of the header
   *
   * @return string
   */
  public function getName() {
    return $this->header;
  }

  /**
   * Change the glue used to implode the values
   *
   * @param string $glue Glue used to implode multiple values
   *
   * @return Header
   */
  public function setGlue($glue) {
    $this->glue = $glue;
    $this->stringCache = null;
    return $this;
  }

  /**
   * Get the glue used to implode multiple values into a string
   *
   * @return string
   */
  public function getGlue() {
    return $this->glue;
  }

  /**
   * Normalize the header into a single standard header with an array of values
   *
   * @return Header
   */
  public function normalize() {
    $this
      ->clearCache();
    $this->values = array(
      $this
        ->getName() => $this
        ->toArray(),
    );
    return $this;
  }

  /**
   * Check if a particular case variation is present in the header
   * Example: A header exists on a message for 'Foo', and 'foo'. The Header object will contain all of the values of
   * 'Foo' and all of the values of 'foo'.  You can use this method to check to see if a header was set using
   * 'foo' (true), 'Foo' (true), 'FOO' (false), etc.
   *
   * @param string $header Exact header to check for
   *
   * @return bool
   */
  public function hasExactHeader($header) {
    return array_key_exists($header, $this->values);
  }

  /**
   * Check if the collection of headers has a particular value
   *
   * @param string $searchValue     Value to search for
   * @param bool   $caseInsensitive Set to TRUE to use a case insensitive search
   *
   * @return bool
   */
  public function hasValue($searchValue, $caseInsensitive = false) {
    foreach ($this
      ->toArray() as $value) {
      if ($value == $searchValue) {
        return true;
      }
      elseif ($caseInsensitive && !strcasecmp($value, $searchValue)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Remove a specific value from the header
   *
   * @param string $value Value to remove
   *
   * @return self
   */
  public function removeValue($searchValue) {
    foreach ($this->values as $key => $values) {
      foreach ($values as $index => $value) {
        if ($value == $searchValue) {
          unset($this->values[$key][$index]);
          $this
            ->clearCache();
          break 2;
        }
      }
    }
    return $this;
  }

  /**
   * Get all of the header values as a flat array
   * {@inheritdoc}
   */
  public function toArray() {
    if (!$this->arrayCache) {
      $this->arrayCache = array();
      foreach ($this->values as $values) {
        foreach ($values as $value) {
          $this->arrayCache[] = $value;
        }
      }
    }
    return $this->arrayCache;
  }

  /**
   * Get the raw data array of the headers. This array is represented as an associative array of the various cases
   * that might be stored in the header and an array of values associated with each case variation.
   *
   * @return array
   */
  public function raw() {
    return $this->values;
  }

  /**
   * Returns the total number of header values
   *
   * @return int
   */
  public function count() {
    return count($this
      ->toArray());
  }

  /**
   * Get an iterator that can be used to easily iterate over each header value
   *
   * @return \ArrayIterator
   */
  public function getIterator() {
    return new \ArrayIterator($this
      ->toArray());
  }

  /**
   * Clear the internal header cache
   */
  private function clearCache() {
    $this->arrayCache = null;
    $this->stringCache = null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Header::$arrayCache protected property
Header::$glue protected property
Header::$header protected property
Header::$stringCache protected property
Header::$values protected property
Header::add public function Add a value to the list of header values
Header::clearCache private function Clear the internal header cache
Header::count public function Returns the total number of header values
Header::getGlue public function Get the glue used to implode multiple values into a string
Header::getIterator public function Get an iterator that can be used to easily iterate over each header value
Header::getName public function Get the name of the header
Header::hasExactHeader public function Check if a particular case variation is present in the header Example: A header exists on a message for 'Foo', and 'foo'. The Header object will contain all of the values of 'Foo' and all of the values of 'foo'.…
Header::hasValue public function Check if the collection of headers has a particular value
Header::normalize public function Normalize the header into a single standard header with an array of values
Header::raw public function Get the raw data array of the headers. This array is represented as an associative array of the various cases that might be stored in the header and an array of values associated with each case variation.
Header::removeValue public function Remove a specific value from the header
Header::setGlue public function Change the glue used to implode the values
Header::toArray public function Get all of the header values as a flat array Overrides ToArrayInterface::toArray
Header::__construct public function Construct a new header object
Header::__toString public function Convert the header to a string