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