Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 13 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,33 @@ PHP Client for the NameAPI Web Service at http://www.nameapi.org/

All you need to send requests is your own api key, get it from nameapi.org.


## Library setup

The recommended way is to use Composer. The project at https://github.com/optimaize/nameapi-client-php-example-composer
The recommended way is to use Composer. The project at https://github.com/optimaize/nameapi-client-php-example-composer
shows how that's done.

You can download the source code and make it available to your code. Or you can check it out directly
from this GitHub project. Currently there is no phar available.
You can download the source code and make it available to your code. Or you can check it out directly from this GitHub project. Currently there is no phar available.

The only requirement is that the php_curl extension is enabled.


## Functional tests

Functional tests that demonstrate how the services work, and that they work, are in
https://github.com/optimaize/nameapi-client-php-functionaltests you can look at the code, and you can
even run those tests using your api key and PHPUnit.

Functional tests that demonstrate how the services work, and that they work, are in
https://github.com/optimaize/nameapi-client-php-functionaltests you can look at the code, and you can even run those tests using your api key and PHPUnit.

## Setup code

At first you need one single include, the one to the nameapi service factory:

```php
require_once('your/path/to/org/nameapi/client/services/ServiceFactory.php');
require_once('your/path/to/Org/NameApi/Client/Services/ServiceFactory.php');
```

Then you need a Context that explains a bit your working environment, something like:

```php
use org\nameapi\ontology\input\context\Context;
use org\nameapi\ontology\input\context\Priority;
use Org\NameApi\Ontology\input\Context\Context;
use Org\NameApi\Ontology\input\Context\Priority;
$context = Context::builder()
->place('US')
->priority(Priority::REALTIME)
Expand All @@ -49,7 +44,6 @@ Then you can already create the service factory which gives you access to all na
$serviceFactory = new ServiceFactory('your-api-key', $context);
```


## Send a ping

This code sends a simple ping to nameapi to test the connection:
Expand All @@ -61,39 +55,28 @@ $pong = $ping->ping();

If the response is 'pong' then all is fine and you can move on to the real goodies.


## Input / Output

All input objects come with builders or nicely documented setters.
The result objects returned by the services all have fully documented getters.
Many input arguments are optional - that means you can start simple, and add more as you need.

Behind the scenes this service api uses REST. But luckily you don't need to worry about any
of the interface detail, you can just use the provided classes.
All input objects come with builders or nicely documented setters. The result objects returned by the services all have fully documented getters. Many input arguments are optional - that means you can start simple, and add more as you need.

Behind the scenes this service api uses REST. But luckily you don't need to worry about any of the interface detail, you can just use the provided classes.

#### Person input object

Most services accept a 'Person' as input. This person contains a name, and optionally
more data such as gender, birth date etc.
The name can be just a single "full name" string, or it can be composed of multiple
fields like given name, middle name, surname.
This standardized api makes it simple to use different services in a consistent way,
and is very convenient in accepting the data however you have it at hands.
Most services accept a 'Person' as input. This person contains a name, and optionally more data such as gender, birth date etc. The name can be just a single "full name" string, or it can be composed of multiple fields like given name, middle name, surname. This standardized api makes it simple to use different services in a consistent way, and is very convenient in accepting the data however you have it at hands.

Creating a simple person looks something like this:

```php
use org\nameapi\ontology\input\entities\person\NaturalInputPerson;
use org\nameapi\ontology\input\entities\person\name\InputPersonName;
use Org\NameApi\Ontology\input\Entities\Person\NaturalInputPerson;
use Org\NameApi\Ontology\input\Entities\Person\Name\InputPersonName;
$inputPerson = NaturalInputPerson::builder()
->name(InputPersonName::westernBuilder()
->fullname( "John F. Kennedy" )
->build())
->build();
```


## Name Parser

Name parsing is the process of splitting a full name into its components.
Expand All @@ -106,7 +89,6 @@ $parseResult = $personNameParser->parse($inputPerson);
var_dump($parseResult);
```


## Name Genderizer

Name genderizing is the process of identifying the gender based on a person's name.
Expand All @@ -119,7 +101,6 @@ $personGenderResult = $personGenderizer->assess($inputPerson);
echo $personGenderResult->getGender()->toString(); //will print 'MALE'
```


## Name Matcher

The Name Matcher compares names and name pairs to discover whether the people could possibly be one and the same person.
Expand All @@ -142,7 +123,6 @@ $personMatcherResult = $personMatcher->match($inputPerson1, $inputPerson2);
echo $personMatcherResult->getPersonMatchType()->toString(); //will print 'MATCHING'
```


## Name Formatter

The Name Formatter displays personal names in the desired form. This includes the order as well as upper and lower case writing.
Expand All @@ -158,7 +138,6 @@ $formatterResult = $personNameFormatter->format($inputPerson, new FormatterPrope
echo $formatterResult->getFormatted(); //will print 'John Kennedy'
```


## Email Name Parser

The Email Name Parser extracts names out of email addresses.
Expand All @@ -169,8 +148,6 @@ $result = $emailNameParser->parse("john.doe@example.com");
echo $result;
```



## Disposable Email Address Detector

The DEA-Detector checks email addresses against a list of known "trash domains" such as mailinator.com.
Expand All @@ -181,11 +158,9 @@ $result = $deaDetector->isDisposable("abcdefgh@10minutemail.com");
echo $result->getDisposable()->toString()); //will print 'YES'
```


## Risk Detector

The Risk-Detector checks all data in the person input, including the name, address, birthdate,
email address and phone number for fake and suspicious data.
The Risk-Detector checks all data in the person input, including the name, address, birthdate, email address and phone number for fake and suspicious data.

```php
$riskDetector = $serviceFactory->riskServices()->personRiskDetector();
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "optimaize/nameapi-client-php",
"description": "PHP Client for the NameAPI Web Service",
"version": "5.3.0-rc4",
"homepage": "https://github.com/optimaize/nameapi-client-php",
"license": "LGPL-3.0",
"require": {
Expand All @@ -11,8 +10,15 @@
},
"autoload": {
"psr-4": {
"org\\": "src/org"
"Org\\NameApi\\": "src"
}
},
"minimum-stability": "dev"
}
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^9"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace org\nameapi\client\fault;
namespace Org\NameApi\Client\Fault;

/**
* Tells who's likely to blame when exceptions occur.
Expand All @@ -9,29 +9,35 @@
*
* <p>Possible values are: CLIENT, SERVER.</p>
*/
final class Blame {
final class Blame
{

/**
* @var string $value
*/
private $value = null;

public function __construct($value) {
if ($value!='CLIENT' && $value!='SERVER') {
throw new \Exception('Invalid value for Blame: '.$value.'!');
public function __construct($value)
{
if ($value != 'CLIENT' && $value != 'SERVER') {
throw new \Exception('Invalid value for Blame: ' . $value . '!');
}
$this->value = $value;
}


public function __toString() {
public function __toString()
{
return $this->value;
}

public function isClient() {
public function isClient()
{
return $this->value === 'CLIENT';
}
public function isServer() {

public function isServer()
{
return $this->value === 'SERVER';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<?php

namespace org\nameapi\client\fault;

require_once(__DIR__.'/Blame.php');
require_once(__DIR__.'/FaultInfo.php');
require_once(__DIR__.'/FaultInfoUnmarshaller.php');
require_once(__DIR__.'/Retry.php');
require_once(__DIR__.'/RetryType.php');
require_once(__DIR__.'/ServiceException.php');
namespace Org\NameApi\Client\Fault;

/**
* An object containing fault information that is used within a {@link ServiceException}.
Expand Down Expand Up @@ -72,7 +65,7 @@ class FaultInfo
* @param Retry $retrySameLocation
* @param Retry $retryOtherLocations
*/
public function __construct($faultCause, $blame, $message, $applicationErrorCode=null, $incidentId=null, Retry $retrySameLocation=null, Retry $retryOtherLocations=null)
public function __construct($faultCause, $blame, $message, $applicationErrorCode = null, $incidentId = null, Retry $retrySameLocation = null, Retry $retryOtherLocations = null)
{
$this->faultCause = $faultCause;
$this->blame = $blame;
Expand Down Expand Up @@ -172,4 +165,4 @@ public function getRetryOtherLocations()
return $this->retryOtherLocations;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php

namespace org\nameapi\client\fault;

require_once(__DIR__.'/FaultInfo.php');
require_once(__DIR__.'/Blame.php');
require_once(__DIR__.'/Retry.php');
require_once(__DIR__.'/RetryType.php');

namespace Org\NameApi\Client\Fault;

/**
* Converts json to a FaultInfo.
Expand All @@ -15,13 +9,13 @@ class FaultInfoUnmarshaller
{



/**
* @param $data The json string
* @return FaultInfo
* @throws \Exception
*/
public static function unmarshallJsonString($data) {
public static function unmarshallJsonString($data)
{
try {
return FaultInfoUnmarshaller::unmarshallJsonObject(json_decode($data));
} catch (\Exception $e) {
Expand All @@ -35,24 +29,25 @@ public static function unmarshallJsonString($data) {
* @return FaultInfo
* @throws \Exception
*/
public static function unmarshallJsonObject($data) {
public static function unmarshallJsonObject($data)
{
try {
$retrySame = null;
if (isSet($data->retrySameLocation)) {
if (isset($data->retrySameLocation)) {
$retrySame = FaultInfoUnmarshaller::unmarshallRetry($data->retrySameLocation);
}

$retryOther = null;
if (isSet($data->retryOtherLocations)) {
if (isset($data->retryOtherLocations)) {
$retryOther = FaultInfoUnmarshaller::unmarshallRetry($data->retryOtherLocations);
}

return new FaultInfo(
$data->faultCause,
new Blame($data->blame),
$data->message,
(isSet($data->applicationErrorCode)) ? $data->applicationErrorCode : null, //this is optional
(isSet($data->incidentId)) ? $data->incidentId : null, //this is optional
(isset($data->applicationErrorCode)) ? $data->applicationErrorCode : null, //this is optional
(isset($data->incidentId)) ? $data->incidentId : null, //this is optional
$retrySame, $retryOther
);

Expand All @@ -72,8 +67,8 @@ private static function unmarshallRetry($data)
{
return new Retry(
new RetryType($data->retryType),
(isSet($data->retryInSeconds)) ? $data->retryInSeconds : null // this is optional
(isset($data->retryInSeconds)) ? $data->retryInSeconds : null // this is optional
);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<?php

namespace org\nameapi\client\fault;
namespace Org\NameApi\Client\Fault;

/**
* Tells if and when the service request can be tried again in case of a failure.
*/
class Retry
{

public static function no() {
return new Retry(new RetryType('NO'), null);
}

/**
* @var RetryType
*/
private $retryType;

/**
* @var int
*/
Expand All @@ -27,12 +22,17 @@ public static function no() {
* @param RetryType $retryType
* @param int $retryInSeconds
*/
public function __construct(RetryType $retryType, $retryInSeconds=null)
public function __construct(RetryType $retryType, $retryInSeconds = null)
{
$this->retryType = $retryType;
$this->retryInSeconds = $retryInSeconds;
}

public static function no()
{
return new Retry(new RetryType('NO'), null);
}

/**
* @return RetryType not null
*/
Expand All @@ -53,4 +53,4 @@ public function getRetryInSeconds()
}


}
}
Loading