<?php

namespace YOOtheme\Http;

use YOOtheme\Util\Arr;

trait Message
{
    /**
     * Retrieve an array of attributes.
     *
     * @return array
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

    /**
     * Sets an array of attributes on the instance.
     *
     * @param  array $attributes
     * @return self
     */
    public function setAttributes(array $attributes)
    {
        $this->attributes = $attributes;

        return $this;
    }

    /**
     * Retrieve a attribute value.
     *
     * @param  string $name
     * @param  string $default
     * @return mixed
     */
    public function getAttribute($name, $default = null)
    {
        return Arr::get($this->attributes, $name, $default);
    }

    /**
     * Sets a attribute value on the instance.
     *
     * @param  string $name
     * @param  mixed $value
     * @return self
     */
    public function setAttribute($name, $value)
    {
        Arr::set($this->attributes, $name, $value);

        return $this;
    }

    /**
     * Gets content type.
     *
     * @return string|null
     */
    public function getContentType()
    {
        $result = $this->getHeader('Content-Type');

        return $result ? $result[0] : null;
    }

    /**
     * Gets content length.
     *
     * @return int|null
     */
    public function getContentLength()
    {
        $result = $this->getHeader('Content-Length');

        return $result ? (int) $result[0] : null;
    }
}
