/** * Executes the data retrieval from the external API. * The results from the external API are read, and the individual values are assigned to the array $returnFields, on the key present in the $fieldMappings array. in case there's no mapping for the field to be returned, it will stay with blanks. * @param string * @return mixed (JSON, array) */ public function getInformationFromProvider() { $apiInfo = file_get_contents(str_replace(self::$ipPlaceholder, $this->ipAddress->getIpAddress(), $this->apiUrl), true); if ($apiInfo !== false) { $decodedApiInfo = json_decode($apiInfo, true); foreach ($this->returnFields as $valueKey => $keyValue) { if (array_key_exists($valueKey, $this->fieldMappings) === true) { $keyFromApi = $this->fieldMappings[$valueKey]; if (array_key_exists($keyFromApi, $decodedApiInfo) === true) { $this->returnFields[$valueKey] = $decodedApiInfo[$keyFromApi]; } } } } }
/** * Tests if IpAddress class can set the ip address from the value of an external API. * This is the third and last way the class has to set the ip address. */ public function testIpFromExternalAPI() { $ip = new IpAddress(); $this->assertNotEquals('', $ip->getIpAddress()); }