-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSerializer.php
More file actions
125 lines (113 loc) · 4.47 KB
/
Copy pathUserSerializer.php
File metadata and controls
125 lines (113 loc) · 4.47 KB
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
<?php namespace App\ModelSerializers\Auth;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\ModelSerializers\BaseSerializer;
use App\ModelSerializers\SerializerRegistry;
use Auth\Group;
use Auth\User;
/**
* Class BaseUserSerializer
* @package App\ModelSerializers\Auth
*/
class BaseUserSerializer extends BaseSerializer
{
protected static $array_mappings = [
'FirstName' => 'first_name:json_string',
'LastName' => 'last_name:json_string',
'Pic' => 'pic:json_url',
];
}
/**
* Class PublicUserSerializer
* @package App\ModelSerializers\Auth
*/
final class PublicUserSerializer extends BaseUserSerializer
{
}
/**
* Class PrivateUserSerializer
* @package App\ModelSerializers\Auth
*/
final class PrivateUserSerializer extends BaseUserSerializer
{
protected static $array_mappings = [
'Email' => 'email:json_string',
'Identifier' => 'identifier:json_string',
'EmailVerified' => 'email_verified:json_boolean',
'Bio' => 'bio:json_string',
'Address1' => 'address1:json_string',
'Address2' => 'address2:json_string',
'City' => 'city:json_string',
'State' => 'state:json_string',
'PostCode' => 'post_code:json_string',
'CountryIsoCode' => 'country_iso_code:json_string',
'SecondEmail' => 'second_email:json_string',
'ThirdEmail' => 'third_email:json_string',
'Gender' => 'gender:json_string',
'GenderSpecify' => 'gender_specify:json_string',
'StatementOfInterest' => 'statement_of_interest:json_string',
'Irc' => 'irc:json_string',
'LinkedInProfile' => 'linked_in_profile:json_string',
'GithubUser' => 'github_user:json_string',
'WechatUser' => 'wechat_user:json_string',
'TwitterName' => 'twitter_name:json_string',
'Language' => 'language:json_string',
'DateOfBirth' => 'birthday:datetime_epoch',
'PhoneNumber' => 'phone_number:json_string',
'Company' => 'company:json_string',
'JobTitle' => 'job_title:json_string',
'SpamType' => 'spam_type:json_string',
'LastLoginDate' => 'last_login_date:datetime_epoch',
'Active' => 'active:json_boolean',
'PublicProfileShowPhoto' => 'public_profile_show_photo:json_boolean',
'PublicProfileShowFullname' => 'public_profile_show_fullname:json_boolean',
'PublicProfileShowEmail' => 'public_profile_show_email:json_boolean',
'PublicProfileShowSocialMediaInfo' => 'public_profile_show_social_media_info:json_boolean',
'PublicProfileShowBio' => 'public_profile_show_bio:json_boolean',
'PublicProfileAllowChatWithMe' => 'public_profile_allow_chat_with_me:json_boolean',
'PublicProfileShowTelephoneNumber' => 'public_profile_show_telephone_number:json_boolean',
];
/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
{
$user = $this->object;
if (!$user instanceof User) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
$groups = [];
foreach ($user->getGroups() as $group) {
if (!$group instanceof Group) continue;
$groups[] = $group->getSlug();
}
$values['groups'] = $groups;
if (!empty($expand)) {
$exp_expand = explode(',', $expand);
foreach ($exp_expand as $relation) {
if (trim($relation) == 'groups') {
$res = [];
unset($values['groups']);
foreach ($user->getGroups() as $group) {
$res[] = SerializerRegistry::getInstance()->getSerializer($group)->serialize();
}
$values['groups'] = $res;
}
}
}
return $values;
}
}