<?php

namespace Dflydev\Hawk\Header;

class HeaderFactory
{
    public static function create($fieldName, array $attributes = null)
    {
        $fieldValue = 'Hawk';

        if (null !== $attributes) {
            $index = 0;
            foreach ($attributes as $key => $value) {
                if ($index++ > 0) {
                    $fieldValue .= ',';
                }

                $fieldValue .= ' ' . $key . '="' . $value . '"';
            }
        }

        return new Header($fieldName, $fieldValue, $attributes);
    }

    public static function createFromString($fieldName, $fieldValue, array $requiredKeys = null)
    {
        return static::create(
            $fieldName,
            HeaderParser::parseFieldValue($fieldValue, $requiredKeys)
        );
    }

    public static function createFromHeaderObjectOrString($fieldName, $headerObjectOrString, callable $onError)
    {
        if (is_string($headerObjectOrString)) {
            return static::createFromString($fieldName, $headerObjectOrString);
        } elseif ($headerObjectOrString instanceof Header) {
            return $headerObjectOrString;
        } else {
            throw new \InvalidArgumentException(
                'Header must either be a string or an instance of "Dflydev\Hawk\Header\Header"'
            );
        }
    }
}

<?php

namespace Dflydev\Hawk\Header;

class HeaderFactory
{
    public static function create($fieldName, array $attributes = null)
    {
        $fieldValue = 'Hawk';

        if (null !== $attributes) {
            $index = 0;
            foreach ($attributes as $key => $value) {
                if ($index++ > 0) {
                    $fieldValue .= ',';
                }

                $fieldValue .= ' ' . $key . '="' . $value . '"';
            }
        }

        return new Header($fieldName, $fieldValue, $attributes);
    }

    public static function createFromString($fieldName, $fieldValue, array $requiredKeys = null)
    {
        return static::create(
            $fieldName,
            HeaderParser::parseFieldValue($fieldValue, $requiredKeys)
        );
    }

    public static function createFromHeaderObjectOrString($fieldName, $headerObjectOrString, $onError)
    {
        if (is_string($headerObjectOrString)) {
            return static::createFromString($fieldName, $headerObjectOrString);
        } elseif ($headerObjectOrString instanceof Header) {
            return $headerObjectOrString;
        } else {
            call_user_func($onError);
        }
    }
}

Remove callable for php 5.3 compat.
Remove callable for php 5.3 compat.
