Completed
Push — master ( eef289...972a89 )
by Jeroen
52:29 queued 28:31
created

Address::unsetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Elgg\Email;
4
5
use Zend\Mail\Address as ZendAddress;
6
use Zend\Mail\Exception\InvalidArgumentException;
7
use Zend\Validator\EmailAddress as EmailAddressValidator;
8
use Zend\Validator\Hostname;
9
10
/**
11
 * Email address
12
 *
13
 * @since 3.0
14
 */
15
class Address extends ZendAddress {
16
	
17
	/**
18
	 * Set the email address
19
	 *
20
	 * @param string $email the new email address
21
	 *
22
	 * @return void
23
	 * @throws \Zend\Mail\Exception\InvalidArgumentException
24
	 * @since 3.0
25
	 */
26 2
	public function setEmail($email) {
27
		
28 2
	 	if (!is_string($email) || empty($email)) {
29
			throw new InvalidArgumentException('Email must be a valid email address');
30
		}
31
		
32 2
		if (preg_match("/[\r\n]/", $email)) {
33
			throw new InvalidArgumentException('CRLF injection detected');
34
		}
35
		
36 2
		$emailAddressValidator = new EmailAddressValidator(Hostname::ALLOW_DNS | Hostname::ALLOW_LOCAL);
37 2
		if (!$emailAddressValidator->isValid($email)) {
38 1
			$invalidMessages = $emailAddressValidator->getMessages();
39 1
			throw new InvalidArgumentException(array_shift($invalidMessages));
40
		}
41
		
42 1
		$this->email = $email;
43 1
	}
44
	
45
	/**
46
	 * Set the name
47
	 *
48
	 * @param string $name the new name
49
	 *
50
	 * @return void
51
	 * @throws \Zend\Mail\Exception\InvalidArgumentException
52
	 * @since 3.0
53
	 */
54 3
	public function setName($name) {
55
		
56 3
		if (!is_string($name)) {
57 2
			throw new InvalidArgumentException('Name must be a string');
58
		}
59
		
60 1
		if (preg_match("/[\r\n]/", $name)) {
61
			throw new InvalidArgumentException('CRLF injection detected');
62
		}
63
		
64 1
		$this->name = $name;
65 1
	}
66
	
67
	/**
68
	 * Clear the name from the email address
69
	 *
70
	 * @return void
71
	 * @since 3.0
72
	 */
73 1
	public function unsetName() {
74 1
		$this->name = null;
75 1
	}
76
	
77
	/**
78
	 * Parses strings like "Evan <[email protected]>" into name/email objects.
79
	 *
80
	 * This is not very sophisticated and only used to provide a light BC effort.
81
	 *
82
	 * @param string $contact e.g. "Evan <[email protected]>"
83
	 *
84
	 * @return \Elgg\Email\Address
85
	 * @throws \Zend\Mail\Exception\InvalidArgumentException
86
	 * @since 3.0
87
	 */
88 11
	public static function fromString($contact) {
89 11
		$containsName = preg_match('/<(.*)>/', $contact, $matches) == 1;
90 11
		if ($containsName) {
91 3
			$name = trim(substr($contact, 0, strpos($contact, '<')));
92 3
			return new self($matches[1], $name);
93
		} else {
94 8
			return new self(trim($contact));
95
		}
96
	}
97
	
98
	/**
99
	 * Format an email address and name into a formatted email address
100
	 *
101
	 * eg "Some name <[email protected]>"
102
	 *
103
	 * @param string $email the email address
104
	 * @param string $name  the name
105
	 *
106
	 * @return string
107
	 * @throws \Zend\Mail\Exception\InvalidArgumentException
108
	 * @since 3.0
109
	 */
110 4
	public static function getFormattedEmailAddress($email, $name = null) {
111 4
		$mail = new self($email, $name);
112 2
		return $mail->toString();
113
	}
114
}
115