PHP code example of thalles / repositories-commands

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

    

thalles / repositories-commands example snippets


php artisan repository:setup

php artisan repository:new RepositoryName ModelName



namespace App\Http\Controllers;

use App\Interfaces\UnitOfWorkInterface;

class UserController extends Controller
{
    /**
     * UnitOfWork instance
     *
     * @var App\Interfaces\UnitOfWorkInterface
     */
    private $uow;

    /**
     * Constructor method
     *
     * @param App\Interfaces\UnitOfWorkInterface $uow
     */
    public function __construct(UnitOfWorkInterface $uow)
    {
        $this->uow = $uow;
    }

    public function index()
    {
        try {
            $this->uow->beginTransaction();

            $user = $this->uow->UserRepository->add([
                'name' => 'User Name',
                'email' => '[email protected]',
                'password' => bcrypt('secret')
            ]);

            if ($user) {
                $this->uow->commit();
                return $this->uow->UserRepository->all();
            }

            throw new Exception();
        } catch (Exception $e) {
            $this->uow->rollback();
            return back()->with('error', 'Internal Server Error.');
        }
    }
}

/**
 * @param integer $id
 * @return stdClass|null
 */
function getById(int $id) : ?stdClass

/**
 * @return Collection
 */
function all() : Collection;

/**
 * @param array $data
 * @return stdClass|null
 */
function add(array $data) : ?stdClass;

/**
 * @param integer $id
 * @param array $data
 * @return stdClass|null
 */
function update(int $id, array $data) : ?stdClass;

/**
 * @param integer $id
 * @return boolean
 */
function delete(int $id) : bool;

/**
 * @return integer
 */
function count() : int;

/**
 * @param Model $model
 * @return array
 */
function dataFormat(Model $model) : array;



namespace App\Interfaces;

use Illuminate\Support\Collection;

interface UserRepositoryInterface extends RepositoryInterface
{
    /**
     * Make the search query
     *
     * @param string $search
     * @param int $pagination
     *
     * @return Collection
     */
    function search(string $search, int $id_user_auth) : Collection;
}



namespace App\Repositories;

use App\Models\User;
use Illuminate\Support\Collection;
use App\Interfaces\UserRepositoryInterface;

class UserRepository extends Repository implements UserRepositoryInterface
{
    /**
     * Constructor method
     */
    public function __construct()
    {
        parent::__construct(new User());
    }

    /**
     * Make the search query
     *
     * @param string $search
     * @param int $pagination
     *
     * @return Collection
     */
    public function search(string $search, int $id_user_auth) : Collection
    {
        $users = $this->model
            ->where('id', '<>', $id_user_auth)
            ->where('name', 'LIKE', "%{$search}%")
            ->orWhere('email', 'LIKE', "%{$search}%")
            ->orderBy('name', 'ASC')
            ->get()
            ->toArray();

        $users = $this->arrayToStdClass($users);
        $users = new Collection($users);

        return $users;
    }
}