PHP code example of rougin / dexter

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

    

rougin / dexter example snippets

 php
namespace Rougin\Torin\Depots;

use Rougin\Dexter\Depots\EloquentDepot;
use Rougin\Torin\Models\Client;

class ClientDepot extends EloquentDepot
{
    protected $model;

    public function __construct(Client $client)
    {
        $this->model = $client;
    }
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

/** @var array<string, mixed> */
$data = /** ... */;

/** @var \Acme\Sample\User */
$item = $depot->create($data);
 php
namespace Acme\Depots;

use Acme\Sample\UserFactory;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    /**
     * Creates a new item.
     *
     * @param array<string, mixed> $data
     *
     * @return \Acme\Sample\User
     */
    public function create($data)
    {
        return UserFactory::create($data);
    }

    // ...
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

$depot->delete(99);
 php
namespace Acme\Depots;

use Acme\Sample\UserDeleter;
use Acme\Sample\UserReader;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Checks if the specified item exists.
     *
     * @param integer $id
     *
     * @return boolean
     */
    public function rowExists($id)
    {
        return UserReader::exists($id);
    }

    /**
     * Deletes the specified item.
     *
     * @param integer $id
     *
     * @return boolean
     */
    protected function deleteRow($id)
    {
        return UserDeleter::delete($id);
    }
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

/** @var \Acme\Sample\User */
$item = $depot->find(99);
 php
namespace Acme\Depots;

use Acme\Sample\UserReader;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Returns the specified item.
     *
     * @param integer $id
     *
     * @return \Acme\Sample\User
     * @throws \UnexpectedValueException
     */
    protected function findRow($id)
    {
        $item = UserReader::find($id);

        if (! $item)
        {
            throw new \UnexpectedValueException('Item not found');
        }

        return $item;
    }
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

/** @var \Rougin\Dexter\Result */
$item = $depot->get(1, 10);
 php
namespace Acme\Depots;

use Acme\Sample\UserReader;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Returns the total number of items.
     *
     * @return integer
     */
    public function getTotal()
    {
        return UserReader::totalRows();
    }

    /**
     * Returns the items with filters.
     *
     * @param integer $page
     * @param integer $limit
     *
     * @return \Acme\Sample\User[]
     */
    protected function getItems($page, $limit)
    {
        return UserReader::getByLimit($limit, $page);
    }
}
 php
namespace Acme\Depots;

use Acme\Sample\UserReader;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Returns the items with filters.
     *
     * @param integer $page
     * @param integer $limit
     *
     * @return \Acme\Sample\User[]
     */
    protected function getItems($page, $limit)
    {
        $offset = $this->getOffset($page, $limit);

        return UserReader::items($offset, $limit);
    }

    // ...
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

/** @var \Rougin\Dexter\Result */
$item = $depot->get(1, 10);

print_r($item->toArray());
 php
namespace Acme\Depots;

use Acme\Sample\User;
use Acme\Sample\UserReader;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Returns the parsed item.
     *
     * @param \Acme\Sample\User $row
     *
     * @return array<string, mixed>
     */
    protected function asRow(User $row)
    {
        $data = array('id' => $row->id);

        $data['name'] = $row->name;

        $data['age'] = $row->age + 10;

        return $data;
    }

    // ...
}
 php
// index.php

use Acme\Depots\UserDepot;

$depot = new UserDepot;

/** @var array<string, mixed> */
$data = /** ... */;

$depot->update(99, $data);
 php
namespace Acme\Depots;

use Acme\Sample\UserUpdater;
use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * Updates the specified item.
     *
     * @param integer              $id
     * @param array<string, mixed> $data
     *
     * @return boolean
     */
    public function update($id, $data)
    {
        return UserUpdater::update($id, $data);
    }
}
 php
namespace Acme\Depots;

use Acme\Models\User;
use Illuminate\Database\Eloquent\Model;
use Rougin\Dexter\Depots\EloquentDepot;

class UserDepot extends EloquentDepot
{
    /**
     * @param \Acme\Models\User $user
     */
    public function __construct(User $user)
    {
        $this->model = $user;
    }
}
 php
// index.php

use Acme\Depots\UserDepot;
use Acme\Models\User;

$depot = new UserDepot(new User);

// Create a new item ---------------
$data = array('name' => 'John Doe');

$data['email'] = '[email protected]';

/** @var \Acme\Models\User */
$item = $depot->create($data);
// ---------------------------------

// Find an item by its ID ---
/** @var \Acme\Models\User */
$item = $depot->find(1);
// --------------------------

// Paginate through items -------
/** @var \Rougin\Dexter\Result */
$result = $depot->get(1, 10);

/** @var array<string, mixed> */
$items = $result->toArray();
// ------------------------------

// Update an existing item ---------
$data = array('name' => 'Jane Doe');

$depot->update(1, $data);
// ---------------------------------
 php
namespace Acme\Depots;

use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * @param integer $page
     * @param integer $limit
     *
     * @return mixed[]
     */
    protected function getItems($page, $limit)
    {
        $status = $this->filter->asTrueStr('status');

        $model = $this->user;

        $model = $model->where('status', $status);

        return $model->get();
    }
}
 php
namespace Acme\Depots;

use Rougin\Dexter\Depot;

class UserDepot extends Depot
{
    // ...

    /**
     * @param integer $page
     * @param integer $limit
     *
     * @return mixed[]
     */
    protected function getItems($page, $limit)
    {
        $keys = $this->filter->getSearchKeys();

        $model = $this->user;

        foreach ($keys as $key)
        {
            $value = '%' . $this->filter->asTrueStr($key) . '%';

            $model = $model->where($key, 'like', $value);
        }

        return $model->get();
    }
}
 php
