1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\LaravelSocial\Models; |
4
|
|
|
|
5
|
|
|
use Acacha\LaravelSocial\Traits\UserModel; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
use BadMethodCallException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SocialUser. |
12
|
|
|
* |
13
|
|
|
* @package Acacha\LaravelSocial\Models |
14
|
|
|
*/ |
15
|
|
|
class SocialUser extends Model |
16
|
|
|
{ |
17
|
|
|
use UserModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The attributes that are mass assignable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fillable = [ |
25
|
|
|
'user_id', 'social_id', 'social_type','nickname','name','email','avatar','meta' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the user that owns the social user. |
30
|
|
|
*/ |
31
|
|
|
public function user() |
32
|
|
|
{ |
33
|
|
|
return $this->belongsTo($this->userModel()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Dynamically retrieve attributes on the model. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function __get($key) |
43
|
|
|
{ |
44
|
|
|
if (parent::__get($key) == null ) { |
45
|
|
|
return $this->getAttributeFromMeta($key); |
46
|
|
|
} |
47
|
|
|
return parent::__get($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get an attribute from the meta field. |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function getAttributeFromMeta($key) |
57
|
|
|
{ |
58
|
|
|
if (! $key) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Here we will determine if the model base class itself contains this given key |
63
|
|
|
// since we do not want to treat any of those methods are relationships since |
64
|
|
|
// they are all intended as helper methods and none of these are relations. |
65
|
|
|
if (method_exists(self::class, $key)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$meta = json_decode($this->getAttribute('meta')); |
70
|
|
|
if (isset($meta->$key)) { |
71
|
|
|
return $meta->$key; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$user = $meta->user; |
75
|
|
|
if (isset($user->$key)) { |
76
|
|
|
return $user->$key; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|