1: <?php
2:
3: /**
4: * Avisota newsletter and mailing system
5: *
6: * PHP Version 5.3
7: *
8: * @copyright bit3 UG 2013
9: * @author Tristan Lins <tristan.lins@bit3.de>
10: * @package avisota-core
11: * @license LGPL-3.0+
12: * @link http://avisota.org
13: */
14:
15: namespace Avisota\Recipient;
16:
17: /**
18: * A mutable recipient object.
19: *
20: * @package avisota-core
21: */
22: class MutableRecipient implements RecipientInterface
23: {
24: /**
25: * @var array
26: */
27: protected $data = array();
28:
29: /**
30: * @param $email
31: * @param array $details
32: */
33: public function __construct($email, array $details = array())
34: {
35: $this->setEmail($email);
36: $this->setDetails($details);
37: }
38:
39: /**
40: * {@inheritdoc}
41: */
42: public function getEmail()
43: {
44: return $this->data['email'];
45: }
46:
47: /**
48: * Set the email address.
49: *
50: * @param $email
51: *
52: * @return void
53: * @throws MutableRecipientDataException
54: */
55: public function setEmail($email)
56: {
57: if (empty($email)) {
58: throw new MutableRecipientDataException('Email is required');
59: }
60:
61: $this->data['email'] = (string) $email;
62: }
63:
64: /**
65: * {@inheritdoc}
66: */
67: public function hasDetails()
68: {
69: return count($this->data) > 1;
70: }
71:
72: /**
73: * {@inheritdoc}
74: */
75: public function get($name)
76: {
77: if (array_key_exists($name, $this->data)) {
78: return $this->data[$name];
79: }
80: return null;
81: }
82:
83: /**
84: * Set a personal data field.
85: *
86: * @param string $name The name of the field.
87: * @param mixed $value The value of the field. A value of
88: * <code>null</code> delete the field.
89: *
90: * @return void
91: */
92: public function set($name, $value)
93: {
94: if ($name == 'email') {
95: $this->setEmail($value);
96: }
97: else if ($value === null) {
98: unset($this->data[$name]);
99: }
100: else {
101: $this->data[$name] = $value;
102: }
103: }
104:
105: /**
106: * {@inheritdoc}
107: */
108: public function getDetails()
109: {
110: return $this->data;
111: }
112:
113: /**
114: * Set multiple personal data fields.
115: *
116: * @param array $details
117: *
118: * @return void
119: */
120: public function setDetails(array $details)
121: {
122: foreach ($details as $key => $value) {
123: $this->set($key, $value);
124: }
125: }
126:
127: /**
128: * {@inheritdoc}
129: */
130: public function getKeys()
131: {
132: return array_keys($this->data);
133: }
134: }