PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 3.21.1
GiveWP – Donation Plugin and Fundraising Platform v3.21.1
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Framework / Http / Response / Response.php
Response.php
85 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\Http\Response;
4
5 use ArrayObject;
6 use Give\Framework\Http\Response\Traits\ResponseTrait;
7 use Give\Framework\Support\Contracts\Arrayable;
8 use Give\Framework\Support\Contracts\Jsonable;
9 use Give\Framework\Support\Contracts\Renderable;
10 use JsonSerializable;
11 use Give\Vendors\Symfony\Component\HttpFoundation\Response as SymfonyResponse;
12
13 /**
14 * @since 2.18.0
15 */
16 class Response extends SymfonyResponse
17 {
18 use ResponseTrait;
19
20 /**
21 * Set the content on the response.
22 *
23 * @param mixed $content
24 * @return $this
25 */
26 public function setContent($content)
27 {
28 $this->original = $content;
29
30 // If the content is "JSONable" we will set the appropriate header and convert
31 // the content to JSON. This is useful when returning something like models
32 // from routes that will be automatically transformed to their JSON form.
33 if ($this->shouldBeJson($content)) {
34 $this->header('Content-Type', 'application/json');
35
36 $content = $this->morphToJson($content);
37 }
38
39 // If this content implements the "Renderable" interface then we will call the
40 // render method on the object so we will avoid any "__toString" exceptions
41 // that might be thrown and have their errors obscured by PHP's handling.
42 elseif ($content instanceof Renderable) {
43 $content = $content->render();
44 }
45
46 parent::setContent($content);
47
48 return $this;
49 }
50
51 /**
52 * Determine if the given content should be turned into JSON.
53 *
54 * @param mixed $content
55 * @return bool
56 */
57 protected function shouldBeJson($content)
58 {
59 return $content instanceof Arrayable ||
60 $content instanceof Jsonable ||
61 $content instanceof ArrayObject ||
62 $content instanceof JsonSerializable ||
63 is_array($content);
64 }
65
66 /**
67 * Morph the given content into JSON.
68 *
69 * @param mixed $content
70 * @return string
71 */
72 protected function morphToJson($content)
73 {
74 if ($content instanceof Jsonable) {
75 return $content->toJson();
76 }
77
78 if ($content instanceof Arrayable) {
79 return json_encode($content->toArray());
80 }
81
82 return json_encode($content);
83 }
84 }
85