1: <?php
2:
3: /*
4: * This file is part of SwiftMailer.
5: * (c) 2004-2009 Chris Corbyn
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: /**
12: * An ID MIME Header for something like Message-ID or Content-ID.
13: * @package Swift
14: * @subpackage Mime
15: * @author Chris Corbyn
16: */
17: class Swift_Mime_Headers_IdentificationHeader extends Swift_Mime_Headers_AbstractHeader
18: {
19: /**
20: * The IDs used in the value of this Header.
21: * This may hold multiple IDs or just a single ID.
22: * @var string[]
23: * @access private
24: */
25: private $_ids = array();
26:
27: /**
28: * Creates a new IdentificationHeader with the given $name and $id.
29: * @param string $name
30: * @param Swift_Mime_Grammar $grammar
31: */
32: public function __construct($name, Swift_Mime_Grammar $grammar)
33: {
34: $this->setFieldName($name);
35: parent::__construct($grammar);
36: }
37:
38: /**
39: * Get the type of Header that this instance represents.
40: * @return int
41: * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
42: * @see TYPE_DATE, TYPE_ID, TYPE_PATH
43: */
44: public function getFieldType()
45: {
46: return self::TYPE_ID;
47: }
48:
49: /**
50: * Set the model for the field body.
51: * This method takes a string ID, or an array of IDs
52: * @param mixed $model
53: * @throws Swift_RfcComplianceException
54: */
55: public function setFieldBodyModel($model)
56: {
57: $this->setId($model);
58: }
59:
60: /**
61: * Get the model for the field body.
62: * This method returns an array of IDs
63: * @return array
64: */
65: public function getFieldBodyModel()
66: {
67: return $this->getIds();
68: }
69:
70: /**
71: * Set the ID used in the value of this header.
72: * @param string|array $id
73: * @throws Swift_RfcComplianceException
74: */
75: public function setId($id)
76: {
77: $this->setIds(is_array($id) ? $id : array($id));
78: }
79:
80: /**
81: * Get the ID used in the value of this Header.
82: * If multiple IDs are set only the first is returned.
83: * @return string
84: */
85: public function getId()
86: {
87: if (count($this->_ids) > 0) {
88: return $this->_ids[0];
89: }
90: }
91:
92: /**
93: * Set a collection of IDs to use in the value of this Header.
94: * @param string[] $ids
95: * @throws Swift_RfcComplianceException
96: */
97: public function setIds(array $ids)
98: {
99: $actualIds = array();
100:
101: foreach ($ids as $id) {
102: $this->_assertValidId($id);
103: $actualIds[] = $id;
104: }
105:
106: $this->clearCachedValueIf($this->_ids != $actualIds);
107: $this->_ids = $actualIds;
108: }
109:
110: /**
111: * Get the list of IDs used in this Header.
112: * @return string[]
113: */
114: public function getIds()
115: {
116: return $this->_ids;
117: }
118:
119: /**
120: * Get the string value of the body in this Header.
121: * This is not necessarily RFC 2822 compliant since folding white space will
122: * not be added at this stage (see {@link toString()} for that).
123: * @return string
124: * @see toString()
125: * @throws Swift_RfcComplianceException
126: */
127: public function getFieldBody()
128: {
129: if (!$this->getCachedValue()) {
130: $angleAddrs = array();
131:
132: foreach ($this->_ids as $id) {
133: $angleAddrs[] = '<' . $id . '>';
134: }
135:
136: $this->setCachedValue(implode(' ', $angleAddrs));
137: }
138:
139: return $this->getCachedValue();
140: }
141:
142: /**
143: * Throws an Exception if the id passed does not comply with RFC 2822.
144: * @param string $id
145: * @throws Swift_RfcComplianceException
146: */
147: private function _assertValidId($id)
148: {
149: if (!preg_match(
150: '/^' . $this->getGrammar()->getDefinition('id-left') . '@' .
151: $this->getGrammar()->getDefinition('id-right') . '$/D',
152: $id
153: ))
154: {
155: throw new Swift_RfcComplianceException(
156: 'Invalid ID given <' . $id . '>'
157: );
158: }
159: }
160: }
161: