PHP code example of 3rd-sense / generators

1. Go to this page and download the library: Download 3rd-sense/generators 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/ */

    

3rd-sense / generators example snippets


public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('ThirdSense\Generators\LaravelServiceProvider');
    }
}

if ($app->environment() === 'local') {
    $app->register(\ThirdSense\Generators\LumenServiceProvider::class);
}



namespace App\Repositories;

use App\User;

/**
 * Class UserRepositoryRepository
 * @package App\Repositories
 */
class UserRepository implements UserRepositoryInterface
{
    /**
     * Retrieve all User.
     *
     * @return mixed
     */
    public function all()
    {
        return User::all();
    }

    /**
     * Retrieve a paginated list of User.
     *
     * @param $limit
     */
    public function paginated($limit)
    {
        return User::paginate($limit);
    }

    /**
     * Retrieve a single User by ID.
     *
     * @param $id
     *
     * @return mixed
     */
    public function find($id)
    {
        return User::find($id);
    }

    /**
     * Create and save a new User.
     *
     * @param $data
     *
     * @return bool
     */
    public function create($data)
    {
        $entity = new User;

        return $this->save($entity, $data);
    }

    /**
     * Update an existing User.
     *
     * @param $id
     * @param $data
     *
     * @return User
     */
    public function update($id, $data)
    {
        $entity = $this->findById($id);

        return $this->save($entity, $data);
    }

    /**
     * Remove/delete exiting User
     *
     * @param $id
     *
     * @return int
     */
    public function destroy($id)
    {
        return User::destroy($id);
    }

    /**
     * Save the User.
     *
     * @param $entity
     * @param $data
     *
     * @return boolean
     */
    protected function save($entity, $data)
    {
        // set model properties

        return $entity->save();
    }
}



namespace App\Repositories;

use App\User;

/**
 * Interface UserRepositoryInterface
 * @package App\Repositories
 */
interface UserRepositoryInterface
{
    /**
     * Retrieve all User.
     *
     * @return mixed
     */
    public function all();

    /**
     * Retrieve a paginated list of User.
     *
     * @param $limit
     */
    public function paginated($limit);

    /**
     * Retrieve a single User by ID.
     *
     * @param $id
     *
     * @return mixed
     */
    public function find($id);

    /**
     * Create and save a new User.
     *
     * @param $data
     *
     * @return bool
     */
    public function create($data);

    /**
     * Update an existing User.
     *
     * @param $id
     * @param $data
     *
     * @return User
     *
     */
    public function update($id, $data);

    /**
     * Remove/delete exiting User
     *
     * @param $id
     *
     * @return int
     */
    public function destroy($id);
}

$this->app->bind(UserRepositoryInterface::class, UserRepository::class);



namespace App\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    /**
     * List of resources to possibly tIncludes = [];

    /**
     * @param User $user
     *
     * @return array
     */
    public function transform(User $user)
    {
        return [
            'id'    =>  $user->id,

            // TODO: transform entity properties

            'links' => [
                [
                    'rel' => 'self',
                    'uri' => route('user.show', ['id' =>  $user->id]),
                ],
                [
                    'rel' => 'list',
                    'uri' => route('user.index'),
                ],
            ],
        ];
    }
}