<commit_before><?php

namespace Aidantwoods\SecureHeaders;

use InvalidArgumentException;

class Header
{
    private $name;
    private $value;

    private $attributes = array();

    public function __construct($name, $value = '')
    {
        $this->name = $name;
        $this->value = $value;

        $this->parseAttributes();
    }

    public function getName()
    {
        return strtolower($this->name);
    }

    public function getFriendlyName()
    {
        $friendlyHeader = str_replace('-', ' ', $this->getName());
        return ucwords($friendlyHeader);
    }

    public function is($name)
    {
        return strtolower($name) === strtolower($this->name);
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setValue($newValue)
    {
        $this->value = $newValue;

        $this->parseAttributes();
    }

    public function getFirstAttributeName()
    {
        reset($this->attributes);

        return key($this->attributes);
    }

    public function getAttributeValue($name)
    {
        if ( ! $this->hasAttribute($name)) {
            throw new InvalidArgumentException(
                "Attribute '$name' was not found"
            );
        }

        return $this->attributes[strtolower($name)][0]['value'];
    }

    public function hasAttribute($name)
    {
        $name = strtolower($name);

        return array_key_exists($name, $this->attributes);
    }

    public function removeAttribute($name)
    {
        $name = strtolower($name);
        unset($this->attributes[$name]);

        $this->writeAttributesToValue();
    }

    public function ensureAttributeMaximum($name, $maxValue)
    {
        if (isset($this->attributes[$name])) {
            foreach ($this->attributes[$name] as &$attribute) {
                if (intval($attribute['value']) > $maxValue) {
                    $attribute['value'] = $maxValue;
                }
            }

            $this->writeAttributesToValue();
        }
    }

    public function enableAttribute($name)
    {
        $key = strtolower($name);

        $this->attributes[$key] = array(
            array(
                'name' => $name,
                'value' => true
            )
        );

        $this->writeAttributesToValue();
    }

    public function forEachAttribute($callback)
    {
        foreach ($this->attributes as $attributes) {
            foreach ($attributes as $attribute) {
                $callback($attribute['name'], $attribute['value']);
            }
        }
    }

    public function __toString()
    {
        return $this->name . ':' .(empty($this->value) ? '' : ' ' . $this->value);
    }

    private function parseAttributes()
    {
        $parts = explode('; ', $this->value);

        $this->attributes = array();
        foreach ($parts as $part) {
            $attrParts = explode('=', $part, 2);

            $type = strtolower($attrParts[0]);<commit_msg>allow setting text values in header attribute<commit_after><?php

namespace Aidantwoods\SecureHeaders;

use InvalidArgumentException;

class Header
{
    private $name;
    private $value;

    private $attributes = array();

    public function __construct($name, $value = '')
    {
        $this->name = $name;
        $this->value = $value;

        $this->parseAttributes();
    }

    public function getName()
    {
        return strtolower($this->name);
    }

    public function getFriendlyName()
    {
        $friendlyHeader = str_replace('-', ' ', $this->getName());
        return ucwords($friendlyHeader);
    }

    public function is($name)
    {
        return strtolower($name) === strtolower($this->name);
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setValue($newValue)
    {
        $this->value = $newValue;

        $this->parseAttributes();
    }

    public function getFirstAttributeName()
    {
        reset($this->attributes);

        return key($this->attributes);
    }

    public function getAttributeValue($name)
    {
        if ( ! $this->hasAttribute($name)) {
            throw new InvalidArgumentException(
                "Attribute '$name' was not found"
            );
        }

        return $this->attributes[strtolower($name)][0]['value'];
    }

    public function hasAttribute($name)
    {
        $name = strtolower($name);

        return array_key_exists($name, $this->attributes);
    }

    public function removeAttribute($name)
    {
        $name = strtolower($name);
        unset($this->attributes[$name]);

        $this->writeAttributesToValue();
    }

    public function ensureAttributeMaximum($name, $maxValue)
    {
        if (isset($this->attributes[$name])) {
            foreach ($this->attributes[$name] as &$attribute) {
                if (intval($attribute['value']) > $maxValue) {
                    $attribute['value'] = $maxValue;
                }
            }

            $this->writeAttributesToValue();
        }
    }

    public function setAttribute($name, $value = true)
    {
        $key = strtolower($name);

        $this->attributes[$key] = array(
            array(
                'name' => $name,
                'value' => $value
            )
        );

        if ($value === false)
        {
            unset($this->attributes[$key]);
        }

        $this->writeAttributesToValue();
    }

    public function forEachAttribute($callback)
    {
        foreach ($this->attributes as $attributes) {
            foreach ($attributes as $attribute) {
                $callback($attribute['name'], $attribute['value']);
            }
        }
    }

    public function __toString()
    {
        return $this->name . ':' .(empty($this->value) ? '' : ' ' . $this->value);
    }

    private function parseAttributes()
    {
        $parts = explode('; ', $this->value);

        $this->attributes = array();
        foreach ($parts as $part) {
            $attrParts = explode('=', $part, 2);

            $type = strtolower($attrParts[0]);
{'fraction_non_alphanumeric': 0.14540353193596303, 'fraction_numerical': 0.0009902624195411785, 'mean_word_length': 1.983751846381093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5646555', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1619, 'n_words': 323}