|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* soluble-flexstore library |
|
5
|
|
|
* |
|
6
|
|
|
* @author Vanvelthem Sébastien |
|
7
|
|
|
* @link https://github.com/belgattitude/soluble-flexstore |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien |
|
9
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\FlexStore\Writer; |
|
14
|
|
|
|
|
15
|
|
|
use Soluble\FlexStore\Source\QueryableSourceInterface; |
|
16
|
|
|
use Soluble\FlexStore\Writer\Http\SimpleHeaders; |
|
17
|
|
|
use DateTime; |
|
18
|
|
|
use Soluble\FlexStore\Options; |
|
19
|
|
|
|
|
20
|
|
|
class JsonWriter extends AbstractSendableWriter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var SimpleHeaders |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $headers; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int|string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $request_id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set origin request id. |
|
34
|
|
|
* |
|
35
|
|
|
* Value of request id will be returned in json encoded data |
|
36
|
|
|
* useful for autocompletion usage when synchronous requests |
|
37
|
|
|
* will return asynchronous responses. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int|string $request_id |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function setRequestId($request_id) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->request_id = $request_id; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Options $options |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public function getData(Options $options = null) |
|
52
|
|
|
{ |
|
53
|
5 |
|
if ($options === null) { |
|
54
|
|
|
// Take store global/default options |
|
55
|
5 |
|
$options = $this->store->getOptions(); |
|
56
|
|
|
// By default formatters are disabled in JSON format |
|
57
|
5 |
|
$options->getHydrationOptions()->disableFormatters(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Get unformatted data when using json |
|
61
|
5 |
|
$options->getHydrationOptions()->disableFormatters(); |
|
62
|
|
|
|
|
63
|
5 |
|
$data = $this->store->getData($options); |
|
64
|
5 |
|
$now = new DateTime(); |
|
65
|
|
|
|
|
66
|
|
|
$d = [ |
|
67
|
5 |
|
'success' => true, |
|
68
|
5 |
|
'timestamp' => $now->format(DateTime::W3C), |
|
69
|
5 |
|
'total' => $data->getTotalRows(), |
|
70
|
5 |
|
'request_id' => $this->request_id, |
|
71
|
5 |
|
'start' => $data->getSource()->getOptions()->getOffset(), |
|
72
|
5 |
|
'limit' => $data->getSource()->getOptions()->getLimit(), |
|
73
|
5 |
|
'data' => $data->toArray() |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
5 |
|
if ($this->options['debug']) { |
|
77
|
1 |
|
$source = $data->getSource(); |
|
78
|
1 |
|
if ($source instanceof QueryableSourceInterface) { |
|
79
|
1 |
|
$d['query'] = $source->getQueryString(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
return json_encode($d); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return default headers for sending store data via http. |
|
88
|
|
|
* |
|
89
|
|
|
* @return SimpleHeaders |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getHttpHeaders() |
|
92
|
|
|
{ |
|
93
|
1 |
|
if ($this->headers === null) { |
|
94
|
1 |
|
$this->headers = new SimpleHeaders(); |
|
95
|
1 |
|
$this->headers->setContentType('application/json', 'utf-8'); |
|
96
|
|
|
//$this->headers->setContentDispositionType(SimpleHeaders::DIPOSITION_ATTACHEMENT); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $this->headers; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|