PHP code example of andersonef / repositories-pattern

1. Go to this page and download the library: Download andersonef/repositories-pattern 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/ */

    

andersonef / repositories-pattern example snippets


Andersonef\Repositories\Providers\RepositoryProvider::class,

namespace Inet\Repositories\BlogNamespace;

use Andersonef\Repositories\Abstracts\RepositoryAbstract;
use \Post;

/**
* Data repository to work with entity Post.
*
* Class PostRepository
* @package Inet\Repositories\BlogNamespace
*/
class PostRepository extends RepositoryAbstract{


public function entity()
{
    return \Post::class;
}

}

namespace Inet\Services\BlogNamespace;

use Andersonef\Repositories\Abstracts\ServiceAbstract;
use Illuminate\Database\DatabaseManager;
use \Inet\Repositories\BlogNamespace\PostRepository;

/**
* Service layer that will applies all application rules to work with Post class.
*
* Class PostService
* @package Inet\Services\BlogNamespace
*/
class PostService extends ServiceAbstract{

  /**
   * This constructor will receive by dependency injection a instance of PostRepository and DatabaseManager.
   *
   * @param PostRepository $repository
   * @param DatabaseManager $db
   */
  public function __construct(PostRepository $repository, DatabaseManager $db)
  {
      parent::__construct($repository, $db);
  }
}

$yourservice->repositoryMethod(); // this will be the same as: $yourservice->getRepository()->repositoryMethod();
$yourRepository->entityMethod(); // this will be the same as: $tyourRepository->getEntity()->entityMethod();

$result = $postService->findByCriteria(new FindUsingLikeCriteria($request->get('textQuery')))->paginate(10);
 
namespace Andersonef\Repositories\Criteria;

use Andersonef\Repositories\Abstracts\CriteriaAbstract;
use Andersonef\Repositories\Contracts\RepositoryContract;
use Illuminate\Database\Eloquent\Model;

class UnreadRecentPostsCriteria extends CriteriaAbstract{

public function apply(Model $model, RepositoryContract $repository)
{
    $model
    ->where('created_at','>',(new \DateTime())->sub(new \DateInterval('P3D'))->format('Y-m-d'))
    ->where('status_read', '=', 1);
    return $model;
}
}