Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AcceptHeaderItem.php

class AcceptHeaderItem

Represents an Accept-* header item.

@author Jean-François Simon <contact@jfsimon.fr>

Hierarchy

  • class \Symfony\Component\HttpFoundation\AcceptHeaderItem

Expanded class hierarchy of AcceptHeaderItem

File

vendor/symfony/http-foundation/AcceptHeaderItem.php, line 19

Namespace

Symfony\Component\HttpFoundation
View source
class AcceptHeaderItem {
    private float $quality = 1.0;
    private int $index = 0;
    private array $attributes = [];
    public function __construct(string $value, array $attributes = []) {
        foreach ($attributes as $name => $value) {
            $this->setAttribute($name, $value);
        }
    }
    
    /**
     * Builds an AcceptHeaderInstance instance from a string.
     */
    public static function fromString(?string $itemValue) : self {
        $parts = HeaderUtils::split($itemValue ?? '', ';=');
        $part = array_shift($parts);
        $attributes = HeaderUtils::combine($parts);
        return new self($part[0], $attributes);
    }
    
    /**
     * Returns header value's string representation.
     */
    public function __toString() : string {
        $string = $this->value . ($this->quality < 1 ? ';q=' . $this->quality : '');
        if (\count($this->attributes) > 0) {
            $string .= '; ' . HeaderUtils::toString($this->attributes, ';');
        }
        return $string;
    }
    
    /**
     * Set the item value.
     *
     * @return $this
     */
    public function setValue(string $value) : static {
        $this->value = $value;
        return $this;
    }
    
    /**
     * Returns the item value.
     */
    public function getValue() : string {
        return $this->value;
    }
    
    /**
     * Set the item quality.
     *
     * @return $this
     */
    public function setQuality(float $quality) : static {
        $this->quality = $quality;
        return $this;
    }
    
    /**
     * Returns the item quality.
     */
    public function getQuality() : float {
        return $this->quality;
    }
    
    /**
     * Set the item index.
     *
     * @return $this
     */
    public function setIndex(int $index) : static {
        $this->index = $index;
        return $this;
    }
    
    /**
     * Returns the item index.
     */
    public function getIndex() : int {
        return $this->index;
    }
    
    /**
     * Tests if an attribute exists.
     */
    public function hasAttribute(string $name) : bool {
        return isset($this->attributes[$name]);
    }
    
    /**
     * Returns an attribute by its name.
     */
    public function getAttribute(string $name, mixed $default = null) : mixed {
        return $this->attributes[$name] ?? $default;
    }
    
    /**
     * Returns all attributes.
     */
    public function getAttributes() : array {
        return $this->attributes;
    }
    
    /**
     * Set an attribute.
     *
     * @return $this
     */
    public function setAttribute(string $name, string $value) : static {
        if ('q' === $name) {
            $this->quality = (double) $value;
        }
        else {
            $this->attributes[$name] = $value;
        }
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
AcceptHeaderItem::$attributes private property
AcceptHeaderItem::$index private property
AcceptHeaderItem::$quality private property
AcceptHeaderItem::fromString public static function Builds an AcceptHeaderInstance instance from a string.
AcceptHeaderItem::getAttribute public function Returns an attribute by its name.
AcceptHeaderItem::getAttributes public function Returns all attributes.
AcceptHeaderItem::getIndex public function Returns the item index.
AcceptHeaderItem::getQuality public function Returns the item quality.
AcceptHeaderItem::getValue public function Returns the item value.
AcceptHeaderItem::hasAttribute public function Tests if an attribute exists.
AcceptHeaderItem::setAttribute public function Set an attribute.
AcceptHeaderItem::setIndex public function Set the item index.
AcceptHeaderItem::setQuality public function Set the item quality.
AcceptHeaderItem::setValue public function Set the item value.
AcceptHeaderItem::__construct public function
AcceptHeaderItem::__toString public function Returns header value&#039;s string representation.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal