├── LICENSE ├── README.md ├── composer.json ├── src └── org │ └── nameapi │ ├── client │ ├── fault │ │ ├── Blame.php │ │ ├── FaultInfo.php │ │ ├── FaultInfoUnmarshaller.php │ │ ├── Retry.php │ │ ├── RetryType.php │ │ └── ServiceException.php │ ├── http │ │ ├── HttpResponseData.php │ │ ├── RestHttpClient.php │ │ └── RestHttpClientConfig.php │ └── services │ │ ├── BaseService.php │ │ ├── Host.php │ │ ├── ServiceFactory.php │ │ ├── development │ │ ├── DevelopmentServiceFactory.php │ │ └── exceptionthrower │ │ │ └── ExceptionThrowerService.php │ │ ├── email │ │ ├── EmailServiceFactory.php │ │ ├── disposableemailaddressdetector │ │ │ ├── DisposableEmailAddressDetectorResult.php │ │ │ ├── DisposableEmailAddressDetectorService.php │ │ │ └── Maybe.php │ │ └── emailnameparser │ │ │ ├── EmailAddressNameType.php │ │ │ ├── EmailAddressParsingResultType.php │ │ │ ├── EmailNameParserMatch.php │ │ │ ├── EmailNameParserResult.php │ │ │ ├── EmailNameParserService.php │ │ │ └── NameFromEmailAddress.php │ │ ├── formatter │ │ ├── FormatterProperties.php │ │ ├── FormatterResult.php │ │ ├── FormatterServiceFactory.php │ │ ├── namefieldformatter │ │ │ └── NameFieldFormatterService.php │ │ └── personnameformatter │ │ │ └── PersonNameFormatterService.php │ │ ├── genderizer │ │ ├── GenderizerServiceFactory.php │ │ └── persongenderizer │ │ │ ├── PersonGenderResult.php │ │ │ └── PersonGenderizerService.php │ │ ├── matcher │ │ ├── AgeMatchType.php │ │ ├── AgeMatcherResult.php │ │ ├── GenderMatchType.php │ │ ├── GenderMatcherResult.php │ │ ├── MatcherServiceFactory.php │ │ ├── PersonNameMatchType.php │ │ ├── PersonNameMatcherResult.php │ │ └── personmatcher │ │ │ ├── PersonMatchComposition.php │ │ │ ├── PersonMatchType.php │ │ │ ├── PersonMatcherResult.php │ │ │ └── PersonMatcherService.php │ │ ├── parser │ │ ├── OutputPersonName.php │ │ ├── OutputTermType.php │ │ ├── ParserServiceFactory.php │ │ ├── Term.php │ │ └── personnameparser │ │ │ ├── DisputeType.php │ │ │ ├── ParsedPerson.php │ │ │ ├── ParsedPersonMatch.php │ │ │ ├── ParserDispute.php │ │ │ ├── PersonNameParserResult.php │ │ │ └── PersonNameParserService.php │ │ ├── riskdetector │ │ ├── DataItem.php │ │ ├── DetectedRisk.php │ │ ├── DisguiseRiskType.php │ │ ├── FakeRiskType.php │ │ ├── RiskDetectorResult.php │ │ ├── RiskDetectorServiceFactory.php │ │ ├── RiskType.php │ │ └── person │ │ │ └── PersonRiskDetectorService.php │ │ └── system │ │ ├── SystemServiceFactory.php │ │ └── ping │ │ └── PingService.php │ └── ontology │ └── input │ ├── context │ ├── Context.php │ ├── ContextBuilder.php │ ├── Priority.php │ └── TextCase.php │ └── entities │ ├── address │ ├── AddressRelation.php │ ├── AddressUsage.php │ ├── InputAddress.php │ ├── PlaceInfo.php │ ├── StreetInfo.php │ ├── StructuredAddress.php │ ├── StructuredAddressBuilder.php │ ├── StructuredPlaceInfo.php │ ├── StructuredPlaceInfoBuilder.php │ ├── StructuredStreetInfo.php │ ├── StructuredStreetInfoBuilder.php │ └── UseForAllAddressRelation.php │ ├── contact │ ├── EmailAddress.php │ ├── EmailAddressFactory.php │ ├── TelNumber.php │ └── TelNumberFactory.php │ └── person │ ├── MaritalStatus.php │ ├── NaturalInputPerson.php │ ├── NaturalInputPersonBuilder.php │ ├── PersonRole.php │ ├── PersonType.php │ ├── age │ ├── AgeInfo.php │ ├── AgeInfoFactory.php │ ├── BirthDate.php │ ├── BirthYear.php │ ├── BirthYearRange.php │ └── YearRange.php │ ├── gender │ ├── ComputedPersonGender.php │ └── StoragePersonGender.php │ └── name │ ├── InputPersonName.php │ ├── NameField.php │ ├── builder │ ├── AmericanInputPersonNameBuilder.php │ ├── InputPersonNameBuilder.php │ └── WesternInputPersonNameBuilder.php │ └── types │ ├── AmericanNameFieldType.php │ └── CommonNameFieldType.php └── tests ├── functional ├── DisposableEmailAddressDetectorServiceTest.php ├── PersonNameMatcherServiceTest.php ├── PersonNameParserServiceTest.php ├── PersonRiskDetectorServiceTest.php └── PingServiceTest.php └── unit └── org └── nameapi ├── client └── fault │ └── FaultInfoUnmarshallerTest.php └── ontology └── input ├── context ├── ContextBuilderTest.php ├── PriorityTest.php └── TextCaseTest.php └── entities └── person └── age └── AgeInfoFactoryTest.php /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nameapi-client-php 2 | ================== 3 | 4 | PHP Client for the NameAPI Web Service at http://www.nameapi.org/ 5 | 6 | All you need to send requests is your own api key, get it from nameapi.org. 7 | 8 | 9 | ## Library setup 10 | 11 | The recommended way is to use Composer. The project at https://github.com/optimaize/nameapi-client-php-example-composer 12 | shows how that's done. 13 | 14 | You can download the source code and make it available to your code. Or you can check it out directly 15 | from this GitHub project. Currently there is no phar available. 16 | 17 | The only requirement is that the php_curl extension is enabled. 18 | 19 | 20 | ## Functional tests 21 | 22 | Functional tests that demonstrate how the services work, and that they work, are in 23 | https://github.com/optimaize/nameapi-client-php-functionaltests you can look at the code, and you can 24 | even run those tests using your api key and PHPUnit. 25 | 26 | 27 | ## Setup code 28 | 29 | At first you need one single include, the one to the nameapi service factory: 30 | 31 | ```php 32 | require_once('your/path/to/org/nameapi/client/services/ServiceFactory.php'); 33 | ``` 34 | 35 | Then you need a Context that explains a bit your working environment, something like: 36 | 37 | ```php 38 | use org\nameapi\ontology\input\context\Context; 39 | use org\nameapi\ontology\input\context\Priority; 40 | $context = Context::builder() 41 | ->place('US') 42 | ->priority(Priority::REALTIME()) 43 | ->build(); 44 | ``` 45 | 46 | Then you can already create the service factory which gives you access to all nameapi services: 47 | 48 | ```php 49 | $serviceFactory = new ServiceFactory('your-api-key', $context); 50 | ``` 51 | 52 | 53 | ## Send a ping 54 | 55 | This code sends a simple ping to nameapi to test the connection: 56 | 57 | ```php 58 | $ping = $serviceFactory->systemServices()->ping(); 59 | $pong = $ping->ping(); 60 | ``` 61 | 62 | If the response is 'pong' then all is fine and you can move on to the real goodies. 63 | 64 | 65 | ## Input / Output 66 | 67 | All input objects come with builders or nicely documented setters. 68 | The result objects returned by the services all have fully documented getters. 69 | Many input arguments are optional - that means you can start simple, and add more as you need. 70 | 71 | Behind the scenes this service api uses REST. But luckily you don't need to worry about any 72 | of the interface detail, you can just use the provided classes. 73 | 74 | 75 | #### Person input object 76 | 77 | Most services accept a 'Person' as input. This person contains a name, and optionally 78 | more data such as gender, birth date etc. 79 | The name can be just a single "full name" string, or it can be composed of multiple 80 | fields like given name, middle name, surname. 81 | This standardized api makes it simple to use different services in a consistent way, 82 | and is very convenient in accepting the data however you have it at hands. 83 | 84 | Creating a simple person looks something like this: 85 | 86 | ```php 87 | use org\nameapi\ontology\input\entities\person\NaturalInputPerson; 88 | use org\nameapi\ontology\input\entities\person\name\InputPersonName; 89 | $inputPerson = NaturalInputPerson::builder() 90 | ->name(InputPersonName::westernBuilder() 91 | ->fullname( "John F. Kennedy" ) 92 | ->build()) 93 | ->build(); 94 | ``` 95 | 96 | 97 | ## Name Parser 98 | 99 | Name parsing is the process of splitting a full name into its components. 100 | 101 | Using the $inputPerson created earlier: 102 | 103 | ```php 104 | $personNameParser = $this->makeServiceFactory()->parserServices()->personNameParser(); 105 | $parseResult = $personNameParser->parse($inputPerson); 106 | var_dump($parseResult); 107 | ``` 108 | 109 | 110 | ## Name Genderizer 111 | 112 | Name genderizing is the process of identifying the gender based on a person's name. 113 | 114 | Using the $inputPerson created earlier: 115 | 116 | ```php 117 | $personGenderizer = $serviceFactory->genderizerServices()->personGenderizer(); 118 | $personGenderResult = $personGenderizer->assess($inputPerson); 119 | echo $personGenderResult->getGender()->toString(); //will print 'MALE' 120 | ``` 121 | 122 | 123 | ## Name Matcher 124 | 125 | The Name Matcher compares names and name pairs to discover whether the people could possibly be one and the same person. 126 | 127 | This service takes 2 people as input: 128 | 129 | ```php 130 | $personMatcher = $serviceFactory->matcherServices()->personMatcher(); 131 | $inputPerson1 = NaturalInputPerson::builder() 132 | ->name(InputPersonName::westernBuilder() 133 | ->fullname( "John F. Kennedy" ) 134 | ->build()) 135 | ->build(); 136 | $inputPerson2 = NaturalInputPerson::builder() 137 | ->name(InputPersonName::westernBuilder() 138 | ->fullname( "Jack Kennedy" ) 139 | ->build()) 140 | ->build(); 141 | $personMatcherResult = $personMatcher->match($inputPerson1, $inputPerson2); 142 | echo $personMatcherResult->getPersonMatchType()->toString(); //will print 'MATCHING' 143 | ``` 144 | 145 | 146 | ## Name Formatter 147 | 148 | The Name Formatter displays personal names in the desired form. This includes the order as well as upper and lower case writing. 149 | 150 | ```php 151 | $personNameFormatter = $serviceFactory->formatterServices()->personNameFormatter(); 152 | $inputPerson = NaturalInputPerson::builder() 153 | ->name(InputPersonName::westernBuilder() 154 | ->fullname( "john kennedy" ) 155 | ->build()) 156 | ->build(); 157 | $formatterResult = $personNameFormatter->format($inputPerson, new FormatterProperties()); 158 | echo $formatterResult->getFormatted(); //will print 'John Kennedy' 159 | ``` 160 | 161 | 162 | ## Email Name Parser 163 | 164 | The Email Name Parser extracts names out of email addresses. 165 | 166 | ```php 167 | $emailNameParser = $serviceFactory->emailServices()->emailNameParser(); 168 | $result = $emailNameParser->parse("john.doe@example.com"); 169 | echo $result; 170 | ``` 171 | 172 | 173 | 174 | ## Disposable Email Address Detector 175 | 176 | The DEA-Detector checks email addresses against a list of known "trash domains" such as mailinator.com. 177 | 178 | ```php 179 | $deaDetector = $serviceFactory->emailServices()->disposableEmailAddressDetector(); 180 | $result = $deaDetector->isDisposable("abcdefgh@10minutemail.com"); 181 | echo $result->getDisposable()->toString()); //will print 'YES' 182 | ``` 183 | 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 | 196 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "optimaize/nameapi-client-php", 3 | "description": "PHP Client for the NameAPI Web Service", 4 | "version": "5.3.0-rc4", 5 | "homepage": "https://github.com/optimaize/nameapi-client-php", 6 | "license": "LGPL-3.0", 7 | "require": { 8 | "php": ">=5.3.0", 9 | "ext-curl": "*", 10 | "ext-json": "*" 11 | }, 12 | "autoload": { 13 | "psr-4": { 14 | "org\\": "src/org" 15 | } 16 | }, 17 | "minimum-stability": "dev" 18 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/Blame.php: -------------------------------------------------------------------------------- 1 | Based on this, decisions like auto-retry and logging can be made.

9 | * 10 | *

Possible values are: CLIENT, SERVER.

11 | */ 12 | final class Blame { 13 | 14 | /** 15 | * @var string $value 16 | */ 17 | private $value = null; 18 | 19 | public function __construct($value) { 20 | if ($value!='CLIENT' && $value!='SERVER') { 21 | throw new \Exception('Invalid value for Blame: '.$value.'!'); 22 | } 23 | $this->value = $value; 24 | } 25 | 26 | 27 | public function __toString() { 28 | return $this->value; 29 | } 30 | 31 | public function isClient() { 32 | return $this->value === 'CLIENT'; 33 | } 34 | public function isServer() { 35 | return $this->value === 'SERVER'; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/FaultInfo.php: -------------------------------------------------------------------------------- 1 | It contains: 16 | *
    17 | *
  1. {@link #blame} whether the server or the client is responsible
  2. 18 | *
  3. {@link #applicationErrorCode} the reason for machines to understand
  4. 19 | *
  5. {@link #message} the reason for humans to understand
  6. 20 | *
  7. {@link #faultCause} exception class (technical detail)
  8. 21 | *
  9. {@link #retrySameLocation} and {@link #retryOtherLocations} whether re-sending the same request makes sense
  10. 22 | *
  11. {@link #incidentId} if it escalated for analysis by the service provider
  12. 23 | *
24 | *

25 | */ 26 | class FaultInfo 27 | { 28 | 29 | /** 30 | * @var string 31 | */ 32 | private $faultCause; 33 | 34 | /** 35 | * @var Blame 36 | */ 37 | private $blame; 38 | 39 | /** 40 | * @var string 41 | */ 42 | private $message; 43 | 44 | /** 45 | * @var string 46 | */ 47 | private $applicationErrorCode; 48 | 49 | /** 50 | * @var string 51 | */ 52 | private $incidentId; 53 | 54 | 55 | /** 56 | * @var Retry 57 | */ 58 | private $retrySameLocation; 59 | 60 | /** 61 | * @var Retry 62 | */ 63 | private $retryOtherLocations; 64 | 65 | /** 66 | * FaultInfo constructor. 67 | * @param string $faultCause 68 | * @param $blame 69 | * @param string $message 70 | * @param string $applicationErrorCode 71 | * @param string $incidentId 72 | * @param Retry $retrySameLocation 73 | * @param Retry $retryOtherLocations 74 | */ 75 | public function __construct($faultCause, $blame, $message, $applicationErrorCode=null, $incidentId=null, Retry $retrySameLocation=null, Retry $retryOtherLocations=null) 76 | { 77 | $this->faultCause = $faultCause; 78 | $this->blame = $blame; 79 | $this->message = $message; 80 | $this->applicationErrorCode = $applicationErrorCode; 81 | $this->incidentId = $incidentId; 82 | $this->retrySameLocation = $retrySameLocation; 83 | $this->retryOtherLocations = $retryOtherLocations; 84 | } 85 | 86 | /** 87 | * Exception class type. 88 | * 89 | *

Examples: 90 | *

 91 |      * Caused by Client:
 92 |      *  - BadRequest
 93 |      *  - AccessDenied
 94 |      *  - Protocol
 95 |      * Caused by Server:
 96 |      *  - InternalServerError
 97 |      *  - ServiceTemporarilyUnavailable
 98 |      *  - BadResponse
 99 |      *
100 |      * @return string
101 |      */
102 |     public function getFaultCause()
103 |     {
104 |         return $this->faultCause;
105 |     }
106 | 
107 |     /**
108 |      * @return mixed
109 |      */
110 |     public function getBlame()
111 |     {
112 |         return $this->blame;
113 |     }
114 | 
115 |     /**
116 |      * Exception message for the human to understand the problem.
117 |      *
118 |      * 

119 | * It can be generic or specific. 120 | *

Examples:
121 |      *  - "Account expired"
122 |      *  - "Your account for user id 'foo' has expired on '2014-12-31'"
123 |      * 

124 | * 125 | *

It can be generic because either no detailed info is available, or because the system prefers to hide it 126 | * from the end user.

127 | * 128 | * @return string 129 | */ 130 | public function getMessage() 131 | { 132 | return $this->message; 133 | } 134 | 135 | /** 136 | * An error code for machines to understand the problem. 137 | * 138 | *

It can be generic or specific.

139 | * 140 | * @return string 141 | */ 142 | public function getApplicationErrorCode() 143 | { 144 | return $this->applicationErrorCode; 145 | } 146 | 147 | /** 148 | * Tells if a server error was logged/reported/escalated for analyzing by a system admin, qa or programmer. 149 | * 150 | * @return string 151 | */ 152 | public function getIncidentId() 153 | { 154 | return $this->incidentId; 155 | } 156 | 157 | /** 158 | * Tells if re-sending the same request that just failed with this error to the SAME NETWORK makes sense. 159 | * @return Retry, or null if unknown 160 | */ 161 | public function getRetrySameLocation() 162 | { 163 | return $this->retrySameLocation; 164 | } 165 | 166 | /** 167 | * Tells if re-sending the same request that just failed with this error to ANOTHER NETWORK makes sense. 168 | * @return Retry, or null if unknown 169 | */ 170 | public function getRetryOtherLocations() 171 | { 172 | return $this->retryOtherLocations; 173 | } 174 | 175 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/FaultInfoUnmarshaller.php: -------------------------------------------------------------------------------- 1 | retrySameLocation)) { 42 | $retrySame = FaultInfoUnmarshaller::unmarshallRetry($data->retrySameLocation); 43 | } 44 | 45 | $retryOther = null; 46 | if (isSet($data->retryOtherLocations)) { 47 | $retryOther = FaultInfoUnmarshaller::unmarshallRetry($data->retryOtherLocations); 48 | } 49 | 50 | return new FaultInfo( 51 | $data->faultCause, 52 | new Blame($data->blame), 53 | $data->message, 54 | (isSet($data->applicationErrorCode)) ? $data->applicationErrorCode : null, //this is optional 55 | (isSet($data->incidentId)) ? $data->incidentId : null, //this is optional 56 | $retrySame, $retryOther 57 | ); 58 | 59 | //httpStatusCode\":401,\"httpStatusMeaning\":\"Unauthorized\"}" 60 | 61 | } catch (\Exception $e) { 62 | throw new \Exception("Failed unmarshalling json object into FaultInfo!", 0, $e); 63 | } 64 | } 65 | 66 | /** 67 | * @param $data 68 | * @return Retry 69 | * @throws \Exception 70 | */ 71 | private static function unmarshallRetry($data) 72 | { 73 | return new Retry( 74 | new RetryType($data->retryType), 75 | (isSet($data->retryInSeconds)) ? $data->retryInSeconds : null // this is optional 76 | ); 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/Retry.php: -------------------------------------------------------------------------------- 1 | retryType = $retryType; 33 | $this->retryInSeconds = $retryInSeconds; 34 | } 35 | 36 | /** 37 | * @return RetryType not null 38 | */ 39 | public function getRetryType() 40 | { 41 | return $this->retryType; 42 | } 43 | 44 | /** 45 | * Tells when the service can be called again. 46 | * This is only available if retryType is LATER, and 47 | * can still be null if unknown. 48 | * @return int, or null if unspecified 49 | */ 50 | public function getRetryInSeconds() 51 | { 52 | return $this->retryInSeconds; 53 | } 54 | 55 | 56 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/RetryType.php: -------------------------------------------------------------------------------- 1 | Possible values are: NO, LATER, NOW.

9 | */ 10 | final class RetryType { 11 | 12 | /** 13 | * @var string $value 14 | */ 15 | private $value = null; 16 | 17 | public function __construct($value) { 18 | if ($value!='NO' && $value!='LATER' && $value!='NOW') { 19 | throw new \Exception('Invalid value for RetryType: '.$value.'!'); 20 | } 21 | $this->value = $value; 22 | } 23 | 24 | 25 | public function __toString() { 26 | return $this->value; 27 | } 28 | 29 | public function isNo() { 30 | return $this->value === 'NO'; 31 | } 32 | public function isLater() { 33 | return $this->value === 'LATER'; 34 | } 35 | public function isNow() { 36 | return $this->value === 'NOW'; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/org/nameapi/client/fault/ServiceException.php: -------------------------------------------------------------------------------- 1 | faultInfo = $faultInfo; 38 | $this->httpData = $httpData; 39 | } 40 | 41 | 42 | 43 | /** 44 | * @return FaultInfo or null if not available, for example because un-serializing failed 45 | */ 46 | public function getFaultInfo() 47 | { 48 | return $this->faultInfo; 49 | } 50 | 51 | /** 52 | * @return HttpResponseData or null if not available, for example because the error occurred before even hitting the web, 53 | * or because a timeout occurred. 54 | */ 55 | public function getHttpData() 56 | { 57 | return $this->httpData; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/org/nameapi/client/http/HttpResponseData.php: -------------------------------------------------------------------------------- 1 | url = $url; 40 | $this->responseCode = $responseCode; 41 | $this->responseBody = $responseBody; 42 | $this->responseHeaders = $responseHeaders; 43 | } 44 | 45 | 46 | /** 47 | * The remote address that was called. 48 | * @return string 49 | */ 50 | public function getUrl() 51 | { 52 | return $this->url; 53 | } 54 | 55 | /** 56 | * @return int 57 | */ 58 | public function getResponseCode() 59 | { 60 | return $this->responseCode; 61 | } 62 | 63 | /** 64 | * @return string 65 | */ 66 | public function getResponseBody() 67 | { 68 | return $this->responseBody; 69 | } 70 | 71 | /** 72 | * @return string 73 | */ 74 | public function getResponseHeaders() 75 | { 76 | return $this->responseHeaders; 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/http/RestHttpClient.php: -------------------------------------------------------------------------------- 1 | config = $config; 55 | } 56 | 57 | 58 | public function callApiGet($resourcePath, $queryParams, $headerParams) { 59 | return $this->callApi($resourcePath, 'GET', $queryParams, null, $headerParams); 60 | } 61 | public function callApiPost($resourcePath, $queryParams, $headerParams, $postData) { 62 | return $this->callApi($resourcePath, 'POST', $queryParams, $postData, $headerParams); 63 | } 64 | 65 | /** 66 | * Make the HTTP call (Sync) 67 | * @param string $resourcePath path to method endpoint 68 | * @param string $method method to call 69 | * @param array $queryParams parameters to be place in query URL 70 | * @param array $postData parameters to be placed in POST body 71 | * @param array $headerParams parameters to be place in request header 72 | * @throws ServiceException on a non 2xx response 73 | * @return mixed 74 | */ 75 | public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) { 76 | $headers = array(); 77 | $headers[] = "Accept: application/json"; 78 | $headers[] = "Content-Type: application/json"; 79 | 80 | if (!empty($headerParams)) { 81 | foreach ($headerParams as $key => $val) { 82 | $headers[] = "$key: $val"; 83 | } 84 | } 85 | 86 | if ($postData && is_object($postData) or is_array($postData)) { // json model 87 | $postData = json_encode($postData); 88 | } 89 | 90 | $url = $this->config->getBaseUrl() . $resourcePath; 91 | 92 | $curl = curl_init(); 93 | // set timeout, if needed 94 | if ($this->config->getCurlTimeout() != 0) { 95 | curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout()); 96 | } 97 | // return the result on success, rather than just TRUE 98 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 99 | 100 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 101 | 102 | // disable SSL verification, if needed 103 | if ($this->config->getSSLVerification() == false) { 104 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 105 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 106 | } 107 | 108 | if (is_null($queryParams)) $queryParams = array(); 109 | $queryParams['apiKey'] = $this->config->getApiKey(); 110 | $url = ($url . '?' . http_build_query($queryParams)); 111 | 112 | if ($method == self::$POST) { 113 | curl_setopt($curl, CURLOPT_POST, true); 114 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 115 | } else if ($method == self::$HEAD) { 116 | curl_setopt($curl, CURLOPT_NOBODY, true); 117 | } else if ($method == self::$OPTIONS) { 118 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS"); 119 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 120 | } else if ($method == self::$PATCH) { 121 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); 122 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 123 | } else if ($method == self::$PUT) { 124 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); 125 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 126 | } else if ($method == self::$DELETE) { 127 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 128 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 129 | } else if ($method != self::$GET) { 130 | $faultInfo = new FaultInfo( 131 | 'BadRequest', new Blame('CLIENT'), 132 | 'Method ' . $method . ' is not recognized.', 133 | 1100, null, 134 | Retry::no(), Retry::no() 135 | ); 136 | throw new ServiceException($faultInfo->getMessage(), $faultInfo, null); 137 | } 138 | if ($this->config->getDebug()) { 139 | error_log("[DEBUG] URL is $url", 3, $this->config->getDebugFile()); 140 | } 141 | curl_setopt($curl, CURLOPT_URL, $url); 142 | 143 | // Set user agent 144 | curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent()); 145 | 146 | // debugging for curl 147 | if ($this->config->getDebug()) { 148 | error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, $this->config->getDebugFile()); 149 | 150 | curl_setopt($curl, CURLOPT_VERBOSE, 1); 151 | //this doesn't work, i get: 152 | //"curl_setopt(): cannot represent a stream of type Output as a STDIO FILE*" 153 | // curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a')); 154 | } else { 155 | curl_setopt($curl, CURLOPT_VERBOSE, 0); 156 | } 157 | 158 | // obtain the HTTP response headers 159 | curl_setopt($curl, CURLOPT_HEADER, 1); 160 | 161 | // Make the request 162 | $response = curl_exec($curl); 163 | $http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 164 | $http_header = substr($response, 0, $http_header_size); 165 | $http_body = substr($response, $http_header_size); 166 | $response_info = curl_getinfo($curl); 167 | $httpResponseData = new HttpResponseData($url, $response_info['http_code'], $http_body, $http_header); 168 | 169 | // debug HTTP response body 170 | if ($this->config->getDebug()) { 171 | error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, $this->config->getDebugFile()); 172 | } 173 | 174 | // Handle the response 175 | if ($response_info['http_code'] == 0) { 176 | $faultInfo = new FaultInfo( 177 | 'NetworkTimeout', new Blame('SERVER'), 178 | "API call to $url timed out: ".serialize($response_info), 179 | null, null, 180 | Retry::no(), Retry::no() 181 | ); 182 | throw new ServiceException($faultInfo->getMessage(), $faultInfo, $httpResponseData); 183 | } else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) { 184 | $data = json_decode($http_body); 185 | if (json_last_error() > 0) { // if response is a string 186 | $data = $http_body; 187 | } 188 | } else { 189 | $data = json_decode($http_body); 190 | if (json_last_error() > 0) { // if response is a string 191 | $data = $http_body; 192 | } 193 | 194 | $faultInfo = null; 195 | $msg = null; 196 | try { 197 | $faultInfo = FaultInfoUnmarshaller::unmarshallJsonObject($data); 198 | $msg = $faultInfo->getMessage(); 199 | } catch (\Exception $e) { 200 | //TODO log. 201 | //I'm not throwing this because it would hide the original error. 202 | $msg = $e->getMessage(); 203 | } 204 | throw new ServiceException($msg, $faultInfo, $httpResponseData); 205 | } 206 | return array($data, $httpResponseData); 207 | } 208 | 209 | 210 | 211 | } 212 | -------------------------------------------------------------------------------- /src/org/nameapi/client/http/RestHttpClientConfig.php: -------------------------------------------------------------------------------- 1 | tempFolderPath = sys_get_temp_dir(); 62 | } 63 | 64 | /** 65 | * @return mixed 66 | */ 67 | public function getApiKey() 68 | { 69 | return $this->apiKey; 70 | } 71 | 72 | /** 73 | * @param mixed $apiKey 74 | */ 75 | public function setApiKey($apiKey) 76 | { 77 | $this->apiKey = $apiKey; 78 | } 79 | 80 | /** 81 | * @return mixed 82 | */ 83 | public function getBaseUrl() 84 | { 85 | return $this->baseUrl; 86 | } 87 | 88 | /** 89 | * @param mixed $baseUrl 90 | */ 91 | public function setBaseUrl($baseUrl) 92 | { 93 | $this->baseUrl = $baseUrl; 94 | } 95 | 96 | /** 97 | * @return string 98 | */ 99 | public function getUserAgent() 100 | { 101 | return $this->userAgent; 102 | } 103 | 104 | /** 105 | * @param string $userAgent 106 | */ 107 | public function setUserAgent($userAgent) 108 | { 109 | $this->userAgent = $userAgent; 110 | } 111 | 112 | /** 113 | * Sets the HTTP timeout value 114 | * 115 | * @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] 116 | * 117 | * @return RestHttpClient 118 | */ 119 | public function setCurlTimeout($seconds) 120 | { 121 | if (!is_numeric($seconds) || $seconds < 0) { 122 | throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.'); 123 | } 124 | 125 | $this->curlTimeout = $seconds; 126 | return $this; 127 | } 128 | 129 | /** 130 | * Gets the HTTP timeout value 131 | * 132 | * @return string HTTP timeout value 133 | */ 134 | public function getCurlTimeout() 135 | { 136 | return $this->curlTimeout; 137 | } 138 | 139 | /** 140 | * Sets debug flag 141 | * 142 | * @param bool $debug Debug flag 143 | * 144 | * @return RestHttpClientConfig 145 | */ 146 | public function setDebug($debug) 147 | { 148 | $this->debug = $debug; 149 | return $this; 150 | } 151 | 152 | /** 153 | * Gets the debug flag 154 | * 155 | * @return bool 156 | */ 157 | public function getDebug() 158 | { 159 | return $this->debug; 160 | } 161 | 162 | /** 163 | * Sets the debug file 164 | * 165 | * @param string $debugFile Debug file 166 | * 167 | * @return RestHttpClientConfig 168 | */ 169 | public function setDebugFile($debugFile) 170 | { 171 | $this->debugFile = $debugFile; 172 | return $this; 173 | } 174 | 175 | /** 176 | * Gets the debug file 177 | * 178 | * @return string 179 | */ 180 | public function getDebugFile() 181 | { 182 | return $this->debugFile; 183 | } 184 | 185 | /** 186 | * Sets the temp folder path 187 | * 188 | * @param string $tempFolderPath Temp folder path 189 | * 190 | * @return RestHttpClientConfig 191 | */ 192 | public function setTempFolderPath($tempFolderPath) 193 | { 194 | $this->tempFolderPath = $tempFolderPath; 195 | return $this; 196 | } 197 | 198 | /** 199 | * Gets the temp folder path 200 | * 201 | * @return string Temp folder path 202 | */ 203 | public function getTempFolderPath() 204 | { 205 | return $this->tempFolderPath; 206 | } 207 | 208 | /** 209 | * Sets if SSL verification should be enabled or disabled 210 | * 211 | * @param boolean $sslVerification True if the certificate should be validated, false otherwise 212 | * 213 | * @return RestHttpClientConfig 214 | */ 215 | public function setSSLVerification($sslVerification) 216 | { 217 | $this->sslVerification = $sslVerification; 218 | return $this; 219 | } 220 | 221 | /** 222 | * Gets if SSL verification should be enabled or disabled 223 | * 224 | * @return boolean True if the certificate should be validated, false otherwise 225 | */ 226 | public function getSSLVerification() 227 | { 228 | return $this->sslVerification; 229 | } 230 | 231 | /** 232 | * Gets the default configuration instance 233 | * 234 | * @return RestHttpClientConfig 235 | */ 236 | public static function getDefaultConfiguration() 237 | { 238 | if (self::$_defaultConfiguration == null) { 239 | self::$_defaultConfiguration = new RestHttpClientConfig(); 240 | } 241 | 242 | return self::$_defaultConfiguration; 243 | } 244 | 245 | /** 246 | * Sets the detault configuration instance 247 | * 248 | * @param RestHttpClientConfig $config An instance of the Configuration Object 249 | * 250 | * @return void 251 | */ 252 | public static function setDefaultConfiguration(RestHttpClientConfig $config) 253 | { 254 | self::$_defaultConfiguration = $config; 255 | } 256 | 257 | /** 258 | * Gets the essential information for debugging 259 | * 260 | * @return string The report for debugging 261 | */ 262 | public static function toDebugReport() 263 | { 264 | $report = "RestHttpClientConfig Debug Report:\n"; 265 | $report .= " OS: ".php_uname()."\n"; 266 | $report .= " PHP Version: ".phpversion()."\n"; 267 | $report .= " Swagger Spec Version: v5.3\n"; 268 | $report .= " SDK Package Version: 1.0.0\n"; 269 | $report .= " Temp Folder Path: ".self::getDefaultConfiguration()->getTempFolderPath()."\n"; 270 | 271 | return $report; 272 | } 273 | 274 | } 275 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/BaseService.php: -------------------------------------------------------------------------------- 1 | context = $context; 37 | $configuration = new RestHttpClientConfig(); 38 | $configuration->setApiKey($apiKey); 39 | $configuration->setBaseUrl($baseUrl); 40 | $this->restHttpClient = new RestHttpClient($configuration); 41 | } 42 | 43 | 44 | 45 | /** 46 | * @param $response 47 | * @param $httpResponseData 48 | * @return ServiceException 49 | */ 50 | protected function unmarshallingFailed($response, $httpResponseData) 51 | { 52 | $faultInfo = new FaultInfo( 53 | 'BadRequest', new Blame('CLIENT'), 54 | 'Server sent unexpected or unsupported response!', 55 | 1100, null, 56 | Retry::no(), Retry::no() 57 | ); 58 | return new ServiceException($faultInfo->getMessage(), $faultInfo, $httpResponseData); 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/Host.php: -------------------------------------------------------------------------------- 1 | protocol = $protocol; 47 | $this->hostName = $hostName; 48 | $this->portNumber = $portNumber; 49 | } 50 | 51 | /** 52 | * Returns something like 'http://api.nameapi.org' and omits the port if it's a default port (like 80 for http). 53 | */ 54 | public function toString() { 55 | $str = $this->protocol . '://' . $this->hostName; 56 | if ($this->protocol==='http' && $this->portNumber==80) { 57 | //don't add port 58 | } else if ($this->protocol==='https' && $this->portNumber==443) { 59 | //don't add port 60 | } else { 61 | $str .= ':'. $this->portNumber; 62 | } 63 | return $str; 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/ServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 77 | $this->context = $context; 78 | if ($host==null) { 79 | $this->host = Host::standard(); 80 | } else { 81 | $this->host = $host; 82 | } 83 | if ($apiVersion==null) { 84 | $this->apiVersion = '5.0'; 85 | } else { 86 | $this->apiVersion = $apiVersion; 87 | } 88 | $this->baseUrl = $this->host->toString() . '/'.$this->technology.'/v'.$this->apiVersion.'/'; 89 | } 90 | 91 | 92 | /** 93 | * @return development\DevelopmentServiceFactory 94 | */ 95 | public function developmentServices() { 96 | if ($this->developmentServiceFactory==null) { 97 | $this->developmentServiceFactory = new development\DevelopmentServiceFactory($this->apiKey, $this->context, $this->baseUrl); 98 | } 99 | return $this->developmentServiceFactory; 100 | } 101 | 102 | /** 103 | * @return system\SystemServiceFactory 104 | */ 105 | public function systemServices() { 106 | if ($this->systemServiceFactory==null) { 107 | $this->systemServiceFactory = new system\SystemServiceFactory($this->apiKey, $this->context, $this->baseUrl); 108 | } 109 | return $this->systemServiceFactory; 110 | } 111 | 112 | /** 113 | * @return parser\ParserServiceFactory 114 | */ 115 | public function parserServices() { 116 | if ($this->parserServiceFactory==null) { 117 | $this->parserServiceFactory = new parser\ParserServiceFactory($this->apiKey, $this->context, $this->baseUrl); 118 | } 119 | return $this->parserServiceFactory; 120 | } 121 | 122 | /** 123 | * @return genderizer\GenderizerServiceFactory 124 | */ 125 | public function genderizerServices() { 126 | if ($this->genderizerServiceFactory==null) { 127 | $this->genderizerServiceFactory = new genderizer\GenderizerServiceFactory($this->apiKey, $this->context, $this->baseUrl); 128 | } 129 | return $this->genderizerServiceFactory; 130 | } 131 | 132 | /** 133 | * @return matcher\MatcherServiceFactory 134 | */ 135 | public function matcherServices() { 136 | if ($this->matcherServiceFactory==null) { 137 | $this->matcherServiceFactory = new matcher\MatcherServiceFactory($this->apiKey, $this->context, $this->baseUrl); 138 | } 139 | return $this->matcherServiceFactory; 140 | } 141 | 142 | /** 143 | * @return formatter\FormatterServiceFactory 144 | */ 145 | public function formatterServices() { 146 | if ($this->formatterServiceFactory==null) { 147 | $this->formatterServiceFactory = new formatter\FormatterServiceFactory($this->apiKey, $this->context, $this->baseUrl); 148 | } 149 | return $this->formatterServiceFactory; 150 | } 151 | 152 | /** 153 | * @return email\EmailServiceFactory 154 | */ 155 | public function emailServices() { 156 | if ($this->emailServiceFactory==null) { 157 | $this->emailServiceFactory = new email\EmailServiceFactory($this->apiKey, $this->context, $this->baseUrl); 158 | } 159 | return $this->emailServiceFactory; 160 | } 161 | 162 | /** 163 | * @return riskdetector\RiskDetectorServiceFactory 164 | * @since v5.3 165 | */ 166 | public function riskServices() { 167 | if ($this->riskServiceFactory==null) { 168 | $this->riskServiceFactory = new riskdetector\RiskDetectorServiceFactory($this->apiKey, $this->context, $this->baseUrl); 169 | } 170 | return $this->riskServiceFactory; 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/development/DevelopmentServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 25 | $this->context = $context; 26 | $this->baseUrl = $baseUrl; 27 | } 28 | 29 | /** 30 | * @return ExceptionThrowerService 31 | * @since v5.0 32 | */ 33 | public function exceptionThrower() { 34 | if ($this->exceptionThrowerService==null) { 35 | $this->exceptionThrowerService = new ExceptionThrowerService($this->apiKey, $this->context, $this->baseUrl); 36 | } 37 | return $this->exceptionThrowerService; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/development/exceptionthrower/ExceptionThrowerService.php: -------------------------------------------------------------------------------- 1 | The use is for development only; to see what happens when the server throws exceptions, and how they arrive 15 | * at your end.

16 | * 17 | * HOW TO USE: 18 | * $exceptionThrower = $myServiceFactory->developmentServices()->exceptionThrowerService(); 19 | * $exceptionThrower = $ping->throwException(); 20 | */ 21 | class ExceptionThrowerService extends BaseService { 22 | 23 | 24 | private static $RESOURCE_PATH = "development/exceptionthrower"; 25 | 26 | public function __construct($apiKey, Context $context, $baseUrl) { 27 | parent::__construct($apiKey, $context, $baseUrl); 28 | } 29 | 30 | /** 31 | * @param string $exceptionType One of 'AccessDeniedNoSuchAccount', 'InvalidInput', 'InternalServerError' 32 | * @param int $exceptionChance 0-100 where 100 means always 33 | * @return string 'OK' in case the remote service does not throw. 34 | * @throws ServiceException 35 | */ 36 | public function throwException($exceptionType, $exceptionChance) { 37 | $queryParams = array( 38 | 'exceptionType' => $exceptionType, 39 | 'exceptionChance' => $exceptionChance, 40 | ); 41 | $headerParams = array(); 42 | 43 | list($response, $httpResponseData) = $this->restHttpClient->callApiGet( 44 | ExceptionThrowerService::$RESOURCE_PATH, 45 | $queryParams, $headerParams 46 | ); 47 | if ($response !== 'OK') { 48 | throw $this->unmarshallingFailed($response, $httpResponseData); 49 | } 50 | return $response; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/EmailServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 29 | $this->context = $context; 30 | $this->baseUrl = $baseUrl; 31 | } 32 | 33 | /** 34 | * @return DisposableEmailAddressDetectorService 35 | * @since v4.0 36 | */ 37 | public function disposableEmailAddressDetector() { 38 | if ($this->disposableEmailAddressDetector==null) { 39 | $this->disposableEmailAddressDetector = new DisposableEmailAddressDetectorService($this->apiKey, $this->context, $this->baseUrl); 40 | } 41 | return $this->disposableEmailAddressDetector; 42 | } 43 | 44 | /** 45 | * @return EmailNameParserService 46 | * @since v4.0 47 | */ 48 | public function emailNameParser() { 49 | if ($this->emailNameParser==null) { 50 | $this->emailNameParser = new EmailNameParserService($this->apiKey, $this->context, $this->baseUrl); 51 | } 52 | return $this->emailNameParser; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/disposableemailaddressdetector/DisposableEmailAddressDetectorResult.php: -------------------------------------------------------------------------------- 1 | disposable = $disposable; 18 | } 19 | 20 | /** 21 | * @return Maybe 22 | */ 23 | public function getDisposable() { 24 | return $this->disposable; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/disposableemailaddressdetector/DisposableEmailAddressDetectorService.php: -------------------------------------------------------------------------------- 1 | emailServices()->disposableEmailAddressDetector(); 18 | * $result = $deaDetector->isDisposable("abcdefgh@10minutemail.com"); 19 | * echo (string)$result->getDisposable()); //prints 'YES' 20 | * 21 | * @since v4.0 22 | */ 23 | class DisposableEmailAddressDetectorService extends BaseService { 24 | 25 | private static $RESOURCE_PATH = "email/disposableemailaddressdetector"; 26 | 27 | public function __construct($apiKey, Context $context, $baseUrl) { 28 | parent::__construct($apiKey, $context, $baseUrl); 29 | } 30 | 31 | 32 | /** 33 | * @param string $emailAddress 34 | * @return DisposableEmailAddressDetectorResult 35 | * @throws ServiceException 36 | */ 37 | public function isDisposable($emailAddress) { 38 | $queryParams = array( 39 | 'emailAddress'=>$emailAddress 40 | ); 41 | $headerParams = array(); 42 | 43 | list($response, $httpResponseData) = $this->restHttpClient->callApiGet( 44 | DisposableEmailAddressDetectorService::$RESOURCE_PATH, 45 | $queryParams, $headerParams 46 | ); 47 | try { 48 | return new DisposableEmailAddressDetectorResult(new Maybe($response->disposable)); 49 | } catch (\Exception $e) { 50 | throw $this->unmarshallingFailed($response, $httpResponseData); 51 | } 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/disposableemailaddressdetector/Maybe.php: -------------------------------------------------------------------------------- 1 | >>'.$value.'<<value = $value; 24 | } 25 | 26 | 27 | /** 28 | * @return bool 29 | */ 30 | public function isDisposable() { 31 | return $this->value === 'YES'; 32 | } 33 | 34 | public function __toString() { 35 | return $this->value; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/EmailAddressNameType.php: -------------------------------------------------------------------------------- 1 | Possible values are: 9 | * NAME 10 | * It's a regular name, like "Peter" or "Johnson". 11 | * INITIAL 12 | * It's an abbreviated name with an initial like "P". 13 | * The string does not end with a dot. 14 | *
15 | */ 16 | class EmailAddressNameType { 17 | 18 | /** 19 | * @var string $value 20 | */ 21 | private $value = null; 22 | 23 | public function __construct($value) { 24 | if ($value!=='NAME' 25 | && $value!=='INITIAL' 26 | ) { 27 | throw new \Exception('Invalid value for EmailAddressNameType: '.$value.'!'); 28 | } 29 | $this->value = $value; 30 | } 31 | 32 | 33 | public function __toString() { 34 | return $this->value; 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/EmailAddressParsingResultType.php: -------------------------------------------------------------------------------- 1 | Note that this answer is a guess, the 2 letters could also have another meaning 18 | * such as a short given name or surname, or something completely different.

19 | * 20 | * PERSON_NAME 21 | * The email address contains a person's name such as john.doe@example.com. 22 | * 23 | * PSEUDONYM 24 | * The email address uses a pseudonym as the user name such as maverick1986@example.com or happyhippo@example.com. 25 | * 26 | * NOT_A_NAME 27 | * There is no name in the address, for example x2000@example.com 28 | *

The address may be personal or non-personal, can't say (as in UNKNOWN) 29 | * but it is clear that no name can be found in it.

30 | * 31 | * UNKNOWN 32 | * The email address could not be classified and hence the service failed to extract a name. 33 | * 34 | * 35 | */ 36 | final class EmailAddressParsingResultType { 37 | 38 | /** 39 | * @var string $value 40 | */ 41 | private $value = null; 42 | 43 | public function __construct($value) { 44 | if ($value!=='FUNCTIONAL' 45 | && $value!=='INITIALS' 46 | && $value!=='PERSON_NAME' 47 | && $value!=='PSEUDONYM' 48 | && $value!=='NOT_A_NAME' 49 | && $value!=='UNKNOWN' 50 | ) { 51 | throw new \Exception('Invalid value for EmailAddressParsingResultType: '.$value.'!'); 52 | } 53 | $this->value = $value; 54 | } 55 | 56 | 57 | public function __toString() { 58 | return $this->value; 59 | } 60 | 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/EmailNameParserMatch.php: -------------------------------------------------------------------------------- 1 | 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 | public function __toString() { 61 | $ret = ''; 62 | if (count($this->givenNames) >0) { 63 | if (!empty($ret)) $ret .= ', '; 64 | $ret .= 'givenNames='. implode(", ",$this->givenNames); 65 | } 66 | if (count($this->surnames) >0) { 67 | if (!empty($ret)) $ret .= ', '; 68 | $ret .= 'surnames='. implode(", ",$this->surnames); 69 | } 70 | if (!empty($ret)) $ret .= ', '; 71 | $ret .= 'confidence='.$this->confidence; 72 | return '{'.$ret.'}'; 73 | } 74 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/EmailNameParserResult.php: -------------------------------------------------------------------------------- 1 | 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 | 45 | public function __toString() { 46 | $ret = 'Result{'; 47 | $ret .= 'type='.$this->resultType; 48 | if (count($this->matches) >0) { 49 | $ret .= ', matches='. implode(", ",$this->matches); 50 | } 51 | return $ret.'}'; 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/EmailNameParserService.php: -------------------------------------------------------------------------------- 1 | emailServices()->emailNameParser(); 17 | * $result = $emailNameParser->parse("john.doe@example.com"); 18 | * 19 | * @since v4.0 20 | */ 21 | class EmailNameParserService extends BaseService { 22 | 23 | private static $RESOURCE_PATH = "email/emailnameparser"; 24 | 25 | public function __construct($apiKey, Context $context, $baseUrl) { 26 | parent::__construct($apiKey, $context, $baseUrl); 27 | } 28 | 29 | 30 | /** 31 | * @param string $emailAddress 32 | * @return EmailNameParserResult 33 | * @throws ServiceException 34 | */ 35 | public function parse($emailAddress) { 36 | $queryParams = array( 37 | 'emailAddress'=>$emailAddress 38 | ); 39 | $headerParams = array(); 40 | 41 | list($response, $httpResponseData) = $this->restHttpClient->callApiGet( 42 | EmailNameParserService::$RESOURCE_PATH, 43 | $queryParams, $headerParams 44 | ); 45 | try { 46 | $matches = array(); 47 | if (isSet($response->nameMatches)) { 48 | foreach ($response->nameMatches as $match) { 49 | $givenNames = array(); 50 | $surnames = array(); 51 | if (isSet($match->givenNames)) { 52 | foreach ($match->givenNames as $name) { 53 | array_push($givenNames, new NameFromEmailAddress($name->name, new EmailAddressNameType($name->nameType))); 54 | } 55 | } 56 | if (isSet($match->surnames)) { 57 | foreach ($match->surnames as $name) { 58 | array_push($surnames, new NameFromEmailAddress($name->name, new EmailAddressNameType($name->nameType))); 59 | } 60 | } 61 | array_push($matches, new EmailNameParserMatch($givenNames, $surnames, $match->confidence)); 62 | } 63 | } 64 | return new EmailNameParserResult( 65 | new EmailAddressParsingResultType($response->resultType), 66 | $matches 67 | ); 68 | } catch (\Exception $e) { 69 | throw $this->unmarshallingFailed($response, $httpResponseData); 70 | } 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/email/emailnameparser/NameFromEmailAddress.php: -------------------------------------------------------------------------------- 1 | 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 | 44 | public function __toString() { 45 | $ret = $this->name; 46 | if ((string)$this->nameType != 'NAME') { 47 | $ret .= ' (type=' . (string)$this->nameType .')'; 48 | } 49 | return $ret; 50 | } 51 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/formatter/FormatterProperties.php: -------------------------------------------------------------------------------- 1 | formatUnknownInput = $bool; 32 | return $this; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/formatter/FormatterResult.php: -------------------------------------------------------------------------------- 1 | formatted = $formatted; 22 | $this->unknown = $unknown; 23 | } 24 | 25 | /** 26 | * @return string The formatted string, which may be the same as the input name/string if that was in the desired form already. 27 | */ 28 | public function getFormatted() { 29 | return $this->formatted; 30 | } 31 | 32 | 33 | /** 34 | * @return boolean true if the server was unable to understand the input, and thus the formatted output is a guess. 35 | * The default behavior (see input settings) is for the server to throw on unknown input. 36 | */ 37 | public function getUnknown() { 38 | return $this->unknown; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/formatter/FormatterServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 28 | $this->context = $context; 29 | $this->baseUrl = $baseUrl; 30 | } 31 | 32 | /** 33 | * @return PersonNameFormatterService 34 | * @since v4.0 35 | */ 36 | public function personNameFormatter() { 37 | if ($this->personNameFormatterService==null) { 38 | $this->personNameFormatterService = new PersonNameFormatterService($this->apiKey, $this->context, $this->baseUrl); 39 | } 40 | return $this->personNameFormatterService; 41 | } 42 | 43 | /** 44 | * @return NameFieldFormatterService 45 | */ 46 | public function nameFieldFormatter() { 47 | if ($this->nameFieldFormatterService==null) { 48 | $this->nameFieldFormatterService = new NameFieldFormatterService($this->apiKey, $this->context, $this->baseUrl); 49 | } 50 | return $this->nameFieldFormatterService; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/formatter/namefieldformatter/NameFieldFormatterService.php: -------------------------------------------------------------------------------- 1 | formatterServices()->personNameFormatter(); 23 | * $formatterResult = $personNameFormatter->format($myInputPerson, $myParams); 24 | * 25 | * @since v4.0 26 | */ 27 | class PersonNameFormatterService extends BaseService { 28 | 29 | private static $RESOURCE_PATH = "formatter/personnameformatter"; 30 | 31 | public function __construct($apiKey, Context $context, $baseUrl) { 32 | parent::__construct($apiKey, $context, $baseUrl); 33 | } 34 | 35 | 36 | /** 37 | * @param NaturalInputPerson $person 38 | * @param FormatterProperties $properties 39 | * @return FormatterResult 40 | * @throws ServiceException 41 | */ 42 | public function format(NaturalInputPerson $person, FormatterProperties $properties) { 43 | $queryParams = array(); 44 | $headerParams = array(); 45 | 46 | list($response, $httpResponseData) = $this->restHttpClient->callApiPost( 47 | PersonNameFormatterService::$RESOURCE_PATH, 48 | $queryParams, $headerParams, 49 | array('inputPerson'=>$person, 'properties'=>$properties, 'context'=>$this->context) 50 | ); 51 | try { 52 | return new FormatterResult($response->formatted, $response->unknown); 53 | } catch (\Exception $e) { 54 | throw $this->unmarshallingFailed($response, $httpResponseData); 55 | } 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/genderizer/GenderizerServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 24 | $this->context = $context; 25 | $this->baseUrl = $baseUrl; 26 | } 27 | 28 | /** 29 | * @return persongenderizer\PersonGenderizerService 30 | * @since v4.0 31 | */ 32 | public function personGenderizer() { 33 | if ($this->personGenderizerService==null) { 34 | $this->personGenderizerService = new persongenderizer\PersonGenderizerService($this->apiKey, $this->context, $this->baseUrl); 35 | } 36 | return $this->personGenderizerService; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/genderizer/persongenderizer/PersonGenderResult.php: -------------------------------------------------------------------------------- 1 | gender = $gender; 31 | $this->maleProportion = $maleProportion; 32 | $this->confidence = $confidence; 33 | } 34 | 35 | /** 36 | * @return ComputedPersonGender 37 | */ 38 | public function getGender() { 39 | return $this->gender; 40 | } 41 | 42 | /** 43 | * Only of interest if getGender() returns NEUTRAL. And even then it may be null (unknown). 44 | * @return float null if inapplicable or unknown. 45 | * Range 0-1, 0.5 is neutral, high above is more male and closer to 0 is more female). 46 | */ 47 | public function getMalePercent() { 48 | return $this->maleProportion; 49 | } 50 | 51 | /** 52 | * @return float 0-1, the higher the better. 53 | */ 54 | public function getConfidence() { 55 | return $this->confidence; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/genderizer/persongenderizer/PersonGenderizerService.php: -------------------------------------------------------------------------------- 1 | genderizerServices()->personGenderizer(); 20 | * $personGenderResult = $personGenderizer->assess($inputPerson); 21 | * echo (string)$personGenderResult->getGender(); //prints 'MALE' 22 | * 23 | * @since v4.0 24 | */ 25 | class PersonGenderizerService extends BaseService { 26 | 27 | private static $RESOURCE_PATH = "genderizer/persongenderizer"; 28 | 29 | public function __construct($apiKey, Context $context, $baseUrl) { 30 | parent::__construct($apiKey, $context, $baseUrl); 31 | } 32 | 33 | 34 | /** 35 | * @param NaturalInputPerson $person 36 | * @return PersonGenderResult 37 | * @throws ServiceException 38 | */ 39 | public function assess(NaturalInputPerson $person) { 40 | $queryParams = array(); 41 | $headerParams = array(); 42 | 43 | list($response, $httpResponseData) = $this->restHttpClient->callApiPost( 44 | PersonGenderizerService::$RESOURCE_PATH, 45 | $queryParams, $headerParams, 46 | array('inputPerson'=>$person, 'context'=>$this->context) 47 | ); 48 | try { 49 | return new PersonGenderResult(new ComputedPersonGender( 50 | $response->gender), 51 | (isset($response->maleProportion) ? $response->maleProportion : null), 52 | $response->confidence 53 | ); 54 | } catch (\Exception $e) { 55 | throw $this->unmarshallingFailed($response, $httpResponseData); 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/AgeMatchType.php: -------------------------------------------------------------------------------- 1 | value = $value; 23 | } 24 | 25 | 26 | 27 | public function __toString() { 28 | return $this->value; 29 | } 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/AgeMatcherResult.php: -------------------------------------------------------------------------------- 1 | matchType = $matchType; 16 | } 17 | 18 | /** 19 | * @return AgeMatchType 20 | */ 21 | public function getMatchType() { 22 | return $this->matchType; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/GenderMatchType.php: -------------------------------------------------------------------------------- 1 | value = $value; 23 | } 24 | 25 | 26 | public function __toString() { 27 | return $this->value; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/GenderMatcherResult.php: -------------------------------------------------------------------------------- 1 | matchType = $type; 32 | $this->confidence = $confidence; 33 | } 34 | 35 | /** 36 | * @return GenderMatchType 37 | */ 38 | public function getMatchType() { 39 | return $this->matchType; 40 | } 41 | 42 | /** 43 | * @return float 44 | */ 45 | public function getConfidence() { 46 | return $this->confidence; 47 | } 48 | 49 | /** 50 | * @return string[] 51 | */ 52 | public function getWarnings() { 53 | return $this->warnings; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/MatcherServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 23 | $this->context = $context; 24 | $this->baseUrl = $baseUrl; 25 | } 26 | 27 | /** 28 | * @return personmatcher\PersonMatcherService 29 | * @since v4.0 30 | */ 31 | public function personMatcher() { 32 | if ($this->personMatcher==null) { 33 | $this->personMatcher = new personmatcher\PersonMatcherService($this->apiKey, $this->context, $this->baseUrl); 34 | } 35 | return $this->personMatcher; 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/PersonNameMatchType.php: -------------------------------------------------------------------------------- 1 | Possible values are: 10 | * EQUAL 11 | * MATCHING 12 | * SIMILAR 13 | * PARTIAL 14 | * NO_SIMILARITY_FOUND 15 | * since v4.1 16 | * DIFFERENT 17 | *

18 | */ 19 | final class PersonNameMatchType { 20 | 21 | /** 22 | * @var string $value 23 | */ 24 | private $value = null; 25 | 26 | public function __construct($value) { 27 | if ($value!=='EQUAL' && $value!=='MATCHING' && $value!=='SIMILAR' && $value!=='PARTIAL' && $value!=='NO_SIMILARITY_FOUND' && $value!=='DIFFERENT') { 28 | throw new \Exception('Invalid value for PersonNameMatchType: '.$value.'!'); 29 | } 30 | $this->value = $value; 31 | } 32 | 33 | 34 | public function __toString() { 35 | return $this->value; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/PersonNameMatcherResult.php: -------------------------------------------------------------------------------- 1 | matchType = $type; 19 | } 20 | 21 | /** 22 | * 23 | * @return PersonNameMatchType 24 | */ 25 | public function getMatchType() { 26 | return $this->matchType; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/personmatcher/PersonMatchComposition.php: -------------------------------------------------------------------------------- 1 | value = $value; 23 | } 24 | 25 | 26 | 27 | public function __toString() { 28 | return $this->value; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/personmatcher/PersonMatchType.php: -------------------------------------------------------------------------------- 1 | value = $value; 23 | } 24 | 25 | 26 | 27 | public function __toString() { 28 | return $this->value; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/personmatcher/PersonMatcherResult.php: -------------------------------------------------------------------------------- 1 | matchType = $personMatchType; 65 | $this->matchComposition = $personMatchComposition; 66 | $this->points = $points; 67 | $this->confidence = $confidence; 68 | $this->personNameMatcherResult = $personNameMatch; 69 | $this->genderMatcherResult = $genderMatch; 70 | $this->ageMatcherResult = $ageMatch; 71 | } 72 | 73 | 74 | /** 75 | * @return PersonMatchType 76 | */ 77 | public function getMatchType() { 78 | return $this->matchType; 79 | } 80 | 81 | /** 82 | * @return PersonMatchComposition 83 | */ 84 | public function getMatchComposition() { 85 | return $this->matchComposition; 86 | } 87 | 88 | /** 89 | * 90 | * @return float 91 | */ 92 | public function getPoints() { 93 | return $this->points; 94 | } 95 | 96 | /** 97 | * 98 | * @return float 99 | */ 100 | public function getConfidence() { 101 | return $this->confidence; 102 | } 103 | 104 | /** 105 | * 106 | * @return PersonNameMatcherResult 107 | */ 108 | public function getPersonNameMatcherResult() { 109 | return $this->personNameMatcherResult; 110 | } 111 | 112 | /** 113 | * 114 | * @return GenderMatcherResult 115 | */ 116 | public function getGenderMatcherResult() { 117 | return $this->genderMatcherResult; 118 | } 119 | 120 | /** 121 | * @return AgeMatcherResult 122 | */ 123 | public function getAgeMatcherResult() { 124 | return $this->ageMatcherResult; 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/matcher/personmatcher/PersonMatcherService.php: -------------------------------------------------------------------------------- 1 | matcherServiceFactory()->personMatcher(); 25 | * $matchResult = $personMatcher->match($myInputPerson1, $myInputPerson2); 26 | * 27 | * @since v4.0 28 | */ 29 | class PersonMatcherService extends BaseService { 30 | 31 | private static $RESOURCE_PATH = "matcher/personmatcher"; 32 | 33 | public function __construct($apiKey, Context $context, $baseUrl) { 34 | parent::__construct($apiKey, $context, $baseUrl); 35 | } 36 | 37 | 38 | /** 39 | * @param NaturalInputPerson $person1 40 | * @param NaturalInputPerson $person2 41 | * @return PersonMatcherResult 42 | * @throws ServiceException 43 | */ 44 | public function match(NaturalInputPerson $person1, NaturalInputPerson $person2) { 45 | $queryParams = array(); 46 | $headerParams = array(); 47 | 48 | list($response, $httpResponseData) = $this->restHttpClient->callApiPost( 49 | PersonMatcherService::$RESOURCE_PATH, 50 | $queryParams, $headerParams, 51 | array('inputPerson1'=>$person1, 'inputPerson2'=>$person2, 'context'=>$this->context) 52 | ); 53 | 54 | try { 55 | return new PersonMatcherResult( 56 | new PersonMatchType($response->matchType), 57 | new PersonMatchComposition($response->personMatchComposition), 58 | $response->points, $response->confidence, 59 | new PersonNameMatcherResult(new PersonNameMatchType($response->personNameMatcherResult->matchType)), 60 | new GenderMatcherResult( 61 | new GenderMatchType($response->genderMatcherResult->matchType), 62 | isSet($response->genderMatcherResult->confidence) ? $response->genderMatcherResult->confidence : null, 63 | isSet($response->genderMatcherResult->warnings) ? $response->genderMatcherResult->warnings : null 64 | ), 65 | new AgeMatcherResult(new AgeMatchType($response->ageMatcherResult->matchType)) 66 | ); 67 | } catch (\Exception $e) { 68 | throw $this->unmarshallingFailed($response, $httpResponseData); 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/OutputPersonName.php: -------------------------------------------------------------------------------- 1 | terms = $terms; 21 | } 22 | 23 | /** 24 | * @return Term[] 25 | */ 26 | public function getTerms() { 27 | return $this->terms; 28 | } 29 | 30 | /** 31 | * Returns all terms that have the given term type, or empty array if none. 32 | * @param string $termType 33 | * @return Term[] 34 | */ 35 | public function getAll($termType) { 36 | $arr = array(); 37 | foreach ($this->terms as $term) { 38 | if ((string)$term->getTermType() === $termType) { 39 | array_push($arr, $term); 40 | } 41 | } 42 | return $arr; 43 | } 44 | 45 | /** 46 | * Returns the first term that has the given term type, or null if none. 47 | * @param string $termType 48 | * @return Term 49 | */ 50 | public function getFirst($termType) { 51 | foreach ($this->terms as $term) { 52 | if ((string)$term->getTermType() === $termType) { 53 | return $term; 54 | } 55 | } 56 | return null; 57 | } 58 | 59 | 60 | public function __toString() { 61 | $nameStr = ''; 62 | foreach ($this->terms as $term) { 63 | if ($nameStr != '') $nameStr .= ','; 64 | $nameStr .= $term; 65 | } 66 | $str = 'OutputPersonName{terms='.$nameStr.'}'; 67 | return $str; 68 | } 69 | 70 | public function toShortString() { 71 | $nameStr = ''; 72 | foreach ($this->terms as $term) { 73 | if ($nameStr != '') $nameStr .= ', '; 74 | $nameStr .= $term->toShortString(); 75 | } 76 | return $nameStr; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/OutputTermType.php: -------------------------------------------------------------------------------- 1 | value = $value; 40 | } 41 | 42 | 43 | 44 | public function __toString() { 45 | return $this->value; 46 | } 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/ParserServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 23 | $this->context = $context; 24 | $this->baseUrl = $baseUrl; 25 | } 26 | 27 | /** 28 | * @return personnameparser\PersonNameParserService 29 | * @since v4.0 30 | */ 31 | public function personNameParser() { 32 | if ($this->personNameParser==null) { 33 | $this->personNameParser = new personnameparser\PersonNameParserService($this->apiKey, $this->context, $this->baseUrl); 34 | } 35 | return $this->personNameParser; 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/Term.php: -------------------------------------------------------------------------------- 1 | string = $string; 21 | $this->termType = $termType; 22 | } 23 | 24 | /** 25 | * @return string 26 | */ 27 | public function getString() { 28 | return $this->string; 29 | } 30 | 31 | /** 32 | * @return OutputTermType 33 | */ 34 | public function getTermType() { 35 | return $this->termType; 36 | } 37 | 38 | public function __toString() { 39 | return 'Term{string='.$this->string.', termType='.$this->termType.'}'; 40 | } 41 | 42 | public function toShortString() { 43 | return $this->termType . ':'.$this->string; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/DisputeType.php: -------------------------------------------------------------------------------- 1 | value = $value; 62 | } 63 | 64 | 65 | 66 | public function __toString() { 67 | return $this->value; 68 | } 69 | 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/ParsedPerson.php: -------------------------------------------------------------------------------- 1 | personType = $personType; 53 | $this->personRole = $personRole; 54 | $this->gender = $gender; 55 | $this->addressingGivenName = $addressingGivenName; 56 | $this->addressingSurname = $addressingSurname; 57 | $this->outputPersonName = $outputPersonName; 58 | $this->people = $people; 59 | } 60 | 61 | /** 62 | * @return PersonType 63 | */ 64 | public function getPersonType() { 65 | return $this->personType; 66 | } 67 | 68 | /** 69 | * @return PersonRole 70 | */ 71 | public function getPersonRole() { 72 | return $this->personRole; 73 | } 74 | 75 | /** 76 | * @return PersonGenderResult or null 77 | */ 78 | public function getGender() { 79 | return $this->gender; 80 | } 81 | 82 | /** 83 | * @return null 84 | */ 85 | public function getAddressingGivenName() { 86 | return $this->addressingGivenName; 87 | } 88 | 89 | /** 90 | * @return null 91 | */ 92 | public function getAddressingSurname() { 93 | return $this->addressingSurname; 94 | } 95 | 96 | /** 97 | * @return OutputPersonName 98 | */ 99 | public function getOutputPersonName() { 100 | return $this->outputPersonName; 101 | } 102 | 103 | /** 104 | * Returns the people contained within this person. 105 | * 106 | *

If the getPersonType() is 'MULTIPLE' then expect content here. But also 'FAMILY' and 'LEGAL' can 107 | * have entries here.

108 | * 109 | * @return ParsedPerson[] the array may be empty 110 | */ 111 | public function getPeople() { 112 | return $this->people; 113 | } 114 | 115 | 116 | public function __toString() { 117 | $str = 'ParsedPerson{personType='.$this->personType.'}'; 118 | return $str; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/ParsedPersonMatch.php: -------------------------------------------------------------------------------- 1 | parsedPerson = $parsedPerson; 39 | $this->likeliness = $likeliness; 40 | $this->confidence = $confidence; 41 | $this->parserDisputes = $parserDisputes; 42 | } 43 | 44 | /** 45 | * @return ParsedPerson 46 | */ 47 | public function getParsedPerson() { 48 | return $this->parsedPerson; 49 | } 50 | 51 | /** 52 | * @return float 0-1 53 | */ 54 | public function getLikeliness() { 55 | return $this->likeliness; 56 | } 57 | 58 | /** 59 | * @return float 0-1 60 | */ 61 | public function getConfidence() { 62 | return $this->confidence; 63 | } 64 | 65 | /** 66 | * Usually empty, that's good. 67 | * @return ParserDispute[] 68 | */ 69 | public function getParserDisputes() { 70 | return $this->parserDisputes; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/ParserDispute.php: -------------------------------------------------------------------------------- 1 | disputeType = $disputeType; 26 | $this->message = $message; 27 | } 28 | 29 | /** 30 | * @return DisputeType 31 | */ 32 | public function getDisputeType() { 33 | return $this->disputeType; 34 | } 35 | 36 | /** 37 | * @return string 38 | */ 39 | public function getMessage() { 40 | return $this->message; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/PersonNameParserResult.php: -------------------------------------------------------------------------------- 1 | matches = $matches; 18 | } 19 | 20 | /** 21 | * Returns a non-empty array if getParsingStatus() is SUCCESS, and null otherwise. 22 | * @return ParsedPersonMatch[] 23 | */ 24 | public function getMatches() { 25 | return $this->matches; 26 | } 27 | 28 | /** 29 | * Returns the best match. 30 | * @return ParsedPersonMatch 31 | */ 32 | public function getBestMatch() { 33 | return $this->matches[0]; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/parser/personnameparser/PersonNameParserService.php: -------------------------------------------------------------------------------- 1 | nameParserServiceFactory()->personNameParser(); 27 | * $parseResult = $personNameParser->parse($myInputPerson); 28 | * 29 | * @since v4.0 30 | */ 31 | class PersonNameParserService extends BaseService { 32 | 33 | private static $RESOURCE_PATH = "parser/personnameparser"; 34 | 35 | 36 | public function __construct($apiKey, Context $context, $baseUrl) { 37 | parent::__construct($apiKey, $context, $baseUrl); 38 | } 39 | 40 | 41 | /** 42 | * @param NaturalInputPerson $person 43 | * @return PersonNameParserResult 44 | * @throws ServiceException 45 | */ 46 | public function parse(NaturalInputPerson $person) { 47 | $queryParams = array(); 48 | $headerParams = array(); 49 | 50 | list($response, $httpResponseData) = $this->restHttpClient->callApiPost( 51 | PersonNameParserService::$RESOURCE_PATH, 52 | $queryParams, $headerParams, 53 | array('inputPerson'=>$person, 'context'=>$this->context) 54 | ); 55 | 56 | try { 57 | $matches = array(); //ParsedPersonMatch 58 | foreach ($response->matches as $match) { 59 | $pp = $match->parsedPerson; 60 | 61 | $parsedPerson = $this->extractPerson($pp); 62 | 63 | $parserDisputes = array(); 64 | if (isSet($match->parserDisputes)) { 65 | foreach ($match->parserDisputes as $dispute) { 66 | array_push($parserDisputes, new ParserDispute(new DisputeType($dispute->disputeType), $dispute->message)); 67 | } 68 | } 69 | 70 | $parsedPersonMatch = new ParsedPersonMatch( 71 | $parsedPerson, 72 | $match->likeliness, 73 | $match->confidence, 74 | $parserDisputes 75 | ); 76 | array_push($matches, $parsedPersonMatch); 77 | } 78 | return new PersonNameParserResult($matches); 79 | } catch (\Exception $e) { 80 | throw $this->unmarshallingFailed($response, $httpResponseData); 81 | } 82 | } 83 | 84 | private function extractGender($parsedPerson) { 85 | if (!isSet($parsedPerson->gender)) { 86 | return null; 87 | } 88 | return new PersonGenderResult( 89 | new ComputedPersonGender($parsedPerson->gender->gender), 90 | (isset( $parsedPerson->gender->maleProportion) ? $parsedPerson->gender->maleProportion : null), 91 | $parsedPerson->gender->confidence 92 | ); 93 | } 94 | 95 | private function extractTerms($parsedPerson) { 96 | $terms = array(); 97 | if (isSet($parsedPerson->outputPersonName) && isSet($parsedPerson->outputPersonName->terms)) { 98 | foreach ($parsedPerson->outputPersonName->terms as $term) { 99 | array_push($terms, new Term($term->string, new OutputTermType($term->termType))); 100 | } 101 | } 102 | return new OutputPersonName($terms); 103 | } 104 | 105 | private function extractPeople($parsedPerson) { 106 | $people = array(); 107 | if (isSet($parsedPerson->people)) { 108 | foreach ($parsedPerson->people as $onePerson) { 109 | $extractedPerson = $this->extractPerson($onePerson); 110 | array_push($people, $extractedPerson); 111 | } 112 | } 113 | return $people; 114 | } 115 | 116 | private function extractPerson($pp) { 117 | $gender = $this->extractGender($pp); 118 | $outputPersonName = $this->extractTerms($pp); 119 | $people = $this->extractPeople($pp); 120 | return new ParsedPerson( 121 | new PersonType($pp->personType), 122 | new PersonRole($pp->personRole), 123 | $gender, 124 | (isSet($pp->addressingGivenName)) ? $pp->addressingGivenName : null, 125 | (isSet($pp->addressingSurname)) ? $pp->addressingSurname : null, 126 | $outputPersonName, 127 | $people 128 | ); 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/DataItem.php: -------------------------------------------------------------------------------- 1 | value = $value; 45 | } 46 | 47 | 48 | 49 | public function __toString() { 50 | return $this->value; 51 | } 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/DetectedRisk.php: -------------------------------------------------------------------------------- 1 | 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 | 79 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/DisguiseRiskType.php: -------------------------------------------------------------------------------- 1 | Such mangled input is used to circumvent machine processing.

11 | * 12 | *

Humans can still understand these modified values, but machines can't unless they detect the patterns and clean 13 | * the input.

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 DisguiseRiskType: '.$value.'!'); 55 | } 56 | $this->value = $value; 57 | } 58 | 59 | 60 | 61 | public function __toString() { 62 | return $this->value; 63 | } 64 | 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/FakeRiskType.php: -------------------------------------------------------------------------------- 1 | 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.

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!=='STRING_SIMILARITY' && $value!=='OTHER') { 82 | throw new \Exception('Invalid value for FakeRiskType: '.$value.'!'); 83 | } 84 | $this->value = $value; 85 | } 86 | 87 | 88 | 89 | public function __toString() { 90 | return $this->value; 91 | } 92 | 93 | } 94 | 95 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/RiskDetectorResult.php: -------------------------------------------------------------------------------- 1 | 1) throw new \Exception("Score is out of range [-1,1]: ".$score); 28 | if ($score > 0) { 29 | if (sizeof($risks)==0) throw new \Exception("At least one risk is required when there is a positive score!"); 30 | } 31 | $this->score = $score; 32 | $this->risks = $risks; 33 | } 34 | 35 | 36 | /** 37 | * An overall score considering all the detected risks and all the positive attributes of the record. 38 | * 39 | * Range [-1,0) means no risks were detected and the record looks good. 40 | * 0 means no risks were detected, but also no positive attributes were found, the service can't tell for sure. 41 | * Range (0,1] means one or multiple risks were detected. 42 | * 43 | * @return double in range [-1,1] 44 | */ 45 | public function getScore() { 46 | return $this->score; 47 | } 48 | 49 | public function hasRisk() { 50 | return !empty($this->risks); 51 | } 52 | 53 | /** 54 | * Returns all the detected risks. 55 | * @return DetectedRisk[] Sorted by severity having the worst come first. Possibly empty, guaranteed to be non-empty if the getScore() is > 0. 56 | */ 57 | public function getRisks() { 58 | return $this->risks; 59 | } 60 | 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/RiskDetectorServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 25 | $this->context = $context; 26 | $this->baseUrl = $baseUrl; 27 | } 28 | 29 | /** 30 | * @return riskdetector\PersonRiskDetectorService 31 | */ 32 | public function personRiskDetector() { 33 | if ($this->personRiskDetector==null) { 34 | $this->personRiskDetector = new riskdetector\PersonRiskDetectorService($this->apiKey, $this->context, $this->baseUrl); 35 | } 36 | return $this->personRiskDetector; 37 | } 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/riskdetector/RiskType.php: -------------------------------------------------------------------------------- 1 | riskServices()->personRiskDetector(); 24 | * $riskResult = $riskDetector->detect($myInputPerson); 25 | * 26 | * @since v5.3 27 | */ 28 | class PersonRiskDetectorService extends BaseService { 29 | 30 | private static $RESOURCE_PATH = "riskdetector/person"; 31 | 32 | 33 | public function __construct($apiKey, Context $context, $baseUrl) { 34 | parent::__construct($apiKey, $context, $baseUrl); 35 | } 36 | 37 | 38 | /** 39 | * @param NaturalInputPerson $person 40 | * @return RiskDetectorResult 41 | * @throws ServiceException 42 | */ 43 | public function detect(NaturalInputPerson $person) { 44 | $queryParams = array(); 45 | $headerParams = array(); 46 | 47 | list($response, $httpResponseData) = $this->restHttpClient->callApiPost( 48 | PersonRiskDetectorService::$RESOURCE_PATH, 49 | $queryParams, $headerParams, 50 | array('inputPerson'=>$person, 'context'=>$this->context) 51 | ); 52 | 53 | try { 54 | $score = $response->score; 55 | $risks = array(); 56 | foreach ($response->risks as $risk) { 57 | $risk = new DetectedRisk( 58 | new DataItem($risk->dataItem), 59 | $this->_riskTypeToEnum2($risk->riskType), 60 | $risk->riskScore, 61 | $this->_riskReason($risk) 62 | ); 63 | array_push($risks, $risk); 64 | } 65 | return new RiskDetectorResult($score, $risks); 66 | } catch (\Exception $e) { 67 | throw $this->unmarshallingFailed($response, $httpResponseData); 68 | } 69 | } 70 | 71 | /** 72 | * The object structure changed in version 5.3. This doesn't work anymore. 73 | * @throws \Exception 74 | */ 75 | private function _riskTypeToEnum($val) { 76 | if ($val[0] === 'FakeRiskType') { 77 | return new FakeRiskType($val[1]); 78 | } else if ($val[0] === 'DisguiseRiskType') { 79 | return new DisguiseRiskType($val[1]); 80 | } else { 81 | throw new \Exception("Unsupported risk class: ".$val[0]); 82 | } 83 | } 84 | 85 | private function _riskTypeToEnum2($val) { 86 | if ($val === 'RANDOM_TYPING' || $val === 'PLACEHOLDER' || $val === 'FICTIONAL' || $val === 'FAMOUS' || 87 | $val === 'HUMOROUS' || $val === 'INVALID' || $val === 'STRING_SIMILARITY' || $val === 'OTHER') { 88 | return new FakeRiskType($val); 89 | } else if ($val === 'PADDING' || $val === 'STUTTER_TYPING' || $val === 'SPACED_TYPING') { 90 | return new DisguiseRiskType($val); 91 | } else { 92 | throw new \Exception("Unsupported risk type: ".$val); 93 | } 94 | } 95 | 96 | private function _riskReason($val) { 97 | if (isset($val->reason)) { 98 | return $val->reason; 99 | } else { 100 | //(temporary fallback) 101 | return "(no reason specified)"; 102 | } 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/system/SystemServiceFactory.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 25 | $this->context = $context; 26 | $this->baseUrl = $baseUrl; 27 | } 28 | 29 | /** 30 | * @return PingService 31 | * @since v4.0 32 | */ 33 | public function ping() { 34 | if ($this->pingService==null) { 35 | $this->pingService = new PingService($this->apiKey, $this->context, $this->baseUrl); 36 | } 37 | return $this->pingService; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/org/nameapi/client/services/system/ping/PingService.php: -------------------------------------------------------------------------------- 1 | systemServices()->pingService(); 16 | * $pong = $ping->ping(); 17 | */ 18 | class PingService extends BaseService { 19 | 20 | private static $RESOURCE_PATH = "system/ping"; 21 | 22 | public function __construct($apiKey, Context $context, $baseUrl) { 23 | parent::__construct($apiKey, $context, $baseUrl); 24 | } 25 | 26 | /** 27 | * @return string 'pong' 28 | * @throws ServiceException 29 | */ 30 | public function ping() { 31 | $queryParams = array(); 32 | $headerParams = array(); 33 | 34 | list($response, $httpResponseData) = $this->restHttpClient->callApiGet( 35 | PingService::$RESOURCE_PATH, 36 | $queryParams, $headerParams 37 | ); 38 | if ($response !== 'pong') { 39 | throw $this->unmarshallingFailed($response, $httpResponseData); 40 | } 41 | return $response; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/context/Context.php: -------------------------------------------------------------------------------- 1 | priority = ($priority==null) ? null : (string)$priority; 30 | $this->place = $place; 31 | $this->textCase = ($textCase==null) ? null : (string)$textCase; 32 | $this->properties = isSet($properties) ? $properties : array(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/context/ContextBuilder.php: -------------------------------------------------------------------------------- 1 | place = $place; 19 | return $this; 20 | } 21 | 22 | /** 23 | * @param $priority 24 | * either an instance of Priority, or a string in upper case. 25 | * @return ContextBuilder 26 | */ 27 | function priority($priority) { 28 | if (is_string($priority)) { 29 | $priority = new Priority($priority); 30 | } 31 | $this->priority = $priority; 32 | return $this; 33 | } 34 | 35 | /** 36 | * @param TextCase $textCase 37 | * @return ContextBuilder 38 | */ 39 | function textCase($textCase) { 40 | $this->textCase = $textCase; 41 | return $this; 42 | } 43 | 44 | /** 45 | * @param string $key 46 | * @param string $value 47 | * @return ContextBuilder 48 | */ 49 | function property($key, $value) { 50 | if ($this->properties==null) $this->properties = array(); 51 | $this->properties[$key] = $value; 52 | return $this; 53 | } 54 | 55 | 56 | /** 57 | * @return Context 58 | */ 59 | function build() { 60 | return new Context($this->priority, $this->place, $this->textCase, $this->properties); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/context/Priority.php: -------------------------------------------------------------------------------- 1 | value = $value; 35 | } 36 | 37 | 38 | public function __toString() { 39 | return $this->value; 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/context/TextCase.php: -------------------------------------------------------------------------------- 1 | value = $value; 39 | } 40 | 41 | 42 | public function __toString() { 43 | return $this->value; 44 | } 45 | 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/AddressRelation.php: -------------------------------------------------------------------------------- 1 | value = $value; 25 | } 26 | 27 | 28 | 29 | public function __toString() { 30 | return $this->value; 31 | } 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/InputAddress.php: -------------------------------------------------------------------------------- 1 | streetInfo = $streetInfo; 48 | $this->pobox = $pobox; 49 | $this->placeInfo = $placeInfo; 50 | } 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/StructuredAddressBuilder.php: -------------------------------------------------------------------------------- 1 | The setters don't do anything other than setting the value. They don't check if the value was 11 | * set already, they don't trim the values.

12 | */ 13 | class StructuredAddressBuilder { 14 | 15 | /** 16 | * @var StreetInfo|null $streetInfo 17 | */ 18 | private $streetInfo; 19 | /** 20 | * @var string|null $pobox 21 | */ 22 | private $pobox; 23 | /** 24 | * @var PlaceInfo|null $placeInfo 25 | */ 26 | private $placeInfo; 27 | 28 | 29 | function __construct() { 30 | } 31 | 32 | 33 | /** 34 | * @param StreetInfo|null $streetInfo 35 | * @return StructuredAddressBuilder 36 | */ 37 | public function streetInfo($streetInfo) { 38 | $this->streetInfo = $streetInfo; 39 | return $this; 40 | } 41 | 42 | /** 43 | * @param string|null $pobox 44 | * @return StructuredAddressBuilder 45 | */ 46 | public function pobox($pobox) { 47 | $this->pobox = $pobox; 48 | return $this; 49 | } 50 | 51 | /** 52 | * @param PlaceInfo|null $placeInfo 53 | * @return StructuredAddressBuilder 54 | */ 55 | public function placeInfo($placeInfo) { 56 | $this->placeInfo = $placeInfo; 57 | return $this; 58 | } 59 | 60 | 61 | /** 62 | * @return StructuredAddress 63 | */ 64 | public function build() { 65 | return new StructuredAddress( 66 | $this->streetInfo, 67 | $this->pobox, 68 | $this->placeInfo 69 | ); 70 | } 71 | 72 | } 73 | 74 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/StructuredPlaceInfo.php: -------------------------------------------------------------------------------- 1 | locality = $locality; 55 | $this->postalCode = $postalCode; 56 | $this->neighborhood = $neighborhood; 57 | $this->region = $region; 58 | $this->country = $country; 59 | } 60 | 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/StructuredPlaceInfoBuilder.php: -------------------------------------------------------------------------------- 1 | The setters don't do anything other than setting the value. They don't check if the value was 11 | * set already, they don't trim the values.

12 | */ 13 | class StructuredPlaceInfoBuilder { 14 | 15 | /** 16 | * @var string|null $locality 17 | */ 18 | private $locality = null; 19 | /** 20 | * @var string|null $postalCode 21 | */ 22 | private $postalCode; 23 | /** 24 | * @var string|null $neighborhood 25 | */ 26 | private $neighborhood; 27 | /** 28 | * @var string|null $region 29 | */ 30 | private $region; 31 | /** 32 | * @var string|null $country 33 | */ 34 | private $country; 35 | 36 | 37 | function __construct() { 38 | } 39 | 40 | 41 | /** 42 | * Also known as place name, city etc. 43 | * 44 | * @param string|null $locality 45 | * @return StructuredPlaceInfoBuilder 46 | */ 47 | public function locality($locality) { 48 | $this->locality = $locality; 49 | return $this; 50 | } 51 | 52 | /** 53 | * Also known as zip code in the USA. 54 | * 55 | * @param string|null $postalCode 56 | * @return StructuredPlaceInfoBuilder 57 | */ 58 | public function postalCode($postalCode) { 59 | $this->postalCode = $postalCode; 60 | return $this; 61 | } 62 | 63 | /** 64 | * @param string|null $neighborhood 65 | * @return StructuredPlaceInfoBuilder 66 | */ 67 | public function neighborhood($neighborhood) { 68 | $this->neighborhood = $neighborhood; 69 | return $this; 70 | } 71 | 72 | /** 73 | * @param string|null $region 74 | * @return StructuredPlaceInfoBuilder 75 | */ 76 | public function region($region) { 77 | $this->region = $region; 78 | return $this; 79 | } 80 | 81 | /** 82 | * Either fully spelled out such as "Germany" or the ISO 3166 alpha-2 code such as "DE". 83 | * 84 | * @param string|null $country 85 | * @return StructuredPlaceInfoBuilder 86 | */ 87 | public function country($country) { 88 | $this->country = $country; 89 | return $this; 90 | } 91 | 92 | 93 | /** 94 | * @return StructuredPlaceInfo 95 | */ 96 | public function build() { 97 | return new StructuredPlaceInfo( 98 | $this->locality, 99 | $this->postalCode, 100 | $this->neighborhood, 101 | $this->region, 102 | $this->country 103 | ); 104 | } 105 | 106 | 107 | } 108 | 109 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/StructuredStreetInfo.php: -------------------------------------------------------------------------------- 1 | streetName = $streetName; 62 | $this->houseNumber = $houseNumber; 63 | $this->building = $building; 64 | $this->staircase = $staircase; 65 | $this->floor = $floor; 66 | $this->apartment = $apartment; 67 | } 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/StructuredStreetInfoBuilder.php: -------------------------------------------------------------------------------- 1 | The setters don't do anything other than setting the value. They don't check if the value was 11 | * set already, they don't trim the values.

12 | */ 13 | class StructuredStreetInfoBuilder { 14 | 15 | /** 16 | * @var string|null $streetName 17 | */ 18 | private $streetName; 19 | /** 20 | * @var string|null $houseNumber 21 | */ 22 | private $houseNumber; 23 | /** 24 | * @var string|null $building 25 | */ 26 | private $building; 27 | /** 28 | * @var string|null $staircase 29 | */ 30 | private $staircase; 31 | /** 32 | * @var string|null $floor 33 | */ 34 | private $floor; 35 | /** 36 | * @var string|null $apartment 37 | */ 38 | private $apartment; 39 | 40 | 41 | function __construct() { 42 | } 43 | 44 | 45 | /** 46 | * @param string|null $streetName 47 | * @return StructuredStreetInfoBuilder 48 | */ 49 | public function streetName($streetName) { 50 | $this->streetName = $streetName; 51 | return $this; 52 | } 53 | 54 | /** 55 | * @param string|null $houseNumber 56 | * @return StructuredStreetInfoBuilder 57 | */ 58 | public function houseNumber($houseNumber) { 59 | $this->houseNumber = $houseNumber; 60 | return $this; 61 | } 62 | 63 | /** 64 | * @param string|null $building 65 | * @return StructuredStreetInfoBuilder 66 | */ 67 | public function building($building) { 68 | $this->building = $building; 69 | return $this; 70 | } 71 | 72 | /** 73 | * @param string|null $staircase 74 | * @return StructuredStreetInfoBuilder 75 | */ 76 | public function staircase($staircase) { 77 | $this->staircase = $staircase; 78 | return $this; 79 | } 80 | 81 | /** 82 | * @param string|null $floor 83 | * @return StructuredStreetInfoBuilder 84 | */ 85 | public function floor($floor) { 86 | $this->floor = $floor; 87 | return $this; 88 | } 89 | 90 | /** 91 | * @param string|null $apartment 92 | * @return StructuredStreetInfoBuilder 93 | */ 94 | public function apartment($apartment) { 95 | $this->apartment = $apartment; 96 | return $this; 97 | } 98 | 99 | 100 | /** 101 | * @return StructuredStreetInfo 102 | */ 103 | public function build() { 104 | return new StructuredStreetInfo( 105 | $this->streetName, 106 | $this->houseNumber, 107 | $this->building, 108 | $this->staircase, 109 | $this->floor, 110 | $this->apartment 111 | ); 112 | } 113 | 114 | } 115 | 116 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/address/UseForAllAddressRelation.php: -------------------------------------------------------------------------------- 1 | This is the case when only one address is known from the InputPerson.

11 | */ 12 | class UseForAllAddressRelation extends AddressRelation { 13 | 14 | /** 15 | * Used for JSON marshalling only. 16 | */ 17 | public $type = 'UseForAllAddressRelation'; 18 | 19 | /** 20 | * @var InputAddress $address 21 | */ 22 | public $address; 23 | 24 | 25 | /** 26 | * UseForAllAddressRelation constructor. 27 | * @param InputAddress $address 28 | */ 29 | public function __construct(InputAddress $address) { 30 | $this->address = $address; 31 | } 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/contact/EmailAddress.php: -------------------------------------------------------------------------------- 1 | emailAddress = $emailAddress; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/contact/EmailAddressFactory.php: -------------------------------------------------------------------------------- 1 | fullNumber = $fullNumber; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/contact/TelNumberFactory.php: -------------------------------------------------------------------------------- 1 | value = $value; 25 | } 26 | 27 | 28 | 29 | public function __toString() { 30 | return $this->value; 31 | } 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/NaturalInputPerson.php: -------------------------------------------------------------------------------- 1 | personName = $personName; 108 | $this->gender = ($gender!=null) ? (String)$gender : null; 109 | $this->age = $ageInfo; 110 | $this->maritalStatus = ($maritalStatus!=null) ? (String)$maritalStatus : null; 111 | $this->nationalities = $nationalities; 112 | $this->nativeLanguages = $nativeLanguages; 113 | $this->correspondenceLanguage = $correspondenceLanguage; 114 | $this->religion = $religion; 115 | $this->addresses = $addresses; 116 | $this->telNumbers = $telNumbers; 117 | $this->emailAddresses = $emailAddresses; 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/NaturalInputPersonBuilder.php: -------------------------------------------------------------------------------- 1 | The setters don't do anything other than setting the value. They don't check if the value was 20 | * set already, they don't trim the values.

21 | */ 22 | class NaturalInputPersonBuilder { 23 | 24 | /** 25 | * @var InputPersonName|null $personName 26 | */ 27 | private $personName = null; 28 | 29 | /** 30 | * @var StoragePersonGender $gender 31 | */ 32 | private $gender; 33 | 34 | /** 35 | * @var AgeInfo|null $ageInfo 36 | */ 37 | private $ageInfo = null; 38 | 39 | /** 40 | * @var MaritalStatus $maritalStatus 41 | */ 42 | private $maritalStatus; 43 | 44 | /** 45 | * @var string[]|null $nationalities 46 | */ 47 | private $nationalities = null; 48 | 49 | /** 50 | * @var string[]|null $nativeLanguages 51 | */ 52 | private $nativeLanguages = null; 53 | 54 | /** 55 | * @var string|null $correspondenceLanguage 56 | */ 57 | private $correspondenceLanguage = null; 58 | 59 | /** 60 | * @var string|null $religion 61 | */ 62 | private $religion = null; 63 | 64 | /** 65 | * @var AddressRelation[]|null $addresses 66 | */ 67 | private $addresses = null; 68 | 69 | /** 70 | * @var TelNumber[]|null $telNumbers 71 | */ 72 | private $telNumbers = null; 73 | 74 | /** 75 | * @var EmailAddress[]|null $emailAddresses 76 | */ 77 | private $emailAddresses = null; 78 | 79 | 80 | 81 | function __construct() { 82 | } 83 | 84 | 85 | /** 86 | * @param InputPersonName|null $personName 87 | * @return NaturalInputPersonBuilder 88 | */ 89 | public function name(InputPersonName $personName) { 90 | $this->personName = $personName; 91 | return $this; 92 | } 93 | 94 | /** 95 | * @param $gender 96 | * either an instance of StoragePersonGender, or a string in upper case like 'MALE'. 97 | * @return NaturalInputPersonBuilder 98 | */ 99 | public function gender($gender) { 100 | if (is_string($gender)) { 101 | $gender = new StoragePersonGender($gender); 102 | } 103 | $this->gender = $gender; 104 | return $this; 105 | } 106 | 107 | /** 108 | * @param AgeInfo $ageInfo 109 | * @return NaturalInputPersonBuilder 110 | */ 111 | public function ageInfo(AgeInfo $ageInfo) { 112 | $this->ageInfo = $ageInfo; 113 | return $this; 114 | } 115 | 116 | /** 117 | * @param MaritalStatus $maritalStatus 118 | * @return NaturalInputPersonBuilder 119 | */ 120 | public function maritalStatus($maritalStatus) { 121 | $this->maritalStatus = $maritalStatus; 122 | return $this; 123 | } 124 | 125 | /** 126 | * @param string $nationality 127 | * @return NaturalInputPersonBuilder 128 | */ 129 | public function addNationality($nationality) { 130 | if ($this->nationalities==null) { 131 | $this->nationalities = array(); 132 | } 133 | array_push($this->nationalities, $nationality); 134 | return $this; 135 | } 136 | 137 | /** 138 | * @param string $nativeLanguage 139 | * @return NaturalInputPersonBuilder 140 | */ 141 | public function addNativeLanguage($nativeLanguage) { 142 | if ($this->nativeLanguages==null) { 143 | $this->nativeLanguages = array(); 144 | } 145 | array_push($this->nativeLanguages, $nativeLanguage); 146 | return $this; 147 | } 148 | 149 | /** 150 | * @param string $correspondenceLanguage 151 | * @return NaturalInputPersonBuilder 152 | */ 153 | public function correspondenceLanguage($correspondenceLanguage) { 154 | $this->correspondenceLanguage = $correspondenceLanguage; 155 | return $this; 156 | } 157 | 158 | /** 159 | * @param string $religion 160 | * @return NaturalInputPersonBuilder 161 | */ 162 | public function religion($religion) { 163 | $this->religion = $religion; 164 | return $this; 165 | } 166 | 167 | //TODO not used yet 168 | // public function addAddress($address) { 169 | // } 170 | 171 | /** 172 | * @param string|TelNumber $telNumber 173 | * @return NaturalInputPersonBuilder 174 | */ 175 | public function addTelNumber($telNumber) { 176 | if ($this->telNumbers==null) { 177 | $this->telNumbers = array(); 178 | } 179 | if (is_string($telNumber)) { 180 | $telNumber = \org\nameapi\ontology\input\entities\contact\TelNumberFactory::forNumber($telNumber); 181 | } 182 | array_push($this->telNumbers, $telNumber); 183 | return $this; 184 | } 185 | 186 | /** 187 | * @param string|EmailAddress $emailAddress 188 | * @return NaturalInputPersonBuilder 189 | */ 190 | public function addEmailAddress($emailAddress) { 191 | if ($this->emailAddresses==null) { 192 | $this->emailAddresses = array(); 193 | } 194 | if (is_string($emailAddress)) { 195 | $emailAddress = \org\nameapi\ontology\input\entities\contact\EmailAddressFactory::forAddress($emailAddress); 196 | } 197 | array_push($this->emailAddresses, $emailAddress); 198 | return $this; 199 | } 200 | 201 | 202 | /** 203 | * @param InputAddress $address 204 | * @return NaturalInputPersonBuilder 205 | */ 206 | public function addAddressForAll(InputAddress $address) { 207 | if ($this->addresses==null) { 208 | $this->addresses = array(); 209 | } 210 | array_push($this->addresses, new UseForAllAddressRelation($address)); 211 | return $this; 212 | } 213 | 214 | 215 | /** 216 | * @return NaturalInputPerson 217 | */ 218 | public function build() { 219 | return new NaturalInputPerson( 220 | $this->personName, $this->gender, 221 | $this->ageInfo, $this->maritalStatus, 222 | $this->nationalities, $this->nativeLanguages, 223 | $this->correspondenceLanguage, 224 | $this->religion, 225 | $this->addresses, 226 | $this->telNumbers, $this->emailAddresses 227 | ); 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/PersonRole.php: -------------------------------------------------------------------------------- 1 | value = $value; 21 | } 22 | 23 | 24 | public function __toString() { 25 | return $this->value; 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/PersonType.php: -------------------------------------------------------------------------------- 1 | value = $value; 21 | } 22 | 23 | 24 | public function __toString() { 25 | return $this->value; 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/age/AgeInfo.php: -------------------------------------------------------------------------------- 1 | 2100) throw new \Exception("Year is out of legal range: ".$year."!"); 37 | if ($month<1 || $month>12) throw new \Exception("Month must be 1-12 but was: ".$month."!"); 38 | if ($day<1 || $day>31) throw new \Exception("Day must be 1-31 but was: ".$day."!"); 39 | $this->year = $year; 40 | $this->month = $month; 41 | $this->day = $day; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/age/BirthYear.php: -------------------------------------------------------------------------------- 1 | 2100) throw new \Exception("Year is out of legal range: ".$year."!"); 25 | $this->year = $year; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/age/BirthYearRange.php: -------------------------------------------------------------------------------- 1 | yearRange = $yearRange; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/age/YearRange.php: -------------------------------------------------------------------------------- 1 | startIncluding = $startIncluding; 28 | $this->endIncluding = $endIncluding; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/gender/ComputedPersonGender.php: -------------------------------------------------------------------------------- 1 | value = $value; 24 | } 25 | 26 | 27 | 28 | public function __toString() { 29 | return $this->value; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/gender/StoragePersonGender.php: -------------------------------------------------------------------------------- 1 | value = $value; 27 | } 28 | 29 | 30 | 31 | public function __toString() { 32 | return $this->value; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/InputPersonName.php: -------------------------------------------------------------------------------- 1 | nameFields = $nameFields; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/NameField.php: -------------------------------------------------------------------------------- 1 | string = $string; 32 | $this->fieldType = $fieldType; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/builder/AmericanInputPersonNameBuilder.php: -------------------------------------------------------------------------------- 1 | nameField(new NameField($string, CommonNameFieldType::GIVENNAME)); 17 | return $this; 18 | } 19 | 20 | /** 21 | * @param $string 22 | * @return $this 23 | */ 24 | public function middleName($string) { 25 | $this->nameField(new NameField($string, AmericanNameFieldType::MIDDLENAME)); 26 | return $this; 27 | } 28 | 29 | /** 30 | * @param $string 31 | * @return $this 32 | */ 33 | public function surname($string) { 34 | $this->nameField(new NameField($string, CommonNameFieldType::SURNAME)); 35 | return $this; 36 | } 37 | 38 | 39 | /** 40 | * @param $string 41 | * @return $this 42 | */ 43 | public function prefix($string) { 44 | $this->nameField(new NameField($string, AmericanNameFieldType::NAMEPREFIX)); 45 | return $this; 46 | } 47 | 48 | /** 49 | * @param $string 50 | * @return $this 51 | */ 52 | public function suffix($string) { 53 | $this->nameField(new NameField($string, AmericanNameFieldType::NAMESUFFIX)); 54 | return $this; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/builder/InputPersonNameBuilder.php: -------------------------------------------------------------------------------- 1 | nameField(new NameField($string, CommonNameFieldType::FULLNAME)); 23 | } 24 | 25 | /** 26 | * @param NameField $nameField 27 | * @return $this 28 | */ 29 | public function nameField(NameField $nameField) { 30 | array_push($this->nameFields, $nameField); 31 | return $this; 32 | } 33 | 34 | 35 | public function isEmpty() { 36 | return empty($this->nameFields); 37 | } 38 | 39 | public function build() { 40 | return new InputPersonName($this->nameFields); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/builder/WesternInputPersonNameBuilder.php: -------------------------------------------------------------------------------- 1 | nameField(new NameField($string, CommonNameFieldType::GIVENNAME)); 16 | return $this; 17 | } 18 | 19 | /** 20 | * @param $string 21 | * @return $this 22 | */ 23 | public function surname($string) { 24 | $this->nameField(new NameField($string, CommonNameFieldType::SURNAME)); 25 | return $this; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/org/nameapi/ontology/input/entities/person/name/types/AmericanNameFieldType.php: -------------------------------------------------------------------------------- 1 | priority(Priority::REALTIME()) 24 | ->build(); 25 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 26 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 27 | 28 | //the call: 29 | $deaDetector = $serviceFactory->emailServices()->disposableEmailAddressDetector(); 30 | $result = $deaDetector->isDisposable("abcdefgh@10minutemail.com"); 31 | 32 | //the assertions: 33 | $this->assertEquals('YES', (string)$result->getDisposable()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/functional/PersonNameMatcherServiceTest.php: -------------------------------------------------------------------------------- 1 | priority(Priority::REALTIME()) 26 | ->build(); 27 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 28 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 29 | $personNameMatcher = $serviceFactory->matcherServices()->personMatcher(); 30 | 31 | //the call: 32 | $inputPerson1 = NaturalInputPerson::builder() 33 | ->name(InputPersonName::westernBuilder() 34 | ->fullname( "John F. Kennedy" ) 35 | ->build()) 36 | ->gender("MALE") 37 | ->build(); 38 | 39 | //the call: 40 | $inputPerson2 = NaturalInputPerson::builder() 41 | ->name(InputPersonName::westernBuilder() 42 | ->fullname( "John Fitzgerald Kennedy" ) 43 | ->build()) 44 | ->gender("MALE") 45 | ->build(); 46 | 47 | $matchResult = $personNameMatcher->match($inputPerson1, $inputPerson2); 48 | 49 | //the assertions: 50 | $this->assertEquals('MATCHING', (string)$matchResult->getMatchType()); 51 | $this->assertTrue($matchResult->getPoints() >= 0.55 && $matchResult->getPoints() <= 1.0); 52 | $this->assertEquals('MATCHING', (string)$matchResult->getPersonNameMatcherResult()->getMatchType()); 53 | $this->assertEquals('FULL', (string)$matchResult->getMatchComposition()); 54 | $this->assertEquals('EQUAL', (string)$matchResult->getGenderMatcherResult()->getMatchType()); 55 | $this->assertEquals('NOT_APPLICABLE', (string)$matchResult->getAgeMatcherResult()->getMatchType()); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /tests/functional/PersonNameParserServiceTest.php: -------------------------------------------------------------------------------- 1 | priority(Priority::REALTIME()) 26 | ->build(); 27 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 28 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 29 | $personNameParser = $serviceFactory->parserServices()->personNameParser(); 30 | 31 | //the call: 32 | $inputPerson = NaturalInputPerson::builder() 33 | ->name(InputPersonName::westernBuilder() 34 | ->fullname( "John Doe" ) 35 | ->build()) 36 | ->gender("FEMALE") 37 | ->build(); 38 | $parseResult = $personNameParser->parse($inputPerson); 39 | 40 | //the assertions: 41 | $bestMatch = $parseResult->getBestMatch(); 42 | $this->assertEquals('NATURAL', (string)$bestMatch->getParsedPerson()->getPersonType()); 43 | $this->assertEquals('PRIMARY', (string)$bestMatch->getParsedPerson()->getPersonRole()); 44 | $this->assertEquals('John', $bestMatch->getParsedPerson()->getAddressingGivenName()); 45 | $this->assertEquals('Doe', $bestMatch->getParsedPerson()->getAddressingSurname()); 46 | $this->assertEquals('John', $bestMatch->getParsedPerson()->getOutputPersonName()->getFirst('GIVENNAME')->getString()); 47 | $this->assertEquals('Doe', $bestMatch->getParsedPerson()->getOutputPersonName()->getFirst('SURNAME')->getString()); 48 | 49 | $this->assertEquals(1, sizeof($bestMatch->getParserDisputes())); 50 | $this->assertEquals('GENDER', (string)$bestMatch->getParserDisputes()[0]->getDisputeType()); 51 | } 52 | 53 | public function test_parseTwoPeople() { 54 | //setup code: 55 | $context = Context::builder() 56 | ->priority(Priority::REALTIME()) 57 | ->build(); 58 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 59 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 60 | $personNameParser = $serviceFactory->parserServices()->personNameParser(); 61 | 62 | //the call: 63 | $inputPerson = NaturalInputPerson::builder() 64 | ->name(InputPersonName::westernBuilder() 65 | ->fullname( "Peter und Daniela Meyer" ) 66 | ->build()) 67 | ->build(); 68 | $parseResult = $personNameParser->parse($inputPerson); 69 | 70 | //the assertions: 71 | $bestMatch = $parseResult->getBestMatch(); 72 | $this->assertEquals('MULTIPLE', (string)$bestMatch->getParsedPerson()->getPersonType()); 73 | $people = $bestMatch->getParsedPerson()->getPeople(); 74 | $this->assertEquals(2, sizeof($people)); 75 | 76 | $firstPerson = $people[0]; 77 | $this->assertEquals('Peter', $firstPerson->getAddressingGivenName()); 78 | $this->assertEquals('Meyer', $firstPerson->getAddressingSurname()); 79 | $this->assertEquals('MALE', (string)$firstPerson->getGender()->getGender()); 80 | 81 | $secondPerson = $people[1]; 82 | $this->assertEquals('Daniela', $secondPerson->getAddressingGivenName()); 83 | $this->assertEquals('Meyer', $secondPerson->getAddressingSurname()); 84 | $this->assertEquals('FEMALE', (string)$secondPerson->getGender()->getGender()); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /tests/functional/PersonRiskDetectorServiceTest.php: -------------------------------------------------------------------------------- 1 | priority(Priority::REALTIME()) 30 | ->build(); 31 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 32 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 33 | $riskDetector = $serviceFactory->riskServices()->personRiskDetector(); 34 | 35 | //the call: 36 | $inputPerson = NaturalInputPerson::builder() 37 | ->name(InputPersonName::westernBuilder() 38 | ->fullname( "John Doe" ) 39 | ->build()) 40 | ->gender("FEMALE") 41 | ->build(); 42 | $riskResult = $riskDetector->detect($inputPerson); 43 | //var_dump($riskResult); 44 | 45 | //the assertions: 46 | $this->assertTrue($riskResult->getScore() >= 0.8); 47 | $this->assertTrue($riskResult->getScore() <= 1.0); 48 | $this->assertTrue($riskResult->hasRisk()); 49 | 50 | $this->assertEquals(1, sizeof($riskResult->getRisks())); 51 | $worstRisk = $riskResult->getRisks()[0]; 52 | $this->assertEquals('NAME', $worstRisk->getDataItem()); 53 | $this->assertEquals('PLACEHOLDER', $riskResult->getRisks()[0]->getRiskType()); 54 | $this->assertTrue($riskResult->getRisks()[0]->getRiskScore() >= 0.8); 55 | $this->assertTrue($riskResult->getRisks()[0]->getRiskScore() <= 1.0); 56 | } 57 | 58 | 59 | /** 60 | * In this test all fields are filled: 61 | * - the person's name 62 | * - an email address 63 | * - a telephone number 64 | * - a physical address 65 | */ 66 | public function testDetect_allValues() { 67 | $context = Context::builder() 68 | ->priority(Priority::REALTIME()) 69 | ->build(); 70 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 71 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 72 | $riskDetector = $serviceFactory->riskServices()->personRiskDetector(); 73 | 74 | $inputPerson = NaturalInputPerson::builder() 75 | ->name(InputPersonName::westernBuilder() 76 | ->givenName( "Donald" ) 77 | ->surname( "Duck" ) 78 | ->build()) 79 | ->addEmailAddress("info@example.com") 80 | ->addTelNumber("999 999 999") 81 | ->addAddressForAll(StructuredAddress::builder() 82 | ->streetInfo(StructuredStreetInfo::builder() 83 | ->streetName("Winzenheimer Str.") 84 | ->houseNumber("5") 85 | ->build()) 86 | ->placeInfo(StructuredPlaceInfo::builder() 87 | ->postalCode("55555") 88 | ->locality("Atlantis") 89 | ->build()) 90 | ->build()) 91 | ->build(); 92 | 93 | $result = $riskDetector->detect($inputPerson); 94 | //var_dump($result); 95 | 96 | $this->assertEquals(true, $result->hasRisk()); 97 | $this->assertEquals(7, sizeof($result->getRisks())); 98 | $this->assertTrue($result->getScore() >= 0.95 && $result->getScore() <= 1.0); 99 | 100 | $this->_expectRiskOnName($result->getRisks()); 101 | $this->_expectRiskOnAddress($result->getRisks()); 102 | $this->_expectRiskOnEmail($result->getRisks()); 103 | $this->_expectRiskOnTel($result->getRisks()); 104 | $firstRisk = $result->getRisks()[0]; 105 | $this->assertTrue($firstRisk->getRiskScore() >= 0.9 && $firstRisk->getRiskScore() <= 1.0); 106 | $this->assertEquals('NAME', (string)$firstRisk->getDataItem()); 107 | $this->assertEquals('FICTIONAL', (string)$firstRisk->getRiskType()); 108 | } 109 | 110 | 111 | private function _expectRiskOnName($risks) { 112 | foreach ($risks as $risk) { 113 | if ((string)$risk->getDataItem() === 'NAME') { 114 | return; 115 | } 116 | } 117 | $this->assertTrue(false); //fail 118 | } 119 | 120 | private function _expectRiskOnAddress($risks) { 121 | foreach ($risks as $risk) { 122 | if ((string)$risk->getDataItem() === 'ADDRESS') { 123 | 124 | return; 125 | } 126 | } 127 | $this->assertTrue(false); //fail 128 | } 129 | 130 | private function _expectRiskOnEmail($risks) { 131 | foreach ($risks as $risk) { 132 | if ((string)$risk->getDataItem() === 'EMAIL') { 133 | 134 | return; 135 | } 136 | } 137 | $this->assertTrue(false); //fail 138 | } 139 | 140 | private function _expectRiskOnTel($risks) { 141 | foreach ($risks as $risk) { 142 | if ((string)$risk->getDataItem() === 'TEL') { 143 | 144 | return; 145 | } 146 | } 147 | $this->assertTrue(false); //fail 148 | } 149 | 150 | 151 | 152 | } 153 | -------------------------------------------------------------------------------- /tests/functional/PingServiceTest.php: -------------------------------------------------------------------------------- 1 | priority(Priority::REALTIME()) 24 | ->build(); 25 | $myApiKey = 'your-api-key'; //grab one from nameapi.org 26 | $serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('api.nameapi.org'), '5.3'); 27 | 28 | //the call: 29 | $pingService = $serviceFactory->systemServices()->ping(); 30 | $result = $pingService->ping(); 31 | 32 | //the assertions: 33 | $this->assertEquals('pong', $result); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/unit/org/nameapi/client/fault/FaultInfoUnmarshallerTest.php: -------------------------------------------------------------------------------- 1 | >>foo<<assertEquals("AccessDenied", $faultInfo->getFaultCause()); 13 | $this->assertTrue($faultInfo->getBlame()->isClient()); 14 | $this->assertEquals("No such account: >>>foo<<getMessage()); 15 | $this->assertEquals("1201", $faultInfo->getApplicationErrorCode()); 16 | $this->assertEquals(null, $faultInfo->getIncidentId()); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /tests/unit/org/nameapi/ontology/input/context/ContextBuilderTest.php: -------------------------------------------------------------------------------- 1 | place('DE')->priority(Priority::REALTIME())->build(); 11 | $this->assertEquals('DE', $context->getPlace()); 12 | $this->assertEquals(Priority::REALTIME(), $context->getPriority()); 13 | $this->assertEquals(array(), $context->getProperties()); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /tests/unit/org/nameapi/ontology/input/context/PriorityTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(Priority::REALTIME(), Priority::REALTIME()); 11 | $this->assertTrue(Priority::REALTIME() === Priority::REALTIME()); 12 | $this->assertEquals('REALTIME', (string)Priority::REALTIME()); 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /tests/unit/org/nameapi/ontology/input/context/TextCaseTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(TextCase::TITLE_CASE(), TextCase::TITLE_CASE()); 11 | $this->assertTrue(TextCase::TITLE_CASE() === TextCase::TITLE_CASE()); 12 | $this->assertEquals('TITLE_CASE', (string)TextCase::TITLE_CASE()); 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /tests/unit/org/nameapi/ontology/input/entities/person/age/AgeInfoFactoryTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(29, $ageInfo->getDay()); 12 | $this->assertEquals(5, $ageInfo->getMonth()); 13 | $this->assertEquals(1917, $ageInfo->getYear()); 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------