1. Go to this page and download the library: Download desmart/laravel-domain-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
desmart / laravel-domain-core example snippets
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;
}
}
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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.