Skip to content

Commit f28c23d

Browse files
committed
Aggiunte le seguenti api:
Comuni Geocoding Marche Temporali
1 parent d78592c commit f28c23d

13 files changed

Lines changed: 980 additions & 31 deletions

OpenApi.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function __construct(array $scopes, string $username, string $apikey, $environme
5959
$this->clients[] = $nome;
6060
}
6161
}
62+
63+
$this->validations = new \OpenApi\classes\utility\Plugins\Validations();
6264
}
6365

6466
/**
@@ -77,6 +79,10 @@ private function getListaModuli(){
7779

7880
$moduli['visengine2.altravia.com'] = "\\OpenApi\\classes\\VisEngine";
7981
$nomi['visengine2.altravia.com'] = "visengine";
82+
83+
84+
$moduli['comuni.openapi.it'] = "\\OpenApi\\classes\\Comuni";
85+
$nomi['comuni.openapi.it'] = "comuni";
8086
return array($moduli,$nomi);
8187
}
8288

classes/Comuni.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class Comuni extends OpenApiBase {
4+
5+
/**
6+
* @param string $token Il token da utilizzare per il collegamento
7+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
8+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
9+
*/
10+
function __construct(string $token, array $scopes, object $cache, string $prefix){
11+
parent::__construct($token, $scopes, $cache, $prefix);
12+
$this->basePath = "https://comuni.openapi.it";
13+
}
14+
15+
16+
/**
17+
*
18+
* A partire dal CAP restistuisce un'array di oggietti di tipo comune
19+
*
20+
* @param string $cap Il cap da ricercare
21+
* @param int $ttl Il tempo di chache degli oggetti ritornati, 0 per no chche
22+
*
23+
* @return array
24+
*/
25+
function getCitiesByCap(string $cap, $ttl = 86400){
26+
$data = $this->connect("cap/$cap", "GET", [], $ttl);
27+
return $data->data;
28+
}
29+
30+
/**
31+
* Restituisce la lista delle regioni italiani
32+
*
33+
* @return array
34+
*/
35+
function getRegioni($ttl = 86400){
36+
$data = $this->connect("regioni", "GET", [], $ttl);
37+
$regioni = $data->data;
38+
sort($regioni);
39+
return $regioni;
40+
}
41+
42+
/**
43+
* Restituisce la lsita delle province italiane (a partire dalla regione)
44+
* @param string $regione La regione per la quale recuperare le regioni
45+
*
46+
* @return array
47+
*/
48+
function getProvince($regione = NULL, $ttl = 86400){
49+
if($regione == NULL){
50+
$data = $this->connect("province", "GET", [], $ttl);
51+
$province = $data->data;
52+
53+
$_province = [];
54+
foreach($province as $key => $p){
55+
$provincia = new \stdClass();
56+
$provincia->nome_provincia = $p;
57+
$provincia->sigla_provincia = $key;
58+
$_province[] = $provincia;
59+
}
60+
61+
usort($_province,[$this, 'sortProcince']);
62+
return $_province;
63+
}
64+
$regione = explode("/",$regione)[0];
65+
$regione = trim(\strtolower($regione));
66+
$regione = urlencode($regione);
67+
68+
$data = $this->connect("regioni/$regione", "GET", [], $ttl);
69+
70+
71+
$province = $data->data;
72+
usort($province,[$this, 'sortProcince']);
73+
return $province;
74+
}
75+
76+
function getComuni($provincia, $ttl = 86400){
77+
78+
$provincia = trim(\strtolower($provincia));
79+
$data = $this->connect("province/$provincia", "GET", [], $ttl);
80+
81+
82+
$comuni = $data->data;
83+
//sort($comuni->comuni);
84+
//usort($comuni->dettaglio_comuni,[$this, 'sortComune']);
85+
return $comuni;
86+
87+
}
88+
89+
private function sortComune($a, $b){
90+
if($a->nome == $b->nome){
91+
return 0;
92+
}
93+
return $a->nome < $b->nome ? -1 : 1;
94+
}
95+
96+
private function sortProcince($a, $b){
97+
if($a->nome_provincia == $b->nome_provincia){
98+
return 0;
99+
}
100+
return $a->nome_provincia < $b->nome_provincia ? -1 : 1;
101+
}
102+
103+
}

classes/Geocoding.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class Geocoding extends OpenApiBase {
4+
5+
/**
6+
* @param string $token Il token da utilizzare per il collegamento
7+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
8+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
9+
*/
10+
function __construct(string $token, array $scopes, object $cache, string $prefix){
11+
parent::__construct($token, $scopes, $cache, $prefix);
12+
$this->basePath = "https://geocoding.realgest.it";
13+
}
14+
15+
function geocode(string $address, $ttl = 86400){
16+
$data = $this->connect("geocode", "POST", ["address" => $address], $ttl, TRUE);
17+
18+
return $data;
19+
}
20+
21+
}

classes/Imprese.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,57 @@ function getByPartitaIva(string $partitaIva, $ttr = 86400){
3838

3939
}
4040

