TRF Certest first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Presenters;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
abstract class Presenter
|
||||
{
|
||||
public function __construct(protected Model $model)
|
||||
{
|
||||
}
|
||||
|
||||
public function __isset($property): bool
|
||||
{
|
||||
return method_exists($this, Str::camel($property));
|
||||
}
|
||||
|
||||
public function __get($property): mixed
|
||||
{
|
||||
$camel_property = Str::camel($property);
|
||||
|
||||
if (method_exists($this, $camel_property)) {
|
||||
return $this->{$camel_property}();
|
||||
}
|
||||
|
||||
return $this->model->{Str::snake($property)};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Presenters\Traits;
|
||||
|
||||
use Exception;
|
||||
use Vanguard\Presenters\Presenter;
|
||||
|
||||
trait Presentable
|
||||
{
|
||||
protected ?Presenter $presenterInstance = null;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function present(): Presenter
|
||||
{
|
||||
if (is_object($this->presenterInstance)) {
|
||||
return $this->presenterInstance;
|
||||
}
|
||||
|
||||
if (property_exists($this, 'presenter') and class_exists($this->presenter)) {
|
||||
return $this->presenterInstance = new $this->presenter($this);
|
||||
}
|
||||
|
||||
throw new Exception('Property $presenter was not set correctly in '.get_class($this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Presenters;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
|
||||
class UserPresenter extends Presenter
|
||||
{
|
||||
public function name(): string
|
||||
{
|
||||
return sprintf('%s %s', $this->model->first_name, $this->model->last_name);
|
||||
}
|
||||
|
||||
public function nameOrEmail(): string
|
||||
{
|
||||
return trim($this->name()) ?: $this->model->email;
|
||||
}
|
||||
|
||||
public function avatar(): string
|
||||
{
|
||||
if (! $this->model->avatar) {
|
||||
return url('assets/img/profile.png');
|
||||
}
|
||||
|
||||
return Str::contains($this->model->avatar, ['http', 'gravatar'])
|
||||
? $this->model->avatar
|
||||
: url("upload/users/{$this->model->avatar}");
|
||||
}
|
||||
|
||||
public function birthday(): string
|
||||
{
|
||||
return $this->model->birthday
|
||||
? $this->model->birthday->format(config('app.date_format'))
|
||||
: 'N/A';
|
||||
}
|
||||
|
||||
public function fullAddress(): string
|
||||
{
|
||||
$address = '';
|
||||
$user = $this->model;
|
||||
|
||||
if ($user->address) {
|
||||
$address .= $user->address;
|
||||
}
|
||||
|
||||
if ($user->country_id) {
|
||||
$address .= $user->address ? ", {$user->country->name}" : $user->country->name;
|
||||
}
|
||||
|
||||
return $address ?: 'N/A';
|
||||
}
|
||||
|
||||
public function lastLogin(): string
|
||||
{
|
||||
return $this->model->last_login
|
||||
? $this->model->last_login->diffForHumans()
|
||||
: 'N/A';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine css class used for status labels
|
||||
* inside the users table by checking user status.
|
||||
*/
|
||||
public function labelClass(): string
|
||||
{
|
||||
return match ($this->model->status) {
|
||||
UserStatus::ACTIVE => 'success',
|
||||
UserStatus::BANNED => 'danger',
|
||||
default => 'warning',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user