namespace Acme\Routes;

use Acme\Depots\UserDepot;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Input;
use Rougin\Dexter\Route;

class Users extends Route
{
    protected $user;

    public function __construct(UserDepot $user)
    {
        $this->user = $user;
    }

    protected function setIndexData($params)
    {
        $query = new Input($params);

        $limit = $query->asTrueInt('l');

        $page = $query->asTrueInt('p');

        $result = $this->user->get($page, $limit);

        $result = $result->toArray();

        return Response::toJson($result);
    }
}
 php
// index.php

use Acme\Depots\UserDepot;
use Acme\Routes\Users;

$depot = new UserDepot;

$route = new Users($depot);

/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */

$response = $route->index($request);
 php
namespace Acme\Routes;

use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Checks if the items are allowed to be returned.
     *
     * @param array<string, mixed> $params
     *
     * @return boolean
     */
    protected function isIndexValid($params)
    {
        return true;
    }
}
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidIndex()
    {
        return new Response(422);
    }
}
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Executes the logic for returning an array of items.
     *
     * @param array<string, mixed> $params
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setIndexData($params)
    {
        $result = $this->user->get($params['page'], $params['limit']);

        return Response::toJson($result->toArray());
    }
}
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidDelete()
    {
        return new Response(404);
    }

    /**
     * Checks if the specified item can be deleted.
     *
     * @param integer $id
     *
     * @return boolean
     */
    protected function isRowValid($id)
    {
        return true;
    }

    /**
     * Executes the logic for deleting the specified item.
     *
     * @param integer $id
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setDeleteData($id)
    {
        $this->user->delete($id);

        return Response::toJson('Deleted!', 204);
    }
}
 php
// index.php

// ...

/** @var \Acme\Depots\UserDepot */
$route = /** ... */;

$response = $route->delete($id);
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidIndex()
    {
        return new Response(422);
    }

    /**
     * Checks if the items are allowed to be returned.
     *
     * @param array<string, mixed> $params
     *
     * @return boolean
     */
    protected function isIndexValid($params)
    {
        return true;
    }

    /**
     * Executes the logic for returning an array of items.
     *
     * @param array<string, mixed> $params
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setIndexData($params)
    {
        $result = $this->user->get($params['page'], $params['limit']);

        return Response::toJson($result->toArray());
    }
}
 php
// index.php

// ...

/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */

/** @var \Acme\Depots\UserDepot */
$route = /** ... */;

$response = $route->index($request);
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidShow()
    {
        return new Response(422);
    }

    /**
     * Checks if the specified item is allowed to be returned.
     *
     * @param integer              $id
     * @param array<string, mixed> $params
     *
     * @return boolean
     */
    protected function isRowValid($id)
    {
        return true;
    }

    /**
     * Executes the logic for returning the specified item.
     *
     * @param integer              $id
     * @param array<string, mixed> $params
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setShowData($id, $params)
    {
        $item = $this->user->find($id);

        return Response::toJson($item);
    }
}
 php
// index.php

// ...

/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */

/** @var \Acme\Depots\UserDepot */
$route = /** ... */;

$response = $route->show(99, $request);
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidStore()
    {
        return new Response(422);
    }

    /**
     * Checks if it is allowed to create a new item.
     *
     * @param array<string, mixed> $parsed
     *
     * @return boolean
     */
    protected function isStoreValid($parsed)
    {
        return true;
    }

    /**
     * Executes the logic for creating a new item.
     *
     * @param array<string, mixed> $parsed
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setStoreData($parsed)
    {
        $this->user->create($parsed);

        return Response::toJson('Created!', 201);
    }
}
 php
// index.php

// ...

/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */

/** @var \Acme\Depots\UserDepot */
$route = /** ... */;

$response = $route->store($request);
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * Returns a response if the validation failed.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalidUpdate()
    {
        return new Response(422);
    }

    /**
     * Checks if the specified item can be updated.
     *
     * @param integer $id
     * @param array<string, mixed> $parsed
     *
     * @return boolean
     */
    protected function isUpdateValid($id, $parsed)
    {
        return true;
    }

    /**
     * Executes the logic for updating the specified item.
     *
     * @param integer              $id
     * @param array<string, mixed> $parsed
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function setUpdateData($id, $parsed)
    {
        $this->user->update($id, $parsed);

        return Response::toJson('Updated!', 204);
    }
}
 php
// index.php

// ...

/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */

/** @var \Acme\Depots\UserDepot */
$route = /** ... */;

$response = $route->update(99, $request);
 php
namespace Acme\Routes;

use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * @param integer $code
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function invalid($code = 400)
    {
        $data = $this->check->errors();

        // HTTP 404 means the row does not exists ---
        if ($code === 404)
        {
            $data = 'User does not exists';
        }
        // ------------------------------------------

        return Response::toJson($data, $code);
}
 php
namespace Acme\Routes;

use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * @param array<string, mixed> $data
     * @param integer              $id
     *
     * @return boolean
     */
    protected function isAllowed($data, $id = 0)
    {
        if ($id && ! $this->depot->rowExists($id))
        {
            return false;
        }

        return $this->auth->isAuthorized($data);
    }
}
 php
namespace Acme\Routes;

use Rougin\Dexter\Route;

class Users extends Route
{
    // ...

    /**
     * @param array<string, mixed> $data
     * @param integer              $id
     *
     * @return boolean
     */
    protected function isDataValid($data, $id = 0)
    {
        return $this->check->valid($data);
    }
}