View Decorator

Um ein Template gänzlich von jeder Logik (bsp.: Button-farbe aufgrund eines Statuses anzeigen) abzutrennen kann man einen View Decorator brauchen. Die Logik wird von der View in eine eigene Klasse verschoben.

class UserProfileDecorator
{
   private $entity;

   public static function decorate($model)
   {
       return new self($model);
   }

   public function __construct($model)
   {
       $this->entity = $model;
   }

   public function __get($name)
   {
       $methodName = 'get' . $name;
       if (method_exists(self::class, $methodName)) {
           return $this->$methodName();
       } else {
           return $this->entity->{$name};
       }
   }

   public function __call($name, $arguments)
   {
       return $this->entity->$name($arguments);
   }

   public function getStatus()
   {
       if($this->entity->status == 
App
Models
User::STATUS_ONLINE) {
           return '<label class="text-primary">Online</label>';
       } else {
           return '<label class="text-danger">Offline</label>';
       }
   }

   public function getLastOnline()
   {
       return  date('F j, Y', strtotime($this->entity->lastOnline));
   }
}
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

Dieser Decorator wird dann in der Controllerklasse wie folgt verwendet:

public function user (Request $request)
{
    $user = $this->userService->getUserById($request->id);
    $user = DTO::make($user);
    $user = UserProfileDecorator::decorate($user);
    return view('user.index', compact('user'));
}
1
2
3
4
5
6
7

Der Code welcher anzeigt, wie ein Button aussehen soll wir hier bereits in einer Funktion "vorgerendert" und anschliessend dann in der View in das Template eingefügt.