2017 © Pedro PelĂĄez
 

library laravel-domain-core

DeSmart core DDD package for Laravel

image

desmart/laravel-domain-core

DeSmart core DDD package for Laravel

  • Thursday, November 3, 2016
  • by DeSmart
  • Repository
  • 10 Watchers
  • 1 Stars
  • 973 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

Domain core

Hydrating models to entities

Models should use DeSmart\DomainCore\ConvertsToEntityTrait. It gives toEntity() method which tries to convert the model to entity., (*1)

Entity class name

Entity class name is based on model class name. The convention here is:, (*2)

Module\Model\SomeModel -> Module\Entity\SomeModelEntity

Entity class name can be changed. Simply set $entityClassName attribute in model definition., (*3)

Passing data to entity

Model is converted to array, and through JsonMapper all entity properties are set., (*4)

toEntity() will also map related models to proper entites.
It's done similar to JsonMapper., (*5)

If needed some relations can be converted through a dedicated method. Simply create [relationName]ToEntity method inside your model., (*6)

One-to-many relations have to represented as type hinted variadic argument in entity (see example below)., (*7)

Example

<?php
class User extends \Illuminate\Database\Eloquent\Model
{
    use ConvertsToEntityTrait;

    public function files()
    {
        return $this->hasMany(File::class);
    }

    public function bio()
    {
        return $this->hasOne(Bio::class);
    }

    public function avatar()
    {
        return $this->files()
            ->where('is_avatar', 1);
    }

    public function avatarToEntity(File $avatar, UserEntity $user)
    {
        return new Avatar($file->toEntity());
    }
}

class UserEntity
{
    public function setFiles(FileEntity ...$files)
    {
        $this->files = $files;
    }

    public function setBio(BioEntity $bio) 
    {
        $this->bio = $bio;
    }

    public function setAvatar(Avatar $avatar)
    {
        $this->avatar = $avatar;
    }
}

Querying Collections

Example

<?php

class UserEntitiesRepository
{
    use ConvertsCollectionToEntitiesTrait;

    /** @var User */
    private $query;

    public function __construct(User $user)
    {
        $this->query = $user;
    }

    /**
     * @param CriteriaCollectionInterface $criteriaCollection
     * @return UserEntity[]
     */
    public function getAllMatching(CriteriaCollectionInterface $criteriaCollection) 
    {
        /** @var \Illuminate\Database\Eloquent\Builder $builder */
        $builder = $this->getQueryBuilder();

        $queryBuilder = $builder->getQuery();
        $queryBuilder->select($this->query->getTable() . '.*');

        foreach ($criteriaCollection->getAll() as $criterion) {
            $criterion->apply($builder);
        }

        $collection = $builder->get();

        return $this->convertCollectionToEntities($collection);
    }
}

class WithIdsCriterion implements \DeSmart\DomainCore\Repository\Criteria\CriterionInterface
{
    /** @var array */
    protected $ids;

    /**
     * @param array $ids
     */
    public function __construct(array $ids)
    {
        $this->ids = $ids;
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return mixed
     */
    public function apply($query)
    {
        $idsList = array_map(function ($id) {
            return "'{$id}'";
        }, $this->ids);

        $query->whereIn($query->getModel()->getQualifiedKeyName(), $this->ids);

        if (false === empty($idsList)) {
            $query->orderBy(\DB::raw("FIELD ({$query->getModel()->getQualifiedKeyName()}," . implode(',', $idsList) . ")"));
        }

        return $query;
    }
}

$criteriaCollection = new \DeSmart\DomainCore\Repository\Criteria\CriteriaCollection();
$usersRepository = new UsersRepository(new User());
$criteriaCollection->add(
    new WithIdsCriterion([1, 2, 3])
);

$users = $usersRepository->getAllMatchingCriteria($criteriaCollection);

The Versions

03/11 2016

dev-master

9999999-dev

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

03/11 2016

0.7.1

0.7.1.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

04/02 2016

0.7.0

0.7.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

29/01 2016

0.6.0

0.6.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

10/09 2015

0.5.0

0.5.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

17/08 2015

0.4.2

0.4.2.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

17/08 2015

0.4.1

0.4.1.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

17/08 2015

0.4.0

0.4.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

15/07 2015

0.3.3

0.3.3.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

09/07 2015

0.3.2

0.3.2.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

01/07 2015

0.3.1

0.3.1.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

01/07 2015

0.3.0

0.3.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski

01/07 2015

0.2.0

0.2.0.0

DeSmart core DDD package for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Radoslaw Mejer
by MichaƂ Golom
by Lucjan Wilczewski