1 <?php
2 /* --------------------------------------------------------------
3 EmailAddress.inc.php 2015-01-29 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('EmailAddressInterface');
13
14 /**
15 * Class EmailAddress
16 *
17 * Represents an email address (both email address and displayed name).
18 *
19 * @category System
20 * @package Email
21 * @subpackage ValueObjects
22 */
23 class EmailAddress implements EmailAddressInterface
24 {
25 /**
26 * Defines the max length of the database field.
27 * @var int
28 */
29 const MAX_LENGTH = 128;
30
31
32 /**
33 * E-Mail address.
34 * @var string
35 */
36 protected $emailAddress;
37
38
39 /**
40 * Constructor
41 *
42 * Executes the validation checks for the email address.
43 *
44 * @throws InvalidArgumentException If the provided argument is not valid.
45 *
46 * @param string $p_emailAddress E-Mail address.
47 */
48 public function __construct($p_emailAddress)
49 {
50 if(!is_string($p_emailAddress) || empty($p_emailAddress))
51 {
52 throw new InvalidArgumentException('Invalid argument provided (expected string email address) $p_emailAddress: '
53 . print_r($p_emailAddress, true));
54 }
55
56 if(strlen(trim($p_emailAddress)) > self::MAX_LENGTH)
57 {
58 throw new InvalidArgumentException('Argument exceeded the maximum database field length ('
59 . self::MAX_LENGTH . '):' . $p_emailAddress);
60 }
61
62 if(!filter_var($p_emailAddress, FILTER_VALIDATE_EMAIL))
63 {
64 throw new InvalidArgumentException('Invalid email address provided $p_emailAddress: ' . $p_emailAddress);
65 }
66
67 $this->emailAddress = $p_emailAddress;
68 }
69
70
71 /**
72 * Returns the email address as a string.
73 *
74 * @return string Equivalent string.
75 */
76 public function __toString()
77 {
78 return $this->emailAddress;
79 }
80 }