50
common/Auth/Traits/HasAvatarAttribute.php
Executable file
50
common/Auth/Traits/HasAvatarAttribute.php
Executable file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Auth\Traits;
|
||||
|
||||
use Common\Auth\BaseUser;
|
||||
use Storage;
|
||||
use Str;
|
||||
|
||||
trait HasAvatarAttribute
|
||||
{
|
||||
public function getAvatarAttribute(?string $value)
|
||||
{
|
||||
// absolute link
|
||||
if ($value && Str::contains($value, '//')) {
|
||||
// change google/twitter avatar imported via social login size
|
||||
$value = str_replace(
|
||||
'.jpg?sz=50',
|
||||
".jpg?sz=$this->gravatarSize",
|
||||
$value,
|
||||
);
|
||||
if ($this->gravatarSize > 50) {
|
||||
// twitter
|
||||
$value = str_replace('_normal.jpg', '.jpg', $value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
// relative link (for new and legacy urls)
|
||||
if ($value) {
|
||||
return Storage::disk('public')->url(
|
||||
str_replace('storage/', '', $value),
|
||||
);
|
||||
}
|
||||
|
||||
// gravatar
|
||||
$hash = md5(trim(strtolower($this->email)));
|
||||
|
||||
return "https://www.gravatar.com/avatar/$hash?s={$this->gravatarSize}&d=retro";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number $size
|
||||
* @return BaseUser
|
||||
*/
|
||||
public function setGravatarSize($size)
|
||||
{
|
||||
$this->gravatarSize = $size;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
26
common/Auth/Traits/HasDisplayNameAttribute.php
Executable file
26
common/Auth/Traits/HasDisplayNameAttribute.php
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Auth\Traits;
|
||||
|
||||
trait HasDisplayNameAttribute
|
||||
{
|
||||
/**
|
||||
* Compile user display name from available attributes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDisplayNameAttribute()
|
||||
{
|
||||
if ($this->username) {
|
||||
return $this->username;
|
||||
} else if ($this->first_name && $this->last_name) {
|
||||
return $this->first_name.' '.$this->last_name;
|
||||
} else if ($this->first_name) {
|
||||
return $this->first_name;
|
||||
} else if ($this->last_name) {
|
||||
return $this->last_name;
|
||||
} else {
|
||||
return explode('@', $this->email)[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user