diff --git a/README.md b/README.md index 902480d..df44bec 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,12 @@ $result = \Cvrapi\Cvrapi::request('I/S Just Iversen', 'dk', 'name', 'Description // Get company by searching specifically in the phone number field $result = \Cvrapi\Cvrapi::request('61401169', 'dk', 'phone', 'Description of my project'); + +// Get company using 'get' with token +$result = \Cvrapi\Cvrapi::get('29910251', 'dk', 'Description of my project', 'secret-token'); + +// Get company using 'request' with token +$result = \Cvrapi\Cvrapi::request('61401169', 'dk', 'phone', 'Description of my project', 'secret-token'); ``` ## License diff --git a/composer.json b/composer.json index 7424188..93f7699 100644 --- a/composer.json +++ b/composer.json @@ -3,13 +3,13 @@ "description": "CVR API package", "authors": [ { - "name": "Kristian Just Iversen", + "name": "Kristian Just", "email": "kristian@justiversen.dk" } ], "license": "MIT", "require": { - "php": ">=5.3.0" + "php": ">=7.3.0" }, "require-dev": { "phpunit/phpunit": "dev-master" diff --git a/src/Cvrapi/Config.php b/src/Cvrapi/Config.php index cf29a30..2220429 100644 --- a/src/Cvrapi/Config.php +++ b/src/Cvrapi/Config.php @@ -5,10 +5,10 @@ /** * Config * - * @author Kristian Just Iversen + * @author Kristian Just */ -class Config { - +class Config +{ /** * API version * @@ -36,5 +36,4 @@ class Config { * @var array */ static $countriesAvailable = array('dk', 'no'); - -} \ No newline at end of file +} diff --git a/src/Cvrapi/Cvrapi.php b/src/Cvrapi/Cvrapi.php index 3a8c220..42313fd 100644 --- a/src/Cvrapi/Cvrapi.php +++ b/src/Cvrapi/Cvrapi.php @@ -5,24 +5,25 @@ /** * CVR API * - * @author Kristian Just Iversen + * @author Kristian Just * @source http://cvrapi.dk/examples */ -class Cvrapi { - +class Cvrapi +{ /** * Get company by VAT, P-number or company name. * * @param mixed $search * @param string $country * @param string $project Optional. Description of your project. + * @param string $token Optional. Token used for CVRAPI.dk subscription. * @return object */ - public static function get($search, $country, $project = 'mit projekt') + public static function get($search, $country, $project = 'mit projekt', $token = null) { - return self::request($search, $country, 'search', $project); + return self::request($search, $country, 'search', $project, $token); } - + /** * Request company information * @@ -32,11 +33,11 @@ public static function get($search, $country, $project = 'mit projekt') * P-number or company name. Allowed types is 'search', * 'vat', 'name', 'produ' or 'phone'. * @param string $project Optional. Description of your project. + * @param string $token Optional. Token used for CVRAPI.dk subscription. * @return array|string Array of data or XML string. */ - public static function request($search, $country, $type = 'search', $project = 'mit projekt') + public static function request($search, $country, $type = 'search', $project = 'mit projekt', $token = null) { - // Validate search term if (in_array($type, array('vat', 'produ', 'phone'))) { $search = Validate::int($search); @@ -44,20 +45,24 @@ public static function request($search, $country, $type = 'search', $project = ' Validate::search($search); Validate::country($country); - + // Start cURL - $ch = curl_init(); + $ch = \curl_init(); // Determine protocol $protocol = Config::$secure ? 'https' : 'http'; - $parameters = Array( + $parameters = array( $type => $search, 'country' => $country, 'version' => Config::$version, 'format' => Config::$format, ); - + + if ($token) { + $parameters['token'] = $token; + } + // Set cURL options curl_setopt($ch, CURLOPT_URL, $protocol . '://cvrapi.dk/api?' . http_build_query($parameters)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @@ -75,8 +80,5 @@ public static function request($search, $country, $type = 'search', $project = ' } else { return new \SimpleXMLElement($result); } - } - } - diff --git a/src/Cvrapi/Validate.php b/src/Cvrapi/Validate.php index 051d75e..b192c26 100644 --- a/src/Cvrapi/Validate.php +++ b/src/Cvrapi/Validate.php @@ -5,10 +5,10 @@ /** * Validate input * - * @author Kristian Just Iversen + * @author Kristian Just */ -class Validate { - +class Validate +{ /** * Validate int (e.g. VAT or phonenumber) * @@ -22,7 +22,7 @@ public static function int($input) return $validated; } - + /** * Validate search term * @@ -35,7 +35,7 @@ public static function search($input) throw new \Exception('Invalid search term.'); } } - + /** * Validate country * @@ -44,9 +44,8 @@ public static function search($input) */ public static function country($input) { - if(!in_array($input, Config::$countriesAvailable)) { + if (!in_array($input, Config::$countriesAvailable)) { throw new \Exception('Invalid country code.'); } } } - diff --git a/tests/Cvrapi/CvrapiTest.php b/tests/Cvrapi/CvrapiTest.php index 1bc50ce..0914ad2 100644 --- a/tests/Cvrapi/CvrapiTest.php +++ b/tests/Cvrapi/CvrapiTest.php @@ -1,6 +1,6 @@ assertTrue(property_exists($result, 'vat')); $this->assertTrue((int)$result->vat === 29910251); } @@ -88,7 +88,7 @@ public function testSearchOnlyPNumber() */ public function testSearchOnlyCompanyName() { - $result = \Cvrapi\Cvrapi::request('I/S Just Iversen', 'dk', 'name', 'Unit Testing'); + $result = \Cvrapi\Cvrapi::request('Just Iversen', 'dk', 'name', 'Unit Testing'); $this->assertTrue(property_exists($result, 'vat')); $this->assertTrue((int)$result->vat === 29910251); @@ -118,21 +118,21 @@ public function testIncorrectVat() /** * Empty VAT number - * - * @expectedException Exception */ public function testEmptyVat() { + $this->expectException(Exception::class); + $result = \Cvrapi\Cvrapi::request('xxxx', 'dk', 'vat', 'Unit Testing'); } /** * Empty search term - * - * @expectedException Exception */ public function testEmptySearch() { + $this->expectException(Exception::class); + $result = \Cvrapi\Cvrapi::get('', 'dk', 'Unit Testing'); } @@ -150,21 +150,21 @@ public function testStrippedVat() /** * Malformed VAT number - * - * @expectedException Exception */ public function testMalformedVat() { + $this->expectException(Exception::class); + $result = \Cvrapi\Cvrapi::request('xxxx', 'dk', 'vat', 'Unit Testing'); } /** * Not available country code - * - * @expectedException Exception */ public function testMalformedCountryCode() { + $this->expectException(Exception::class); + $result = \Cvrapi\Cvrapi::request('29910251', 'ch', 'Unit Testing'); }