Skip to content

Commit

Permalink
Token and tests:
Browse files Browse the repository at this point in the history
- Added possibility to use a token when calling cvrapi.dk.
- Updated tests to comply with phpunit 7.0.
- Added phpunit constraint in composer.json.
  • Loading branch information
Kristian Just committed Nov 4, 2020
1 parent 886d073 commit 49b2dec
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 41 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions src/Cvrapi/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
/**
* Config
*
* @author Kristian Just Iversen
* @author Kristian Just
*/
class Config {

class Config
{
/**
* API version
*
Expand Down Expand Up @@ -36,5 +36,4 @@ class Config {
* @var array
*/
static $countriesAvailable = array('dk', 'no');

}
}
32 changes: 17 additions & 15 deletions src/Cvrapi/Cvrapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -32,32 +33,36 @@ 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);
}

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);
Expand All @@ -75,8 +80,5 @@ public static function request($search, $country, $type = 'search', $project = '
} else {
return new \SimpleXMLElement($result);
}

}

}

13 changes: 6 additions & 7 deletions src/Cvrapi/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
/**
* Validate input
*
* @author Kristian Just Iversen
* @author Kristian Just
*/
class Validate {
class Validate
{
/**
* Validate int (e.g. VAT or phonenumber)
*
Expand All @@ -22,7 +22,7 @@ public static function int($input)

return $validated;
}

/**
* Validate search term
*
Expand All @@ -35,7 +35,7 @@ public static function search($input)
throw new \Exception('Invalid search term.');
}
}

/**
* Validate country
*
Expand All @@ -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.');
}
}
}

24 changes: 12 additions & 12 deletions tests/Cvrapi/CvrapiTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class CvrapiTest extends PHPUnit_Framework_TestCase {
class CvrapiTest extends \PHPUnit\Framework\TestCase {

/**
* Project not filled in
Expand Down Expand Up @@ -55,8 +55,8 @@ public function testGetCompanyByPNumber()
*/
public function testGetCompanyByCompanyName()
{
$result = \Cvrapi\Cvrapi::get('I/S Just Iversen', 'dk', 'Unit Testing');
$result = \Cvrapi\Cvrapi::get('Just Iversen', 'dk', 'Unit Testing');

$this->assertTrue(property_exists($result, 'vat'));
$this->assertTrue((int)$result->vat === 29910251);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down

0 comments on commit 49b2dec

Please sign in to comment.