41+
function getClosed(string $partitaIva, $ttr = 86400){
42+
$partitaIva = trim($partitaIva);
43+
try{
44+
$data = $this->connect("closed/$partitaIva", "GET", [], $ttr);
45+
return $data->data;
46+
}catch (\OpenApi\classes\exception\OpenApiConnectionsException $e){
47+
48+
if($e->getHTTPCode() == 404){
49+
return null;
50+
}
51+
throw $e;
52+
53+
54+
exit;
55+
}
56+
}
57+
58+
function getVatGroup(string $partitaIva, $ttr = 86400){
59+
$partitaIva = trim($partitaIva);
60+
try{
61+
$data = $this->connect("gruppoIva/$partitaIva", "GET", [], $ttr);
62+
return $data->data;
63+
}catch (\OpenApi\classes\exception\OpenApiConnectionsException $e){
64+
65+
if($e->getHTTPCode() == 404){
66+
return null;
67+
}
68+
throw $e;
69+
70+
71+
exit;
72+
}
73+
}
74+
75+
function getPec(string $partitaIva, $ttr = 86400){
76+
$partitaIva = trim($partitaIva);
77+
try{
78+
$data = $this->connect("pec/$partitaIva", "GET", [], $ttr);
79+
return $data->data;
80+
}catch (\OpenApi\classes\exception\OpenApiConnectionsException $e){
81+
82+
if($e->getHTTPCode() == 404){
83+
return null;
84+
}
85+
throw $e;
86+
87+
88+
exit;
89+
}
90+
}
91+
4192
/**
4293
*
4394
* Cerca un'azienda o più utilizzando vari parametri
@@ -53,7 +104,7 @@ function getByPartitaIva(string $partitaIva, $ttr = 86400){
53104
function getBySearch(string $denominazione, string $provincia, $partitaIva= NULL , $codiceFiscale=NULL, $ttr = 86400){
54105
$params=[];
55106
if($denominazione != NULL){
56-
$params['denominazione'] = $denominazione;
107+
$params['denominazione'] = trim($denominazione);
57108
}
58109
if($provincia != NULL){
59110
$params['provincia'] = $provincia;

classes/MarcheTemporali.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class MarcheTemporali extends OpenApiBase {
4+
5+
/**
6+
* @param string $token Il token da utilizzare per il collegamento
7+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
8+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
9+
*/
10+
function __construct(string $token, array $scopes, object $cache, string $prefix){
11+
parent::__construct($token, $scopes, $cache, $prefix);
12+
$this->basePath = "https://ws.marchetemporali.com";
13+
}
14+
15+
function availability(string $type, int $qty){
16+
$data = $this->connect("availability/$type/$qty", "GET", []);
17+
return $data->data;
18+
}
19+
20+
function checkLotto($username, $password){
21+
22+
if(substr($username,0,4) == "FAKE" && substr($password,0,4) == "FAKE"){
23+
$ret = new \stdClass();
24+
$ret->data = new \stdClass();
25+
$ret->data->available = 10;
26+
$ret->data->used = 90;
27+
$ret->message = "DESCR= Marche per $username; disponibili 10 consumate 0";
28+
$ret->success = TRUE;
29+
$ret->error = NULL;
30+
return $ret->data;
31+
}
32+
$data = $this->connect("check_lotto", "POST", ["username"=>$username, "password"=> $password]);
33+
34+
return $data->data;
35+
}
36+
37+
function purcahse(string $type, int $qty){
38+
$data = $this->availability($type, $qty);
39+
//var_dump($data);exit;
40+
if($data->availability == 0){
41+
throw new \OpenApi\classes\exception\OpenApiMarcheTemporaliException("$qty $type time stamps is not availabile for purchase",40009);
42+
}
43+
//echo "marche/$type/$qty";exit;
44+
$data = $this->connect("marche/$type/$qty", "GET", []);
45+
//var_dump($data);exit;
46+
return $data->data;
47+
}
48+
49+
50+
51+
}

classes/OpenApiBase.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ private function checkHasScope(string $url, string $type){
8989
*
9090
* @return mixed
9191
*/
92-
protected function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0){
92+
protected function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0, $force = false){
9393
$url = $this->basePath;
9494
$url = str_replace("https://","https://".$this->prefix,$url);
9595
$url = str_replace("http://","http://".$this->prefix,$url);
9696
$url .= "/".$endpoint;
97-
$this->checkHasScope($url, $type);
97+
if(!$force){
98+
$this->checkHasScope($url, $type);
99+
}
100+
98101
if($type == "GET" && $ttr > 0 && $ret = $this->getCacheObject($url)) {
99102
return $ret;
100103
}
@@ -108,11 +111,7 @@ protected function connect(string $endpoint, $type = "GET", $param = [], $ttr =
108111
$url .= "?".$param;
109112

110113
}else{
111-
if($type == "PUT") {
112-
$param = json_encode($param);
113-
}else{
114-
$param = json_encode($param);
115-
}
114+
$param = json_encode($param);
116115
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
117116
}
118117
}
@@ -122,6 +121,7 @@ protected function connect(string $endpoint, $type = "GET", $param = [], $ttr =
122121
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Authorization: Bearer ".$this->token));
123122
curl_setopt($ch, CURLOPT_HEADER, 1);
124123
$response = curl_exec($ch);
124+
//var_dump($response);exit;
125125
$this->rawResponse = $response;
126126
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
127127
$this->header = substr($response, 0, $header_size);

classes/UfficioPostale.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ function __construct(string $token, array $scopes, object $cache, string $prefi
1313
}
1414

1515

16-
/**
17-
*
18-
* A partire dal CAP restistuisce un'array di oggietti di tipo comune
19-
*
20-
* @param string $cap Il cap da ricercare
21-
* @param int $ttl Il tempo di chache degli oggetti ritornati, 0 per no chche
22-
*
23-
* @return array
24-
*/
25-
function getCitiesByCap(string $cap, $ttl = 86400){
26-
$data = $this->connect("comuni/$cap", "GET", [], $ttl);
27-
return $data->data;
28-
}
16+
2917

3018
}

0 commit comments

Comments
 (0)