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\Zend; |
14
|
|
|
|
15
|
|
|
use Soluble\FlexStore\Writer\Http\SimpleHeaders; |
16
|
|
|
use Zend\Json\Encoder; |
17
|
|
|
use Soluble\FlexStore\Writer\AbstractSendableWriter; |
18
|
|
|
use Soluble\FlexStore\Source\QueryableSourceInterface; |
19
|
|
|
use Soluble\FlexStore\Options; |
20
|
|
|
|
21
|
|
|
class JsonWriter extends AbstractSendableWriter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SimpleHeaders |
25
|
|
|
*/ |
26
|
|
|
protected $headers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Options $options |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
2 |
|
public function getData(Options $options = null) |
34
|
|
|
{ |
35
|
2 |
|
if ($options === null) { |
36
|
|
|
// Take store global/default options |
37
|
2 |
|
$options = $this->store->getOptions(); |
38
|
|
|
// By default formatters are disabled in JSON |
39
|
2 |
|
$options->getHydrationOptions()->disableFormatters(); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
$data = $this->store->getData($options); |
43
|
|
|
$d = [ |
44
|
2 |
|
'success' => true, |
45
|
2 |
|
'total' => $data->getTotalRows(), |
46
|
2 |
|
'start' => $data->getSource()->getOptions()->getOffset(), |
47
|
2 |
|
'limit' => $data->getSource()->getOptions()->getLimit(), |
48
|
2 |
|
'data' => $data->toArray() |
49
|
|
|
]; |
50
|
|
|
|
51
|
2 |
|
if ($this->options['debug']) { |
52
|
1 |
|
$source = $data->getSource(); |
53
|
1 |
|
if ($source instanceof QueryableSourceInterface) { |
54
|
1 |
|
$d['query'] = $source->getQueryString(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return Encoder::encode($d); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return default headers for sending store data via http. |
63
|
|
|
* |
64
|
|
|
* @return SimpleHeaders |
65
|
|
|
*/ |
66
|
|
|
public function getHttpHeaders() |
67
|
|
|
{ |
68
|
|
|
if ($this->headers === null) { |
69
|
|
|
$this->headers = new SimpleHeaders(); |
70
|
|
|
$this->headers->setContentType('application/json', 'utf-8'); |
71
|
|
|
//$this->headers->setContentDispositionType(SimpleHeaders::DIPOSITION_ATTACHEMENT); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->headers; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|