Skip to content

Commit 5790f2d

Browse files
committed
Added email name parser support.
1 parent 3457b96 commit 5790f2d

20 files changed

+431
-27
lines changed

src/org/nameapi/client/services/email/EmailServiceFactory.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44

55
use org\nameapi\ontology\input\context\Context;
66
use org\nameapi\client\services\email\disposableemailaddressdetector\DisposableEmailAddressDetectorService;
7+
use org\nameapi\client\services\email\emailnameparser\EmailNameParserService;
78

89
require_once('disposableemailaddressdetector/DisposableEmailAddressDetectorService.php');
10+
require_once('emailnameparser/EmailNameParserService.php');
911

1012

1113
/**
12-
*
14+
* Provides access to the email-related services.
1315
*/
1416
class EmailServiceFactory {
1517

1618
private $context;
1719
private $disposableEmailAddressDetector;
20+
private $emailNameParser;
1821

1922
/**
2023
*/
@@ -32,4 +35,14 @@ public function disposableEmailAddressDetector() {
3235
return $this->disposableEmailAddressDetector;
3336
}
3437

38+
/**
39+
* @return EmailNameParserService
40+
*/
41+
public function emailNameParser() {
42+
if ($this->emailNameParser==null) {
43+
$this->emailNameParser = new EmailNameParserService($this->context);
44+
}
45+
return $this->emailNameParser;
46+
}
47+
3548
}

