Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing deep and eloquent models with Lara...

nunulk
June 21, 2017

Implementing deep and eloquent models with Laravel 5.4

nunulk

June 21, 2017
Tweet

More Decks by nunulk

Other Decks in Programming

Transcript

  1. deep model “A deep model provides a lucid expression of

    the primary concerns of the domain experts and their most relevant knowledge while it sloughs of the superficial aspects of the domain.”
 - Domain-Driven Design
  2. Model inherits Eloquent\Model class User extends Model
 {
 } //

    retrieving a model
 $user = User::find($id);
 // accessors & mutators
 $user->email

  3. Model inherits Eloquent\Model class Guest extends Model
 {
 protected $table

    = ‘users’;
 protected static function boot()
 {
 parent::boot();
 static::addGlobalScope(‘guest’, function (Builder $builder) {
 // restrictions here
 });
 }
 } // retrieving a model
 $guest = Guest::find($id);
 // accessors & mutators
 $guest->email

  4. Model has Eloquent\Model class Guest
 {
 protected $user;
 public function

    __construct(User $user)
 {
 $this->user = $user;
 }
 public function __get($key)
 {
 return $this->user->$key;
 }
 } // retrieving a model
 $guest = new Guest(User::find($id));
 // accessors & mutators
 $guest->email

  5. Model as ValueObject class ElapsedTime
 {
 public function __toString()
 {


    $diff = $this->from->diff($this->to);
 if ($diff->d > 0) {
 return $diff->d . ‘೔લ’;
 }
 […]
 }
 }
 class User extends Model
 {
 public function getLastLoggedInAtAttribute($value)
 {
 return new ElapsedTime($value, Carbon::now()); // N೔લ, N࣌ؒલ, N෼લ, ͨͬͨࠓ
 }
 } // accessors & mutators
 $user->last_logged_in_at // instance of ElapsedTime
  6. Model as Enum class UserRole extends Enum
 {
 const HOST

    = 1;
 const GUEST = 2;
 static private $list = [
 self::HOST => ‘ϗετ’,
 self::GUEST => ‘ήετ’
 ];
 }
 class User extends Model
 {
 public function getRoleAttribute($value)
 {
 return new UserRole($value);
 }
 } // accessors & mutators
 $user->role // instance of UserRole
  7. Model as ViewModel with Fluent abstract class ViewModel extends Fluent


    {
 abstract public function render();
 }
 class ConcreteViewModel extends ViewModel
 {
 public function render()
 {
 return […]
 }
 } $vm = new ConcreteViewModel([‘foo’ => $foo]);
 $vm->foo // attribute
  8. Model as ViewModel class UserView extends ViewModel
 {
 public function

    render()
 {
 $additionals = [‘full_name’];
 return $this->user->append($additionals);
 }
 }
 class User extends Model
 {
 public function getFullNameAttribute()
 {
 return $this->family_name . ’ ’ . $this->given_name;
 }
 } // like DTO
 return (new UserView([‘user’ => $user]))->render();
  9. Model as ViewModel <template>
 <div class=“p-user”>
 <span class=“p-user__avater”><img :src=“user.avater”></span>
 <span

    class=“p-user__name”>{{ user.full_name }}</span>
 </div>
 </template>
 <script>
 export default {
 data () {
 return {
 user: {},
 }
 },
 created () {
 window.axios.get(‘/api/user’)[…]
 }
 }
 </script>