1: <?php
2: /**
3: * Object representation of a RFC 822 e-mail group.
4: *
5: * @since 1.1.0
6: *
7: * Copyright 2012 Horde LLC (http://www.horde.org/)
8: *
9: * See the enclosed file COPYING for license information (BSD). If you
10: * did not receive this file, see http://www.horde.org/licenses/bsd.
11: *
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/bsd New BSD License
15: * @package Mail
16: */
17:
18: /**
19: * Object representation of a RFC 822 e-mail address.
20: *
21: * @author Michael Slusarz <slusarz@horde.org>
22: * @category Horde
23: * @license http://www.horde.org/licenses/bsd New BSD License
24: * @package Mail
25: */
26: class Horde_Mail_Rfc822_Group extends Horde_Mail_Rfc822_Object implements ArrayAccess
27: {
28: /**
29: * List of group e-mail address objects.
30: *
31: * @var array
32: */
33: public $addresses = array();
34:
35: /**
36: * Group name.
37: *
38: * @var string
39: */
40: public $groupname = '';
41:
42: /**
43: * Constructor.
44: *
45: * @param string $groupname If set, used as the group name. (Since 1.2.0)
46: * @param string $addresses If set, addresses is parsed and used as the
47: * list of group addresses. (Since 1.2.0)
48: * Addresses are not verified; sub-groups are
49: * eliminated.
50: */
51: public function __construct($groupname = null, $addresses = null)
52: {
53: if (!is_null($groupname)) {
54: $this->groupname = $groupname;
55: }
56:
57: if (!is_null($addresses)) {
58: $rfc822 = new Horde_Mail_Rfc822();
59: $this->addresses = $rfc822->parseAddressList($addresses, array(
60: 'nest_groups' => false,
61: 'validate' => false
62: ));
63: }
64: }
65:
66: /**
67: */
68: public function __get($name)
69: {
70: switch ($name) {
71: case 'groupname_decoded':
72: // DEPRECATED
73: return Horde_Mime::decode($this->groupname, 'UTF-8');
74:
75: case 'groupname_encoded':
76: return Horde_Mime::encode($this->groupname, 'UTF-8');
77:
78: default:
79: return null;
80: }
81: }
82:
83: /**
84: * Write a group address given information in this part.
85: *
86: * @param array $opts Optional arguments:
87: * - encode: (boolean) MIME encode the groupname/personal parts?
88: * - idn: (boolean) See Horde_Mime_Address#writeAddress().
89: *
90: * @return string The correctly escaped/quoted address.
91: */
92: public function writeAddress(array $opts = array())
93: {
94: $addr = array();
95: foreach ($this->addresses as $val) {
96: $addr[] = $val->writeAddress(array(
97: 'encode' => !empty($opts['encode']),
98: 'idn' => (isset($opts['idn']) ? $opts['idn'] : null)
99: ));
100: }
101:
102: $groupname = empty($opts['encode'])
103: ? $this->groupname
104: : $this->groupname_encoded;
105: $rfc822 = new Horde_Mail_Rfc822();
106:
107: return $rfc822->encode($groupname, 'address') . ':' .
108: (empty($addr) ? '' : (' ' . implode(', ', $addr)) . ';');
109: }
110:
111: /* ArrayAccess methods. TODO: Here for BC purposes. Remove for 2.0. */
112:
113: /**
114: */
115: public function offsetExists($offset)
116: {
117: switch ($offset) {
118: case 'addresses':
119: case 'groupname':
120: return true;
121:
122: default:
123: return false;
124: }
125: }
126:
127: /**
128: */
129: public function offsetGet($offset)
130: {
131: return $this->offsetExists($offset)
132: ? $this->$offset
133: : null;
134: }
135:
136: /**
137: */
138: public function offsetSet($offset, $value)
139: {
140: if ($this->offsetExists($offset)) {
141: $this->$offset = $value;
142: }
143: }
144:
145: /**
146: */
147: public function offsetUnset($offset)
148: {
149: /* Don't allow unsetting of values. */
150: }
151:
152: }
153: