forked from alekseykuleshov/rocket-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.php
854 lines (746 loc) · 18.6 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
<?php
namespace ATDev\RocketChat\Users;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;
/**
* User data trait
*/
trait Data
{
/** @var string User id */
private $userId;
/* Required properties for creation */
/** @var string User email */
private $email;
/** @var string User display name */
private $name;
/** @var string User password */
private $password;
/** @var string Username of user */
private $username;
/* Optional properties for creation */
/** @var boolean Indicates if user is active, default true */
private $active;
/** @var array User roles, default ['user'] */
private $roles;
/** @var boolean Indicates if user should join default channels on creation, default true */
private $joinDefaultChannels;
/** @var boolean Indicates if user should change password on first login, default false */
private $requirePasswordChange;
/** @var boolean Indicates if user should receive welcome email, default false */
private $sendWelcomeEmail;
/** @var boolean Indicates if user email is verified, default false */
private $verified;
/** @var string User custom fields, default is undefined */
private $customFields;
/** @var string The new password for the user to update own info */
private $newPassword;
/* Other methods required properties */
/** @var string The user's status message */
private $statusText;
/* Readonly properties returned from api */
/** @var string Date-time user created at api */
private $createdAt;
/** @var string User type at the api */
private $type;
/** @var string User status */
private $status;
/** @var string Date-time user last logged in */
private $lastLogin;
/** @var string User status connection of the api */
private $statusConnection;
/** @var int|float User utc offset */
private $utcOffset;
/** @var string Avatar Url */
private $avatarUrl;
/** @var string Authentication token - the same type of session token a user would get via login */
private $authToken;
/** @var Preferences All preferences of the user */
private $preferences;
/**
* Creates user out of api response
*
* @param \stdClass $response
*
* @return \ATDev\RocketChat\Users\Data
*/
public static function createOutOfResponse($response)
{
$user = new static($response->_id);
return $user->updateOutOfResponse($response);
}
/**
* Class constructor
*
* @param string|null $userId
*/
public function __construct($userId = null)
{
if (!empty($userId)) {
$this->setUserId($userId);
}
}
/**
* Sets user id
*
* @param string $userId
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setUserId($userId)
{
if (!(is_null($userId) || is_string($userId))) {
$this->setDataError("Invalid user Id");
} else {
$this->userId = $userId;
}
return $this;
}
/**
* Gets user id
*
* @return string
*/
public function getUserId()
{
return $this->userId;
}
/**
* Sets user email
*
* @param string $email
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setEmail($email)
{
if (!(is_null($email) || is_string($email))) {
$this->setDataError("Invalid email");
} else {
if (!is_null($email)) {
$validator = new EmailValidator();
if (!$validator->isValid($email, new RFCValidation())) {
$this->setDataError("Invalid email value");
} else {
$this->email = $email;
}
} else {
$this->email = $email;
}
}
return $this;
}
/**
* Gets user email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Sets user display name
*
* @param string $name
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setName($name)
{
if (!(is_null($name) || is_string($name))) {
$this->setDataError("Invalid name");
} else {
$this->name = $name;
}
return $this;
}
/**
* Gets user display name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets user password
*
* @param string $password
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setPassword($password)
{
if (!(is_null($password) || is_string($password))) {
$this->setDataError("Invalid password");
} else {
$this->password = $password;
}
return $this;
}
/**
* Gets user password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* New password to update own info
*
* @param string $newPassword
* @return $this
*/
public function setNewPassword($newPassword)
{
if (!(is_null($newPassword) || is_string($newPassword))) {
$this->setDataError("Invalid new password");
} else {
$this->newPassword = $newPassword;
}
return $this;
}
/**
* Returns new password value
*
* @return string
*/
public function getNewPassword()
{
return $this->newPassword;
}
/**
* Sets user name
*
* @param string $username
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setUsername($username)
{
if (!(is_null($username) || is_string($username))) {
$this->setDataError("Invalid user name");
} else {
$this->username = $username;
}
return $this;
}
/**
* Gets user name
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Sets if user is active
*
* @param boolean $active
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setActive($active)
{
if (!(is_null($active) || is_bool($active))) {
$this->setDataError("Invalid active value");
} else {
$this->active = $active;
}
return $this;
}
/**
* Gets if user is active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Sets user roles
*
* @param array $roles
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setRoles($roles)
{
if (!(is_null($roles) || is_array($roles))) {
$this->setDataError("Invalid roles value");
} else {
$this->roles = $roles;
}
return $this;
}
/**
* Gets user roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* Sets if user should join default channels
*
* @param boolean $joinDefaultChannels
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setJoinDefaultChannels($joinDefaultChannels)
{
if (!(is_null($joinDefaultChannels) || is_bool($joinDefaultChannels))) {
$this->setDataError("Invalid join default channels value");
} else {
$this->joinDefaultChannels = $joinDefaultChannels;
}
return $this;
}
/**
* Gets if user should join default channels
*
* @return boolean
*/
public function getJoinDefaultChannels()
{
return $this->joinDefaultChannels;
}
/**
* Sets if user should change the password
*
* @param boolean $requirePasswordChange
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setRequirePasswordChange($requirePasswordChange)
{
if (!(is_null($requirePasswordChange) || is_bool($requirePasswordChange))) {
$this->setDataError("Invalid require password change value");
} else {
$this->requirePasswordChange = $requirePasswordChange;
}
return $this;
}
/**
* Gets if user should change the password
*
* @return boolean
*/
public function getRequirePasswordChange()
{
return $this->requirePasswordChange;
}
/**
* Sets if user should receive welcome email
*
* @param boolean $sendWelcomeEmail
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setSendWelcomeEmail($sendWelcomeEmail)
{
if (!(is_null($sendWelcomeEmail) || is_bool($sendWelcomeEmail))) {
$this->setDataError("Invalid send welcome email value");
} else {
$this->sendWelcomeEmail = $sendWelcomeEmail;
}
return $this;
}
/**
* Gets if user should receive welcome email
*
* @return boolean
*/
public function getSendWelcomeEmail()
{
return $this->sendWelcomeEmail;
}
/**
* Sets if user email should be verified
*
* @param boolean $verified
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setVerified($verified)
{
if (!(is_null($verified) || is_bool($verified))) {
$this->setDataError("Invalid verified value");
} else {
$this->verified = $verified;
}
return $this;
}
/**
* Gets if user email should be verified
*
* @return boolean
*/
public function getVerified()
{
return $this->verified;
}
/**
* Gets user status
*
* @return string
*/
public function getStatusValue()
{
return $this->status;
}
/**
* Sets user status like 'online', 'away', 'busy', 'offline'
*
* @param string $status
* @return $this
*/
public function setStatusValue($status)
{
if (!(is_null($status) || is_string($status))) {
$this->setDataError("Invalid status value");
} else {
$this->status = $status;
}
return $this;
}
/**
* The user's status message
*
* @return string
*/
public function getStatusText()
{
return $this->statusText;
}
/**
* Sets the user's status message
*
* @param string $statusText
* @return $this
*/
public function setStatusText($statusText)
{
if (!(is_null($statusText) || is_string($statusText))) {
$this->setDataError("Invalid status message value");
} else {
$this->statusText = $statusText;
}
return $this;
}
/**
* Sets user custom fields
*
* @param string $customFields
*
* @return \ATDev\RocketChat\Users\Data
*/
public function setCustomFields($customFields)
{
if (!(is_null($customFields) || is_string($customFields))) {
$this->setDataError("Invalid custom fields name");
} else {
$this->customFields = $customFields;
}
return $this;
}
/**
* Gets user custom fields
*
* @return string
*/
public function getCustomFields()
{
return $this->customFields;
}
/**
* Gets the date-time user created at api
*
* @return string
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Gets user type at the api
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Gets date-time user last logged in
*
* @return string
*/
public function getLastLogin()
{
return $this->lastLogin;
}
/**
* Gets user status connection of the api
*
* @return string
*/
public function getStatusConnection()
{
return $this->statusConnection;
}
/**
* Gets user utc offset
*
* @return int|float
*/
public function getUtcOffset()
{
return $this->utcOffset;
}
/**
* Gets user avatar url
*
* @return string
*/
public function getAvatarUrl()
{
return $this->avatarUrl;
}
/**
* Returns user authentication token received by createToken api request
*
* @return string
*/
public function getAuthToken()
{
return $this->authToken;
}
/**
* Returns user preferences
*
* @return Preferences
*/
public function getPreferencesData()
{
return $this->preferences;
}
/**
* Updates current user out of api response
*
* @param \stdClass $response
*
* @return \ATDev\RocketChat\Users\Data
*/
public function updateOutOfResponse($response)
{
if (isset($response->_id)) {
$this->setUserId($response->_id);
}
if (isset($response->createdAt)) {
$this->setCreatedAt($response->createdAt);
}
if (isset($response->emails[0]->address)) {
$this->setEmail($response->emails[0]->address);
}
if (isset($response->emails[0]->verified)) {
$this->setVerified($response->emails[0]->verified);
}
if (isset($response->type)) {
$this->setType($response->type);
}
if (isset($response->status)) {
$this->setStatusValue($response->status);
}
if (isset($response->statusText)) {
$this->setStatusText($response->statusText);
}
if (isset($response->message)) {
$this->setStatusText($response->message);
}
if (isset($response->active)) {
$this->setActive($response->active);
}
if (isset($response->roles)) {
$this->setRoles($response->roles);
}
if (isset($response->name)) {
$this->setName($response->name);
}
if (isset($response->lastLogin)) {
$this->setLastLogin($response->lastLogin);
}
if (isset($response->statusConnection)) {
$this->setStatusConnection($response->statusConnection);
}
if (isset($response->connectionStatus)) {
$this->setStatusConnection($response->connectionStatus);
}
if (isset($response->utcOffset)) {
$this->setUtcOffset($response->utcOffset);
}
if (isset($response->username)) {
$this->setUsername($response->username);
}
if (isset($response->avatarUrl)) {
$this->setAvatarUrl($response->avatarUrl);
}
if (isset($response->settings) && isset($response->settings->preferences)) {
$preferences = new Preferences();
$this->setPreferencesData($preferences->updateOutOfResponse($response->settings->preferences));
}
return $this;
}
/**
* Gets full user data to submit to api
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$userData = [
"email" => $this->email,
"name" => $this->name,
"username" => $this->username
];
if (!is_null($this->password)) {
$userData["password"] = $this->password;
}
if (!is_null($this->active)) {
$userData["active"] = $this->active;
}
if (!is_null($this->roles)) {
$userData["roles"] = $this->roles;
}
if (!is_null($this->joinDefaultChannels)) {
$userData["joinDefaultChannels"] = $this->joinDefaultChannels;
}
if (!is_null($this->requirePasswordChange)) {
$userData["requirePasswordChange"] = $this->requirePasswordChange;
}
if (!is_null($this->sendWelcomeEmail)) {
$userData["sendWelcomeEmail"] = $this->sendWelcomeEmail;
}
if (!is_null($this->verified)) {
$userData["verified"] = $this->verified;
}
if (!is_null($this->customFields)) {
$userData["customFields"] = $this->customFields;
}
return $userData;
}
/**
* Sets the date-time user created at api
*
* @param string $createdAt
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setCreatedAt($createdAt)
{
if (is_string($createdAt)) {
$this->createdAt = $createdAt;
}
return $this;
}
/**
* Sets user type at the api
*
* @param string $type
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setType($type)
{
if (is_string($type)) {
$this->type = $type;
}
return $this;
}
/**
* Sets date-time user last logged in
*
* @param string $lastLogin
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setLastLogin($lastLogin)
{
if (is_string($lastLogin)) {
$this->lastLogin = $lastLogin;
}
return $this;
}
/**
* Sets user status connection of the api
*
* @param string $statusConnection
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setStatusConnection($statusConnection)
{
if (is_string($statusConnection)) {
$this->statusConnection = $statusConnection;
}
return $this;
}
/**
* Sets user utc offset
*
* @param int|float $utcOffset
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setUtcOffset($utcOffset)
{
if (is_numeric($utcOffset)) {
$this->utcOffset = $utcOffset;
}
return $this;
}
/**
* Sets user avatar url
*
* @param string $avatarUrl
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setAvatarUrl($avatarUrl)
{
if (is_string($avatarUrl)) {
$this->avatarUrl = $avatarUrl;
}
return $this;
}
/**
* @param Preferences $preferences
* @return $this
*/
private function setPreferencesData(Preferences $preferences)
{
$this->preferences = $preferences;
return $this;
}
/**
* Sets data error
*
* @param string $error
*
* @return \ATDev\RocketChat\Users\Data
*/
private function setDataError($error)
{
static::setError($error);
return $this;
}
}