Repository navigation
Expand file tree
/
Copy pathJsonpResponseFormat.php
More file actions
75 lines (66 loc) · 1.5 KB
/
Copy pathJsonpResponseFormat.php
File metadata and controls
75 lines (66 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Dingo\Api\Http\ResponseFormat;
class JsonpResponseFormat extends JsonResponseFormat
{
/**
* Name of JSONP callback paramater.
*
* @var string
*/
protected $callbackName = 'callback';
/**
* Create a new JSONP response formatter instance.
*
* @param string $callbackName
*
* @return void
*/
public function __construct($callbackName = 'callback')
{
$this->callbackName = $callbackName;
}
/**
* Determine if a callback is valid.
*
* @return bool
*/
protected function hasValidCallback()
{
return $this->request->query->has($this->callbackName);
}
/**
* Get the callback from the query string.
*
* @return string
*/
protected function getCallback()
{
return $this->request->query->get($this->callbackName);
}
/**
* Get the response content type.
*
* @return string
*/
public function getContentType()
{
if ($this->hasValidCallback()) {
return 'application/javascript';
}
return parent::getContentType();
}
/**
* Encode the content to its JSON representation.
*
* @param string $content
*
* @return string
*/
protected function encode($content)
{
if ($this->hasValidCallback()) {
return sprintf('%s(%s);', $this->getCallback(), json_encode($content));
}
return parent::encode($content);
}
}