src/org/nameapi/client/services/email/disposableemailaddressdetector/wsdl/DisposableEmailAddressDetectorArguments.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ class DisposableEmailAddressDetectorArguments {
1010
public $emailAddress = null;
1111

1212
/**
13-
*
1413
* @param Context $context
1514
* @param string $emailAddress
16-
* @access public
1715
*/
18-
public function __construct($context, $emailAddress) {
16+
public function __construct(Context $context, $emailAddress) {
1917
$this->context = $context;
2018
$this->emailAddress = $emailAddress;
2119
}

src/org/nameapi/client/services/email/disposableemailaddressdetector/wsdl/SoapDisposableEmailAddressDetectorService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ class SoapDisposableEmailAddressDetectorService extends BaseSoapClient {
2323
);
2424

2525
/**
26-
*
2726
* @param array $options A array of config values
2827
* @param string $wsdl The wsdl file to use
29-
* @access public
3028
*/
3129
public function __construct(array $options = array(), $wsdl = 'http://api.nameapi.org/soap/v4.0/email/disposableemailaddressdetector?wsdl') {
3230
parent::__construct($wsdl, self::$classmap, $options);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
/**
6+
* Class EmailAddressParsingResultType
7+
*
8+
* Possible values are: NAME, INITIAL
9+
*/
10+
class EmailAddressNameType {
11+
12+
/**
13+
* @var string $value
14+
*/
15+
private $value = null;
16+
17+
public function __construct($value) {
18+
if ($value!=='NAME'
19+
&& $value!=='INITIAL'
20+
) {
21+
throw new \Exception('Invalid value for EmailAddressNameType: '.$value.'!');
22+
}
23+
$this->value = $value;
24+
}
25+
26+
27+
public function toString() {
28+
return $this->value;
29+
}
30+
public function __toString() {
31+
return $this->toString();
32+
}
33+
34+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
6+
/**
7+
* Class EmailAddressParsingResultType
8+
*
9+
* Possible values are:
10+
*
11+
* DEPARTMENT
12+
* The email address belongs to a department, such as [email protected].
13+
*
14+
* TECHNICAL
15+
* It is a technical email address for the domain, such as [email protected].
16+
*
17+
* INITIALS
18+
* The email address contains a person's initials such as [email protected].
19+
* <p>Note that this answer is a guess, the 2 letters could also have another meaning
20+
* such as a short given name or surname, or something completely different.</p>
21+
*
22+
* PERSON_NAME
23+
* The email address contains a person's name such as [email protected].
24+
*
25+
* PSEUDONYM
26+
* The email address uses a pseudonym as the user name such as [email protected] or [email protected].
27+
*
28+
* NOT_A_NAME
29+
* There is no name in the address, for example [email protected]
30+
* <p>The address may be personal or non-personal, can't say (as in UNKNOWN)
31+
* but it is clear that no name can be found in it.</p>
32+
*
33+
* UNKNOWN
34+
* The email address could not be classified and hence the service failed to extract a name.
35+
*
36+
*
37+
*/
38+
final class EmailAddressParsingResultType {
39+
40+
/**
41+
* @var string $value
42+
*/
43+
private $value = null;
44+
45+
public function __construct($value) {
46+
if ($value!=='DEPARTMENT'
47+
&& $value!=='TECHNICAL'
48+
&& $value!=='INITIALS'
49+
&& $value!=='PERSON_NAME'
50+
&& $value!=='PSEUDONYM'
51+
&& $value!=='NOT_A_NAME'
52+
&& $value!=='UNKNOWN'
53+
) {
54+
throw new \Exception('Invalid value for EmailAddressParsingResultType: '.$value.'!');
55+
}
56+
$this->value = $value;
57+
}
58+
59+
60+
public function toString() {
61+
return $this->value;
62+
}
63+
public function __toString() {
64+
return $this->toString();
65+
}
66+
67+
}
68+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
require_once('NameFromEmailAddress.php');
6+
7+
/**
8+
* One way (and ideally the only way) of parsing an email address.
9+
*/
10+
class EmailNameParserMatch {
11+
12+
/**
13+
* @var NameFromEmailAddress[] $givenNames
14+
*/
15+
private $givenNames = null;
16+
17+
/**
18+
* @var NameFromEmailAddress[] $surnames
19+
*/
20+
private $surnames = null;
21+
22+
/**
23+
* @var float $confidence
24+
*/
25+
private $confidence = null;
26+
27+
28+
/**
29+
* @param NameFromEmailAddress[] $givenNames
30+
* @param NameFromEmailAddress[] $surnames
31+
* @param float $confidence
32+
*/
33+
public function __construct($givenNames, $surnames, $confidence) {
34+
$this->givenNames = $givenNames;
35+
$this->surnames = $surnames;
36+
$this->confidence = $confidence;
37+
}
38+
39+
/**
40+
* @return NameFromEmailAddress[]
41+
*/
42+
public function getGivenNames() {
43+
return $this->givenNames;
44+
}
45+
46+
/**
47+
* @return NameFromEmailAddress[]
48+
*/
49+
public function getSurnames() {
50+
return $this->surnames;
51+
}
52+
53+
/**
54+
* @return float 0-1
55+
*/
56+
public function getConfidence() {
57+
return $this->confidence;
58+
}
59+
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
require_once('EmailAddressParsingResultType.php');
6+
require_once('EmailNameParserMatch.php');
7+
8+
class EmailNameParserResult {
9+
10+
/**
11+
* @var EmailAddressParsingResultType
12+
*/
13+
private $resultType = null;
14+
15+
/**
16+
* @var EmailNameParserMatch[] $matches
17+
*/
18+
private $matches = null;
19+
20+
21+
/**
22+
* @param EmailAddressParsingResultType $resultType
23+
* @param EmailNameParserMatch[] $matches
24+
*/
25+
public function __construct(EmailAddressParsingResultType $resultType, $matches) {
26+
$this->resultType = $resultType;
27+
$this->matches = $matches;
28+
}
29+
30+
/**
31+
* @return EmailAddressParsingResultType
32+
*/
33+
public function getResultType() {
34+
return $this->resultType;
35+
}
36+
37+
/**
38+
* @return EmailNameParserMatch[]
39+
*/
40+
public function getMatches() {
41+
return $this->matches;
42+
}
43+
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
use org\nameapi\ontology\input\context\Context;
6+
7+
require_once('wsdl/SoapEmailNameParserService.php');
8+
require_once('EmailNameParserResult.php');
9+
10+
/**
11+
* This is the service class for the web service offered at
12+
* http://api.nameapi.org/soap/v4.0/email/emailnameparser?wsdl
13+
*
14+
* HOW TO USE:
15+
* $emailNameParser = $myServiceFactory->emailServices()->emailNameParser();
16+
* $result = $emailNameParser->parse("[email protected]");
17+
* echo $result->getDisposable()->toString()); //will print 'YES'
18+
*/
19+
class EmailNameParserService {
20+
21+
private $context;
22+
private $soapEmailNameParserService;
23+
24+
public function __construct(Context $context) {
25+
$this->context = $context;
26+
$this->soapEmailNameParserService = new wsdl\SoapEmailNameParserService();
27+
}
28+
29+
/**
30+
* @param string $emailAddress
31+
* @return EmailNameParserResult
32+
*/
33+
public function parse($emailAddress) {
34+
$parameters = new wsdl\EmailNameParserServiceArguments($this->context, $emailAddress);
35+
$result = $this->soapEmailNameParserService->parse($parameters);
36+
$matches = array();
37+
if (isSet($result->return->matches)) {
38+
foreach ($result->return->matches as $match) {
39+
$givenNames = array();
40+
$surnames = array();
41+
if (isSet($match->givenNames)) {
42+
foreach ($match->givenNames as $name) {
43+
array_push($givenNames, new NameFromEmailAddress($name->name, new EmailAddressNameType($name->nameType)));
44+
}
45+
}
46+
if (isSet($match->surnames)) {
47+
foreach ($match->surnames as $name) {
48+
array_push($surnames, new NameFromEmailAddress($name->name, new EmailAddressNameType($name->nameType)));
49+
}
50+
}
51+
array_push($matches, new EmailNameParserMatch($givenNames, $surnames, $match->confidence));
52+
}
53+
}
54+
return new EmailNameParserResult(
55+
new EmailAddressParsingResultType($result->return->resultType),
56+
$matches
57+
);
58+
}
59+
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\email\emailnameparser;
4+
5+
require_once('EmailAddressNameType.php');
6+
7+
class NameFromEmailAddress {
8+
9+
/**
10+
* @var string $name
11+
*/
12+
private $name = null;
13+
14+
/**
15+
* @var EmailAddressNameType $nameType
16+
*/
17+
private $nameType = null;
18+
19+
20+
/**
21+
* @param string[] $name
22+
* @param EmailAddressNameType $nameType
23+
*/
24+
public function __construct($name, EmailAddressNameType $nameType) {
25+
$this->name = $name;
26+
$this->nameType = $nameType;
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getName() {
33+
return $this->name;
34+
}
35+
36+
/**
37+
* @return EmailAddressNameType
38+
*/
39+
public function getNameType() {
40+
return $this->nameType;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)