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