|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LPTracker\models; |
|
4
|
|
|
|
|
5
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
|
6
|
|
|
|
|
7
|
|
|
class Comment extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var integer |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $id; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $text; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var integer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $ownerId; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \DateTime |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $createdAt; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(array $commentData = []) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!empty($commentData['id'])) { |
|
32
|
|
|
$this->id = (int) $commentData['id']; |
|
33
|
|
|
} |
|
34
|
|
|
if (!empty($commentData['text'])) { |
|
35
|
|
|
$this->text = $commentData['text']; |
|
36
|
|
|
} |
|
37
|
|
|
if (!empty($commentData['author'])) { |
|
38
|
|
|
if ($commentData['author']['type'] === 'main') { |
|
39
|
|
|
$this->ownerId = 0; |
|
40
|
|
|
} else { |
|
41
|
|
|
$this->ownerId = (int) $commentData['author']['id']; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
if (!empty($commentData['created_at'])) { |
|
45
|
|
|
$this->setCreatedAt($commentData['created_at']); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function toArray() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
'id' => $this->getId(), |
|
56
|
|
|
'text' => $this->getText(), |
|
57
|
|
|
'owner' => $this->getOwnerId(), |
|
58
|
|
|
'created_at' => $this->getCreatedAt()->format('d.m.Y H:i:s'), |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws LPTrackerSDKException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function validate() |
|
67
|
|
|
{ |
|
68
|
|
|
if (empty($this->text)) { |
|
69
|
|
|
throw new LPTrackerSDKException('Text is required'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getId() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->id; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getText() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->text; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $text |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setText($text) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->text = $text; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getOwnerId() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->ownerId; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return \DateTime |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getCreatedAt() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->createdAt; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param \DateTime|string $createdAt |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setCreatedAt($createdAt) |
|
122
|
|
|
{ |
|
123
|
|
|
if (!$createdAt instanceof \DateTime) { |
|
124
|
|
|
$createdAt = \DateTime::createFromFormat('d.m.Y H:i:s', $createdAt); |
|
125
|
|
|
} |
|
126
|
|
|
$this->createdAt = $createdAt; |
|
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.