|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of :project_name |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package WilliamEspindola\AbstractHTTPClient |
|
9
|
|
|
* @author William Espindola <[email protected]> |
|
10
|
|
|
* @copyright Free |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
* @link https://github.com/williamespindola/abstract-http-client |
|
13
|
|
|
*/ |
|
14
|
|
|
declare(strict_types=1); |
|
15
|
|
|
|
|
16
|
|
|
namespace WilliamEspindola\AbstractHTTPClient; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
use WilliamEspindola\AbstractHTTPClient\HTTPClient; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstracts request resources |
|
24
|
|
|
* |
|
25
|
|
|
* @category PHP |
|
26
|
|
|
* @package WilliamEspindola\AbstractHTTPClient |
|
27
|
|
|
* @author William Espindola <[email protected]> |
|
28
|
|
|
* @copyright Free |
|
29
|
|
|
* @license MIT |
|
30
|
|
|
* @version Release: 1.0.0 |
|
31
|
|
|
* @link https://github.com/williamespindola/abstract-http-client |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class AbstractRequest |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $config; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Client $client Cliente implementation |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $client; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string $baseUrl Base API URL |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $baseUrl; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string $uri String that identifies resource |
|
52
|
|
|
*/ |
|
53
|
|
|
private $uri; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Initializes new AbstractNuxeoIntegration |
|
57
|
|
|
* |
|
58
|
|
|
* @param HTTPClient $client HTTPClient implementation |
|
59
|
|
|
* @param string $baseUrl Base API URL |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(HTTPClient $client, string $baseUrl) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->baseUrl = $baseUrl; |
|
64
|
|
|
|
|
65
|
|
|
$this->client = $client; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get URI concatenating base url api with resource end point |
|
70
|
|
|
* |
|
71
|
|
|
* @return string URI end point |
|
72
|
|
|
* |
|
73
|
|
|
* @throws Exception If AbstractRequest::baseUrl or AbstractRequest::uri was empty |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getURI(): string |
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($this->baseUrl) || empty($this->uri)) { |
|
78
|
|
|
throw new Exception('Url or end point can not be null'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->baseUrl . $this->uri; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set url parameters |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $parameters array with parameters that must be setted |
|
88
|
|
|
* |
|
89
|
|
|
* <code> |
|
90
|
|
|
* $this->setParameters([ |
|
91
|
|
|
* ':paramkey' => 'my param value' |
|
92
|
|
|
* ]); |
|
93
|
|
|
* </code> |
|
94
|
|
|
* |
|
95
|
|
|
* @throws InvalidArgumentException If parameters param was empty |
|
96
|
|
|
* @throws Exception If parametes not match with request endPoint |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function setParameters(array $parameters) |
|
101
|
|
|
{ |
|
102
|
|
|
if (empty($parameters)) { |
|
103
|
|
|
throw new InvalidArgumentException('Parameters can not be null'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->uri = null; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($parameters as $key => $param) { |
|
109
|
|
|
if (preg_match("/{$key}/i", $this->endPoint)) { |
|
110
|
|
|
if (is_null($this->uri)) { |
|
111
|
|
|
$this->uri = $this->endPoint; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->uri = str_replace($key, $param, $this->uri); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (empty($this->uri)) { |
|
119
|
|
|
throw new Exception( |
|
120
|
|
|
'Parameters definition not matches with request end point' |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..