Passed
Push — master ( f5e35b...7b483a )
by Jacobo
07:40 queued 04:57
created

SignalProperties::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SquareetLabs\LaravelOpenVidu;
4
5
use JsonSerializable;
6
7
/**
8
 * Class SignalProperties
9
 * @package SquareetLabs\LaravelOpenVidu
10
 */
11
class SignalProperties implements JsonSerializable
12
{
13
    /** @var  string */
14
    private $session;
15
    /** @var  string */
16
    private $to;
17
    /** @var  string */
18
    private $type;
19
    /** @var  string */
20
    private $data;
21
22
    /**
23
     * SignalProperties constructor.
24
     * @param string $session
25
     * @param string|null $data
26
     * @param string|null $type
27
     * @param string|null $to
28
     */
29
    public function __construct(string $session, ?string $data = null, ?string $type = null, ?string $to = null)
30
    {
31
        $this->session = $session;
32
        $this->data = $data;
33
        $this->type = $type;
34
        $this->to = $to;
35
    }
36
37
    /**
38
     * Session name of the recording
39
     *
40
     * @return string
41
     */
42
    public function session()
43
    {
44
        return $this->session;
45
    }
46
47
    /**
48
     * Convert the model instance to JSON.
49
     *
50
     * @param int $options
51
     * @return string
52
     *
53
     */
54
    public function toJson($options = 0): string
55
    {
56
        $json = json_encode($this->jsonSerialize(), $options);
57
        return $json;
58
    }
59
60
    /**
61
     * Specify data which should be serialized to JSON
62
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
63
     * @return mixed data which can be serialized by <b>json_encode</b>,
64
     * which is a value of any type other than a resource.
65
     * @since 5.4.0
66
     */
67
    public function jsonSerialize()
68
    {
69
        return $this->toArray();
70
    }
71
72
    /**
73
     * Convert the model instance to an array.
74
     *
75
     * @return array
76
     */
77
    public function toArray(): array
78
    {
79
        $array = [
80
            'session' => $this->session,
81
            'data' => $this->data,
82
            'type' => $this->type,
83
            'to' => $this->to
84
        ];
85
        foreach ($array as $key => $value) {
86
            if (is_null($value) || $value == '') {
87
                unset($array[$key]);
88
            }
89
        }
90
        return $array;
91
    }
92
}
93