1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Mail\Header;
11
12use Zend\Mail\Headers;
13
14/**
15 * @todo       Allow setting date from DateTime, Zend\Date, or string
16 */
17class Received implements HeaderInterface, MultipleHeadersInterface
18{
19    /**
20     * @var string
21     */
22    protected $value;
23
24    public static function fromString($headerLine)
25    {
26        list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
27        $value = HeaderWrap::mimeDecodeValue($value);
28
29        // check to ensure proper header type for this factory
30        if (strtolower($name) !== 'received') {
31            throw new Exception\InvalidArgumentException('Invalid header line for Received string');
32        }
33
34        $header = new static($value);
35
36        return $header;
37    }
38
39    public function __construct($value = '')
40    {
41        if (! HeaderValue::isValid($value)) {
42            throw new Exception\InvalidArgumentException('Invalid Received value provided');
43        }
44        $this->value = $value;
45    }
46
47    public function getFieldName()
48    {
49        return 'Received';
50    }
51
52    public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
53    {
54        return $this->value;
55    }
56
57    public function setEncoding($encoding)
58    {
59        // This header must be always in US-ASCII
60        return $this;
61    }
62
63    public function getEncoding()
64    {
65        return 'ASCII';
66    }
67
68    public function toString()
69    {
70        return 'Received: ' . $this->getFieldValue();
71    }
72
73    /**
74     * Serialize collection of Received headers to string
75     *
76     * @param  array $headers
77     * @throws Exception\RuntimeException
78     * @return string
79     */
80    public function toStringMultipleHeaders(array $headers)
81    {
82        $strings = array($this->toString());
83        foreach ($headers as $header) {
84            if (! $header instanceof Received) {
85                throw new Exception\RuntimeException(
86                    'The Received multiple header implementation can only accept an array of Received headers'
87                );
88            }
89            $strings[] = $header->toString();
90        }
91        return implode(Headers::EOL, $strings);
92    }
93}
94