Success::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Rest Response
5
 *
6
 * @link        https://github.com/MelquesPaiva/rest-response for the canonical source repository
7
 * @copyright   Copyright (c) MelquesPaiva
8
 */
9
10
namespace MelquesPaiva\RestResponse\HttpResponse;
11
12
/**
13
 * Class Success
14
 * @package MelquesPaiva\RestResponse\HttpResponse
15
 */
16
class Success extends HttpResponse
17
{
18
    /**
19
     * The request has succeeded
20
     * 
21
     * @param string $message
22
     * @param array|null $data
23
     * @return Success
24
     */
25
    public function success(string $message, ?array $data = null): Success
26
    {
27
        $this->statusCode = 200;
28
        $this->message = $message;
29
        $this->data = $data;
30
31
        return $this;
32
    }
33
34
    /**
35
     * The server does not need to return an entity body
36
     *
37
     * @return Success
38
     */
39
    public function noContent(): Success
40
    {
41
        $this->statusCode = 204;
42
        $this->message = "";
43
        $this->data = [];
44
        $this->type = "";
45
        $this->parameter = "";
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $nameDataParam
52
     * @return array
53
     */
54
    public function toArray(string $nameDataParam = "data"): array
55
    {
56
        return [
57
            "message" => $this->message,
58
            $nameDataParam => $this->data,
59
        ];
60
    }
61
}
62