Passed
Push — master ( 151cc0...3583a4 )
by Romain
49s
created

Persona::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
9
class Persona implements \JsonSerializable
10
{
11
    use ValidatorTrait;
12
13
    /**
14
     * @var string|null
15
     */
16
    protected $id;
17
18
    /**
19
     * @var string|null
20
     */
21
    protected $name;
22
23
    /**
24
     * @var string|null
25
     */
26
    protected $profile_picture_url;
27
28
    /**
29
     * Persona constructor.
30
     *
31
     * @param array $data
32
     */
33 13
    public function __construct(array $data = [])
34
    {
35 13
        foreach ($data as $field => $value) {
36 10
            if (property_exists($this, $field)) {
37 10
                $this->{$field} = $value;
38
            }
39
        }
40 13
    }
41
42
    /**
43
     * @param array $data
44
     *
45
     * @return Persona
46
     */
47 2
    public static function create(array $data = []): self
48
    {
49 2
        return new static($data);
50
    }
51
52
    /**
53
     * @param string $id
54
     *
55
     * @return Persona
56
     */
57 1
    public function setId(string $id): self
58
    {
59 1
        $this->id = $id;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @return Persona
68
     */
69 2
    public function setName(string $name): self
70
    {
71 2
        $this->name = $name;
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * @param string $url
78
     *
79
     * @return Persona
80
     */
81 2
    public function setProfilePictureUrl(string $url): self
82
    {
83 2
        $this->isValidUrl($url);
84
85 2
        $this->profile_picture_url = $url;
86
87 2
        return $this;
88
    }
89
90
    /**
91
     * @return string|null
92
     */
93 3
    public function getId(): ?string
94
    {
95 3
        return $this->id;
96
    }
97
98
    /**
99
     * @return string|null
100
     */
101 2
    public function getName(): ?string
102
    {
103 2
        return $this->name;
104
    }
105
106
    /**
107
     * @return string|null
108
     */
109 2
    public function getProfilePictureUrl(): ?string
110
    {
111 2
        return $this->profile_picture_url;
112
    }
113
114
    /**
115
     * @return array
116
     */
117 2
    public function toArray(): array
118
    {
119
        $array = [
120 2
            'id' => $this->id,
121 2
            'name' => $this->name,
122 2
            'profile_picture_url' => $this->profile_picture_url,
123
        ];
124
125 2
        return \array_filter($array);
126
    }
127
128
    /**
129
     * @return array
130
     */
131 1
    public function jsonSerialize(): array
132
    {
133 1
        return $this->toArray();
134
    }
135
}
136