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: * A Path Header in Swift Mailer, such a Return-Path.
13: * @package Swift
14: * @subpackage Mime
15: * @author Chris Corbyn
16: */
17: class Swift_Mime_Headers_PathHeader extends Swift_Mime_Headers_AbstractHeader
18: {
19: /**
20: * The address in this Header (if specified).
21: * @var string
22: * @access private
23: */
24: private $_address;
25:
26: /**
27: * Creates a new PathHeader with the given $name.
28: * @param string $name
29: * @param Swift_Mime_Grammar $grammar
30: */
31: public function __construct($name, Swift_Mime_Grammar $grammar)
32: {
33: $this->setFieldName($name);
34: parent::__construct($grammar);
35: }
36:
37: /**
38: * Get the type of Header that this instance represents.
39: * @return int
40: * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
41: * @see TYPE_DATE, TYPE_ID, TYPE_PATH
42: */
43: public function getFieldType()
44: {
45: return self::TYPE_PATH;
46: }
47:
48: /**
49: * Set the model for the field body.
50: * This method takes a string for an address.
51: * @param string $model
52: * @throws Swift_RfcComplianceException
53: */
54: public function setFieldBodyModel($model)
55: {
56: $this->setAddress($model);
57: }
58:
59: /**
60: * Get the model for the field body.
61: * This method returns a string email address.
62: * @return mixed
63: */
64: public function getFieldBodyModel()
65: {
66: return $this->getAddress();
67: }
68:
69: /**
70: * Set the Address which should appear in this Header.
71: * @param string $address
72: * @throws Swift_RfcComplianceException
73: */
74: public function setAddress($address)
75: {
76: if (is_null($address)) {
77: $this->_address = null;
78: } elseif ('' == $address) {
79: $this->_address = '';
80: } else {
81: $this->_assertValidAddress($address);
82: $this->_address = $address;
83: }
84: $this->setCachedValue(null);
85: }
86:
87: /**
88: * Get the address which is used in this Header (if any).
89: * Null is returned if no address is set.
90: * @return string
91: */
92: public function getAddress()
93: {
94: return $this->_address;
95: }
96:
97: /**
98: * Get the string value of the body in this Header.
99: * This is not necessarily RFC 2822 compliant since folding white space will
100: * not be added at this stage (see {@link toString()} for that).
101: * @return string
102: * @see toString()
103: */
104: public function getFieldBody()
105: {
106: if (!$this->getCachedValue()) {
107: if (isset($this->_address)) {
108: $this->setCachedValue('<' . $this->_address . '>');
109: }
110: }
111:
112: return $this->getCachedValue();
113: }
114:
115: /**
116: * Throws an Exception if the address passed does not comply with RFC 2822.
117: * @param string $address
118: * @throws Swift_RfcComplianceException If invalid.
119: * @access private
120: */
121: private function _assertValidAddress($address)
122: {
123: if (!preg_match('/^' . $this->getGrammar()->getDefinition('addr-spec') . '$/D',
124: $address))
125: {
126: throw new Swift_RfcComplianceException(
127: 'Address set in PathHeader does not comply with addr-spec of RFC 2822.'
128: );
129: }
130: }
131: }
132: