Skip to content

Commit 0c70820

Browse files
committed
New service "risk detector", initial functional version. #10
1 parent 588c478 commit 0c70820

File tree

11 files changed

+571
-0
lines changed

11 files changed

+571
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,15 @@ $result = $deaDetector->isDisposable("[email protected]");
181181
echo $result->getDisposable()->toString()); //will print 'YES'
182182
```
183183

184+
185+
## Risk Detector
186+
187+
The Risk-Detector checks all data in the person input, including the name, address, birthdate,
188+
email address and phone number for fake and suspicious data.
189+
190+
```php
191+
$riskDetector = $serviceFactory->riskServices()->personRiskDetector();
192+
$riskResult = $riskDetector->detect($inputPerson);
193+
var_dump($riskResult);
194+
```
195+

src/org/nameapi/client/services/ServiceFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require_once(__DIR__.'/matcher/MatcherServiceFactory.php');
1414
require_once(__DIR__.'/formatter/FormatterServiceFactory.php');
1515
require_once(__DIR__.'/email/EmailServiceFactory.php');
16+
require_once(__DIR__.'/riskdetector/RiskDetectorServiceFactory.php');
1617

1718
require_once(__DIR__.'/../http/RestHttpClient.php');
1819

@@ -59,6 +60,7 @@ class ServiceFactory {
5960
private $matcherServiceFactory;
6061
private $formatterServiceFactory;
6162
private $emailServiceFactory;
63+
private $riskServiceFactory;
6264

6365

6466
/**
@@ -155,4 +157,15 @@ public function emailServices() {
155157
return $this->emailServiceFactory;
156158
}
157159

160+
/**
161+
* @return riskdetector\RiskDetectorServiceFactory
162+
* @since v5.3
163+
*/
164+
public function riskServices() {
165+
if ($this->riskServiceFactory==null) {
166+
$this->riskServiceFactory = new riskdetector\RiskDetectorServiceFactory($this->apiKey, $this->context, $this->baseUrl);
167+
}
168+
return $this->riskServiceFactory;
169+
}
170+
158171
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\riskdetector;
4+
5+
6+
/**
7+
* Class DataItem
8+
*
9+
* Possible values are are listed here.
10+
*
11+
*
12+
* NAME
13+
* The person's name (given name, surname, business name ...).
14+
*
15+
*
16+
* ADDRESS
17+
* The person's address (domicile, delivery address, ...).
18+
*
19+
*
20+
* AGE
21+
* for natural people it's the birth date
22+
* for legal people it's the founding time.
23+
*
24+
*
25+
* EMAIL
26+
* An email address.
27+
*
28+
*
29+
* TEL
30+
* Includes telephone numbers, fax numbers, mobile phone numbers etc.
31+
*
32+
*/
33+
final class DataItem {
34+
35+
/**
36+
* @var string $value
37+
*/
38+
private $value = null;
39+
40+
public function __construct($value) {
41+
if ($value!=='NAME' && $value!=='ADDRESS' && $value!=='AGE' && $value!=='EMAIL' && $value!=='TEL') {
42+
throw new \Exception('Invalid value for RiskType: '.$value.'!');
43+
}
44+
$this->value = $value;
45+
}
46+
47+
48+
49+
public function __toString() {
50+
return $this->value;
51+
}
52+
53+
}
54+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\riskdetector;
4+
5+
require_once(__DIR__ . '/DataItem.php');
6+
require_once(__DIR__ . '/DisguiseRiskType.php');
7+
require_once(__DIR__ . '/FakeRiskType.php');
8+
9+
/**
10+
* One detected risk within a RiskDetectorResult.
11+
* There can be 0-n of such risks in one result.
12+
*
13+
* @since v5.3
14+
*/
15+
class DetectedRisk {
16+
17+
/**
18+
* @var DataItem $dataItem
19+
*/
20+
private $dataItem = null;
21+
22+
/**
23+
* @var RiskType $riskType
24+
*/
25+
private $riskType = null;
26+
27+
/**
28+
* @var double $riskScore
29+
*/
30+
private $riskScore = null;
31+
32+
/**
33+
* @var string $reason
34+
*/
35+
private $reason = null;
36+
37+
38+
/**
39+
*/
40+
public function __construct($dataItem, $riskType, $riskScore, $reason) {
41+
if ($riskScore <= 0 || $riskScore > 1) throw new \Exception("Risk score is out of range (0,1]: ".$riskScore."!");
42+
$this->dataItem = $dataItem;
43+
$this->riskType = $riskType;
44+
$this->riskScore = $riskScore;
45+
$this->reason = $reason;
46+
}
47+
48+
49+
/**
50+
* @return DataItem
51+
*/
52+
public function getDataItem() {
53+
return $this->dataItem;
54+
}
55+
56+
/**
57+
* @return RiskType
58+
*/
59+
public function getRiskType() {
60+
return $this->riskType;
61+
}
62+
63+
/**
64+
* @return float range (0,1] the higher the worse.
65+
*/
66+
public function getRiskScore() {
67+
return $this->riskScore;
68+
}
69+
70+
/**
71+
* A one sentence text reason intended for the human that explains the risk.
72+
*/
73+
public function getReason() {
74+
return $this->reason;
75+
}
76+
77+
}
78+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\riskdetector;
4+
5+
require_once(__DIR__ . '/RiskType.php');
6+
7+
/**
8+
* Classification of disguise risks.
9+
*
10+
* <p>Such mangled input is used to circumvent machine processing.</p>
11+
*
12+
* <p>Humans can still understand these modified values, but machines can't unless they detect the patterns and clean
13+
* the input.</p>
14+
*
15+
* Possible values are are listed here.
16+
*
17+
*
18+
* PADDING
19+
* Padding is adding content to the left/right of a value.
20+
* Example: XXXJohnXXX
21+
*
22+
*
23+
* STUTTER_TYPING
24+
* Example: Petttttttttterson
25+
*
26+
*
27+
* SPACED_TYPING
28+
* Example: P e t e r M i l l e r
29+
*
30+
*
31+
* OTHER
32+
* Everything that does not fit into any of the other categories.
33+
* Individual categories may be created in the future.
34+
* Currently here goes:
35+
* - Leetspeak (using numbers instead of letters): l33t spe4k
36+
* - Crossing fields (moving a part into the next field): ["Danie", "lJohnson"]
37+
* This often happens unintentionally.
38+
* - Writing out numbers where digits are expected, for example in house numbers.
39+
* For example "twentyseven" instead of "27".
40+
* - Using visually identical or similar letters with different Unicode values.
41+
* Mixing scripts: For example mixing the Cyrillic with the Latin alphabet. Cyrillic has visually identical letters.
42+
* Same script: For example using the lower case L for an upper case i (l vs I) and vice versa, using a zero 0 for an oh O.
43+
*
44+
*/
45+
final class DisguiseRiskType extends RiskType {
46+
47+
/**
48+
* @var string $value
49+
*/
50+
private $value = null;
51+
52+
public function __construct($value) {
53+
if ($value!=='PADDING' && $value!=='STUTTER_TYPING' && $value!=='SPACED_TYPING' && $value!=='OTHER') {
54+
throw new \Exception('Invalid value for RiskType: '.$value.'!');
55+
}
56+
$this->value = $value;
57+
}
58+
59+
60+
61+
public function __toString() {
62+
return $this->value;
63+
}
64+
65+
}
66+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace org\nameapi\client\services\riskdetector;
4+
5+
require_once(__DIR__ . '/RiskType.php');
6+
7+
/**
8+
* Classification of risks.
9+
*
10+
* <p>In some situations the exact classification is difficult.
11+
* For example a person's name may be from fiction, but also be famous at the same time.</p>
12+
*
13+
*
14+
* Possible values are are listed here.
15+
*
16+
*
17+
* RANDOM_TYPING
18+
* This kind of input is often used to quickly pass mandatory fields in a form.
19+
* Example: "asdf asdf".
20+
*
21+
*
22+
* PLACEHOLDER
23+
* Examples:
24+
* For person name: "John Doe".
25+
* For person title: Example: "King Peter"
26+
* The given name field doesn't contain a given name, but has at least a title.
27+
* It may, in addition, contain a salutation.
28+
* For salutation: Example: "Mr. Smith" (Mr. in the given name field).
29+
* The given name field doesn't contain a given name, but has a salutation.
30+
* There is no title in it, otherwise PLACEHOLDER_TITLE would be used.
31+
* For place name: "Anytown"
32+
*
33+
*
34+
* FICTIONAL
35+
* Examples:
36+
* For natural person: "James Bond".
37+
* For legal person: ACME (American Company Making Everything)
38+
* For place: "Atlantis", "Entenhausen"
39+
*
40+
*
41+
* FAMOUS
42+
* Examples:
43+
* For natural person: "Barak Obama".
44+
*
45+
*
46+
* HUMOROUS
47+
* For natural person: "Sandy Beach".
48+
* Place example: "Timbuckthree"
49+
*
50+
*
51+
* INVALID
52+
* This includes multiple types of invalid form input.
53+
* Refusing input:
54+
* Example: "None of your business"
55+
* Placeholder nouns: "Someone", "Somebody else", "Somewhere", "Nowhere"
56+
* Repeating the form fields:
57+
* Example for person name: "firstname lastname"
58+
* Examples for street: "Street"
59+
* Vulgar language, swearing
60+
* Examples: "fuck off"
61+
*
62+
*
63+
* STRING_SIMILARITY
64+
* The given name and surname field are equal or almost equal, or match a certain pattern.
65+
* Example: "John" / "John"
66+
* The risk score is culture adjusted. In some cultures such names do exist, however, a risk is still raised.
67+
*
68+
*
69+
* OTHER
70+
* Everything that does not fit into any of the other categories.
71+
*
72+
*/
73+
final class FakeRiskType extends RiskType {
74+
75+
/**
76+
* @var string $value
77+
*/
78+
private $value = null;
79+
80+
public function __construct($value) {
81+
if ($value!=='RANDOM_TYPING' && $value!=='PLACEHOLDER' && $value!=='FICTIONAL' && $value!=='FAMOUS' && $value!=='HUMOROUS' && $value!=='INVALID' && $value!=='OTHER') {
82+
throw new \Exception('Invalid value for RiskType: '.$value.'!');
83+
}
84+
$this->value = $value;
85+
}
86+
87+
88+
89+
public function __toString() {
90+
return $this->value;
91+
}
92+
93+
}
94+

0 commit comments

Comments
 (0)