forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMapper.php
More file actions
115 lines (105 loc) · 2.82 KB
/
Copy pathAbstractMapper.php
File metadata and controls
115 lines (105 loc) · 2.82 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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Auth\Social\Mapper;
use Cake\Utility\Hash;
/**
* AbstractMapper
*
*/
abstract class AbstractMapper
{
/**
* Provider Raw data
* @var
*/
protected $_rawData;
/**
* Map for provider fields
* @var null
*/
protected $_mapFields;
/**
* Default Map for provider fields
* @var
*/
protected $_defaultMapFields = [
'id' => 'id',
'username' => 'username',
'full_name' => 'name',
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'email',
'avatar' => 'avatar',
'gender' => 'gender',
'link' => 'link',
'bio' => 'bio',
'locale' => 'locale',
'validated' => 'validated'
];
/**
* Constructor
*
* @param mixed $rawData raw data
* @param mixed $mapFields map fields
*/
public function __construct($rawData, $mapFields = null)
{
$this->_rawData = $rawData;
if (!is_null($mapFields)) {
$this->_mapFields = $mapFields;
}
$this->_mapFields = array_merge($this->_defaultMapFields, $this->_mapFields);
}
/**
* Invoke method
*
* @return mixed
*/
public function __invoke()
{
return $this->_map();
}
/**
* If email is present the user is validated
*
* @return bool
*/
protected function _validated()
{
$email = Hash::get($this->_rawData, $this->_mapFields['email']);
return !empty($email);
}
/**
* Maps raw data using mapFields
*
* @return mixed
*/
protected function _map()
{
$result = [];
collection($this->_mapFields)->each(function ($mappedField, $field) use (&$result) {
$value = Hash::get($this->_rawData, $mappedField);
$function = '_' . $field;
if (method_exists($this, $function)) {
$value = $this->{$function}();
}
$result[$field] = $value;
});
$token = Hash::get($this->_rawData, 'token');
$result['credentials'] = [
'token' => is_array($token) ? Hash::get($token, 'accessToken') : $token->getToken(),
'secret' => is_array($token) ? Hash::get($token, 'tokenSecret') : null,
'expires' => is_array($token) ? Hash::get($token, 'expires') : $token->getExpires(),
];
$result['raw'] = $this->_rawData;
return $result;
}
}