Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit cb3bda8

Browse files
author
Andrey Helldar
committed
content renamed to data
1 parent e29fdf3 commit cb3bda8

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ returned with code 400:
116116

117117
### returned array:
118118
```php
119-
$content = [
119+
$data = [
120120
[
121121
'title' => 'Title #1',
122122
'description' => 'Description #1',
@@ -130,7 +130,7 @@ $content = [
130130

131131
#### as error
132132
```php
133-
return api_response($content, 400);
133+
return api_response($data, 400);
134134
```
135135
returned with code 400:
136136
```json
@@ -153,7 +153,7 @@ returned with code 400:
153153

154154
#### as success
155155
```php
156-
return api_response($content, 200);
156+
return api_response($data, 200);
157157
```
158158
returned with code 200:
159159
```json

src/Services/ResponseService.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ResponseService
1212
protected $with = [];
1313

1414
/** @var null|string|int|array|object */
15-
protected $content = null;
15+
protected $data = null;
1616

1717
/** @var array */
1818
protected $headers = [];
@@ -41,15 +41,15 @@ public function status(int $status = 200): self
4141
}
4242

4343
/**
44-
* @param mixed $content
44+
* @param mixed $data
4545
*
4646
* @return $this
4747
*/
48-
public function content($content = null): self
48+
public function data($data = null): self
4949
{
50-
$this->content = $content instanceof Responsable
51-
? $this->toResponse($content)
52-
: $content;
50+
$this->data = $data instanceof Responsable
51+
? $this->toResponse($data)
52+
: $data;
5353

5454
return $this;
5555
}
@@ -83,7 +83,7 @@ public function headers(array $headers = []): self
8383
*/
8484
public function response(): JsonResponse
8585
{
86-
return JsonResponse::create($this->getContent(), $this->status_code, $this->headers);
86+
return JsonResponse::create($this->getData(), $this->status_code, $this->headers);
8787
}
8888

8989
protected function isError(): bool
@@ -93,7 +93,7 @@ protected function isError(): bool
9393

9494
private function e($value = null, $doubleEncode = true)
9595
{
96-
if (!is_string($value) || null === $value) {
96+
if (! is_string($value) || null === $value) {
9797
return $value;
9898
}
9999

@@ -102,37 +102,37 @@ private function e($value = null, $doubleEncode = true)
102102
: null;
103103
}
104104

105-
private function getContent()
105+
private function getData()
106106
{
107-
$content = $this->isError()
108-
? $this->getErrorContent()
109-
: $this->getSuccessContent();
107+
$data = $this->isError()
108+
? $this->getErrorData()
109+
: $this->getSuccessData();
110110

111-
return $this->mergeWith($content);
111+
return $this->mergeDataWith($data);
112112
}
113113

114-
private function getErrorContent(): array
114+
private function getErrorData(): array
115115
{
116116
return [
117117
'error' => [
118118
'code' => $this->status_code,
119-
'data' => $this->e($this->content),
119+
'data' => $this->e($this->data),
120120
],
121121
];
122122
}
123123

124-
private function getSuccessContent()
124+
private function getSuccessData()
125125
{
126126
return [
127-
'data' => $this->e($this->content),
127+
'data' => $this->e($this->data),
128128
];
129129
}
130130

131-
private function mergeWith(array $content = []): array
131+
private function mergeDataWith(array $data = []): array
132132
{
133133
return $this->with
134-
? array_merge($content, $this->with)
135-
: $content;
134+
? array_merge($data, $this->with)
135+
: $data;
136136
}
137137

138138
private function toResponse(Responsable $content)

src/helpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
/**
77
* Return a new response from the application.
88
*
9-
* @param null|string|int|array|object $content
9+
* @param null|mixed $data
1010
* @param int $status_code
1111
* @param array $headers
1212
* @param array $with
1313
*
1414
* @return JsonResponse
1515
*/
16-
function api_response($content = null, $status_code = 200, $headers = [], array $with = [])
16+
function api_response($data = null, int $status_code = 200, array $headers = [], array $with = [])
1717
{
1818
return ResponseService::init()
1919
->headers($headers)
20-
->content($content)
20+
->data($data)
2121
->with($with)
2222
->status($status_code)
2323
->response();

tests/ResponseServiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testEmpty()
2929
$this->assertEquals(json_encode(['error' => ['code' => 500, 'data' => null]]), api_response('', 500)->getContent());
3030
}
3131

32-
public function testContent()
32+
public function testData()
3333
{
3434
$this->assertJson(api_response('ok')->getContent());
3535
$this->assertJson(api_response('fail', 400)->getContent());
@@ -45,7 +45,7 @@ public function testStructure()
4545
$this->assertJsonStringNotEqualsJsonString(json_encode(['data' => 'ok']), api_response('fail', 400)->getContent());
4646
}
4747

48-
public function testWithContent()
48+
public function testDataWith()
4949
{
5050
$this->assertJson(api_response('ok', 200, [], ['foo' => 'bar'])->getContent());
5151
$this->assertJson(api_response('fail', 400, [], ['foo' => 'bar'])->getContent());
@@ -64,7 +64,7 @@ public function testWithContent()
6464
);
6565
}
6666

67-
public function testNumberContent()
67+
public function testNumber()
6868
{
6969
$this->assertEquals(json_encode(['data' => 304]), api_response(304)->getContent());
7070
$this->assertEquals(json_encode(['error' => ['code' => 400, 'data' => 304]]), api_response(304, 400)->getContent());

0 commit comments

Comments
 (0)