1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Service\Serializer\SerializerBuilder; |
17
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
18
|
|
|
use Ivory\Serializer\Format; |
19
|
|
|
use Ivory\Serializer\SerializerInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractSerializableService extends AbstractHttpService |
26
|
|
|
{ |
27
|
|
|
const FORMAT_JSON = Format::JSON; |
28
|
|
|
const FORMAT_XML = Format::XML; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SerializerInterface |
32
|
|
|
*/ |
33
|
|
|
private $serializer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $format = self::FORMAT_JSON; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $url |
42
|
|
|
* @param HttpClient $client |
43
|
|
|
* @param MessageFactory $messageFactory |
44
|
|
|
* @param SerializerInterface|null $serializer |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
$url, |
48
|
|
|
HttpClient $client, |
49
|
|
|
MessageFactory $messageFactory, |
50
|
|
|
SerializerInterface $serializer = null |
51
|
|
|
) { |
52
|
|
|
parent::__construct($url, $client, $messageFactory); |
53
|
|
|
|
54
|
|
|
$this->setSerializer($serializer ?: SerializerBuilder::create()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return SerializerInterface |
59
|
|
|
*/ |
60
|
|
|
public function getSerializer() |
61
|
|
|
{ |
62
|
|
|
return $this->serializer; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SerializerInterface $serializer |
67
|
|
|
*/ |
68
|
|
|
public function setSerializer(SerializerInterface $serializer) |
69
|
|
|
{ |
70
|
|
|
$this->serializer = $serializer; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getFormat() |
77
|
|
|
{ |
78
|
|
|
return $this->format; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $format |
83
|
|
|
*/ |
84
|
|
|
public function setFormat($format) |
85
|
|
|
{ |
86
|
|
|
$this->format = $format; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
protected function createBaseUrl(RequestInterface $request) |
93
|
|
|
{ |
94
|
|
|
return parent::createBaseUrl($request).'/'.$this->format; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param ResponseInterface $response |
99
|
|
|
* @param string $type |
100
|
|
|
* @param ContextInterface|null $context |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
protected function deserialize(ResponseInterface $response, $type, ContextInterface $context = null) |
105
|
|
|
{ |
106
|
|
|
return $this->serializer->deserialize((string) $response->getBody(), $type, $this->format, $context); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|