1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\Localise; |
11
|
|
|
|
12
|
|
|
use FAPI\Localise\Api\Asset; |
13
|
|
|
use FAPI\Localise\Api\Translation; |
14
|
|
|
use FAPI\Localise\Hydrator\ModelHydrator; |
15
|
|
|
use FAPI\Localise\Hydrator\Hydrator; |
16
|
|
|
use Http\Client\HttpClient; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class LocoClient |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var HttpClient |
25
|
|
|
*/ |
26
|
|
|
private $httpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Hydrator |
30
|
|
|
*/ |
31
|
|
|
private $hydrator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RequestBuilder |
35
|
|
|
*/ |
36
|
|
|
private $requestBuilder; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The constructor accepts already configured HTTP clients. |
40
|
|
|
* Use the configure method to pass a configuration to the Client and create an HTTP Client. |
41
|
|
|
* |
42
|
|
|
* @param HttpClient $httpClient |
43
|
|
|
* @param Hydrator|null $hydrator |
44
|
|
|
* @param RequestBuilder|null $requestBuilder |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
HttpClient $httpClient, |
48
|
|
|
Hydrator $hydrator = null, |
49
|
|
|
RequestBuilder $requestBuilder = null |
50
|
|
|
) { |
51
|
|
|
$this->httpClient = $httpClient; |
52
|
|
|
$this->hydrator = $hydrator ?: new ModelHydrator(); |
53
|
|
|
$this->requestBuilder = $requestBuilder ?: new RequestBuilder(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param HttpClientConfigurator $httpClientConfigurator |
58
|
|
|
* @param Hydrator|null $hydrator |
59
|
|
|
* @param RequestBuilder|null $requestBuilder |
60
|
|
|
* |
61
|
|
|
* @return ApiClient |
62
|
|
|
*/ |
63
|
|
|
public static function configure( |
64
|
|
|
HttpClientConfigurator $httpClientConfigurator, |
65
|
|
|
Hydrator $hydrator = null, |
66
|
|
|
RequestBuilder $requestBuilder = null |
67
|
|
|
): self { |
68
|
|
|
$httpClient = $httpClientConfigurator->createConfiguredClient(); |
69
|
|
|
|
70
|
|
|
return new self($httpClient, $hydrator, $requestBuilder); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Api\Translation |
75
|
|
|
*/ |
76
|
|
|
public function translations(): Translation |
77
|
|
|
{ |
78
|
|
|
return new Api\Translation($this->httpClient, $this->hydrator, $this->requestBuilder); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Api\Asset |
83
|
|
|
*/ |
84
|
|
|
public function asset(): Asset |
85
|
|
|
{ |
86
|
|
|
return new Api\Asset($this->httpClient, $this->hydrator, $this->requestBuilder); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|