1
|
|
|
<?php namespace League\OAuth2\Client\Provider; |
2
|
|
|
|
3
|
|
|
use League\OAuth2\Client\Tool\ArrayAccessorTrait; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @property array $response |
7
|
|
|
* @property string $uid |
8
|
|
|
*/ |
9
|
|
|
class LinkedInResourceOwner extends GenericResourceOwner |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
use ArrayAccessorTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Raw response |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $response = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sorted profile pictures |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $sortedProfilePictures = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $email; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates new resource owner. |
35
|
|
|
* |
36
|
|
|
* @param array $response |
37
|
|
|
* @param string|null $email |
38
|
|
|
*/ |
39
|
3 |
|
public function __construct(array $response = array(), $email = null) |
40
|
|
|
{ |
41
|
3 |
|
$this->response = $response; |
42
|
3 |
|
$this->email = $email; |
43
|
3 |
|
$this->setSortedProfilePictures(); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets resource owner attribute by key. The key supports dot notation. |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
3 |
|
public function getAttribute($key) |
52
|
|
|
{ |
53
|
3 |
|
return $this->getValueByKey($this->response, (string) $key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get user first name |
58
|
|
|
* |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
3 |
|
public function getFirstName() |
62
|
|
|
{ |
63
|
3 |
|
return $this->getAttribute('localizedFirstName'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get user user id |
68
|
|
|
* |
69
|
|
|
* @return string|null |
70
|
|
|
*/ |
71
|
3 |
|
public function getId() |
72
|
|
|
{ |
73
|
3 |
|
return $this->getAttribute('id'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get specific image by size |
78
|
|
|
* |
79
|
|
|
* @param integer $size |
80
|
|
|
* @return array|null |
81
|
|
|
*/ |
82
|
1 |
|
public function getImageBySize($size) |
83
|
|
|
{ |
84
|
|
|
$pictures = array_filter($this->sortedProfilePictures, function ($picture) use ($size) { |
85
|
1 |
|
return isset($picture['width']) && $picture['width'] == $size; |
86
|
1 |
|
}); |
87
|
|
|
|
88
|
1 |
|
return count($pictures) ? $pictures[0] : null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get available user image sizes |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
1 |
|
public function getImageSizes() |
97
|
|
|
{ |
98
|
|
|
return array_map(function ($picture) { |
99
|
1 |
|
return $this->getValueByKey($picture, 'width'); |
100
|
1 |
|
}, $this->sortedProfilePictures); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get user image url |
105
|
|
|
* |
106
|
|
|
* @return string|null |
107
|
|
|
*/ |
108
|
3 |
|
public function getImageUrl() |
109
|
|
|
{ |
110
|
3 |
|
$pictures = $this->getSortedProfilePictures(); |
111
|
3 |
|
$picture = array_pop($pictures); |
112
|
|
|
|
113
|
3 |
|
return $picture ? $this->getValueByKey($picture, 'url') : null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get user last name |
118
|
|
|
* |
119
|
|
|
* @return string|null |
120
|
|
|
*/ |
121
|
3 |
|
public function getLastName() |
122
|
|
|
{ |
123
|
3 |
|
return $this->getAttribute('localizedLastName'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the sorted collection of profile pictures. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
3 |
|
public function getSortedProfilePictures() |
132
|
|
|
{ |
133
|
3 |
|
return $this->sortedProfilePictures; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get user url |
138
|
|
|
* |
139
|
|
|
* @return string|null |
140
|
|
|
*/ |
141
|
3 |
|
public function getUrl() |
142
|
|
|
{ |
143
|
3 |
|
$vanityName = $this->getAttribute('vanityName'); |
144
|
|
|
|
145
|
3 |
|
return $vanityName ? sprintf('https://www.linkedin.com/in/%s', $vanityName) : null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get user email, if available |
150
|
|
|
* |
151
|
|
|
* @return string|null |
152
|
|
|
*/ |
153
|
2 |
|
public function getEmail() |
154
|
|
|
{ |
155
|
2 |
|
return $this->email; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Attempts to sort the collection of profile pictures included in the profile |
160
|
|
|
* before caching them in the resource owner instance. |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
3 |
|
private function setSortedProfilePictures() |
165
|
|
|
{ |
166
|
3 |
|
$pictures = $this->getAttribute('profilePicture.displayImage~.elements'); |
167
|
3 |
|
if (is_array($pictures)) { |
168
|
|
|
$pictures = array_filter($pictures, function ($element) { |
169
|
|
|
// filter to public images only |
170
|
|
|
return |
171
|
2 |
|
isset($element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']) |
172
|
2 |
|
&& strtoupper($element['authorizationMethod']) === 'PUBLIC' |
173
|
2 |
|
&& isset($element['identifiers'][0]['identifier']) |
174
|
|
|
; |
175
|
2 |
|
}); |
176
|
|
|
// order images by width, LinkedIn profile pictures are always squares, so that should be good enough |
177
|
|
|
usort($pictures, function ($elementA, $elementB) { |
178
|
2 |
|
$wA = $elementA['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width']; |
179
|
2 |
|
$wB = $elementB['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width']; |
180
|
2 |
|
return $wA - $wB; |
181
|
2 |
|
}); |
182
|
2 |
|
$pictures = array_map(function ($element) { |
183
|
|
|
// this is an URL, no idea how many of identifiers there can be, so take the first one. |
184
|
2 |
|
$url = $element['identifiers'][0]['identifier']; |
185
|
2 |
|
$type = $element['identifiers'][0]['mediaType']; |
186
|
2 |
|
$width = $element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width']; |
187
|
|
|
return [ |
188
|
2 |
|
'width' => $width, |
189
|
2 |
|
'url' => $url, |
190
|
2 |
|
'contentType' => $type, |
191
|
|
|
]; |
192
|
2 |
|
}, $pictures); |
193
|
|
|
} else { |
194
|
1 |
|
$pictures = []; |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
$this->sortedProfilePictures = $pictures; |
198
|
3 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Return all of the owner details available as an array. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
2 |
|
public function toArray() |
206
|
|
|
{ |
207
|
2 |
|
return $this->response; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|