PHP code example of nilportugues / eloquent-repository

1. Go to this page and download the library: Download nilportugues/eloquent-repository 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/ */

    

nilportugues / eloquent-repository example snippets



use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule();
$capsule->addConnection(
    [
        'driver' => 'sqlite', 
        'database' => __DIR__.'/database.db',
        'prefix' => ''
    ], 
    'default' //connection name.
);
$capsule->bootEloquent();
$capsule->setAsGlobal();


use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
}


use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    protected $userAdapter;
    
    /**
     * @param $userAdapter
     */
    public function __construct($userAdapter)
    {
        $this->userAdapter = $userAdapter; 
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
    
    /**
     * {@inheritdoc}
     */    
    public function find(Identity $id, Fields $fields = null)
    {
        $eloquentModel = parent::find($id, $fields);   
        
        return $this->userAdapter->fromEloquent($eloquentModel);
    }
    
    /**
     * {@inheritdoc}
     */    
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    {
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);   
        
        return $this->fromEloquentArray($eloquentModelArray);
    }       
    
    /**
     * {@inheritdoc}
     */
    public function findAll(Pageable $pageable = null)
    {
        $page = parent::findAll($pageable);
        
        return new Page(
            $this->fromEloquentArray($page->content()),
            $page->totalElements(),
            $page->pageNumber(),
            $page->totalPages(),
            $page->sortings(),
            $page->filters(),
            $page->fields()
        );
    } 

   /**
    * @param array $eloquentModelArray
    * @return array
    */
   protected function fromEloquentArray(array $eloquentModelArray)
   {
        $results = [];
        foreach ($eloquentModelArray as $eloquentModel) {
            //This is 


use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository as Repository;

class EloquentRepository extends Repository
{
    /**
     * @var string
     */
    protected $modelClass;
    
    /**
     * @param string $modelClass
     */
    public function __construct($modelClass)
    {
        $this->modelClass = (string) $modelClass;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return $this->modelClass;
    }
}


use NilPortugues\Foundation\Domain\Model\Repository\Filter;

$repository = new UserRepository();

$filter = new Filter();
$filter->must()->contain('name', 'Ken');

echo $repository->count($filter);


use NilPortugues\Foundation\Domain\Model\Repository\Sort;

$repository = new UserRepository();

$filter = null; //all records
$sort = new Sort(['name', 'id'], new Order('ASC', 'DESC'));
$fields = null; //all columns

$results = $repository->findBy($filter, $sort, $fields);


use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;

$repository = new UserRepository();

$filter = null; //all records
$sort = null; //existing order
$fields = new Fields(['name', 'id']);

$results = $repository->findBy($filter, $sort, $fields);