PHP code example of reindert-vetter / api-version-control
1. Go to this page and download the library: Download reindert-vetter/api-version-control 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/ */
reindert-vetter / api-version-control example snippets
namespace App\VersionControl\Orders;
use ReindertVetter\ApiVersionControl\Concerns\VersionStatement;
class ValidateZipCode
{
use VersionStatement;
}
if (ValidateZipCode::permitted()) {
(...)
}
namespace App\Middleware\Version;
use Closure;
use Illuminate\Http\Request;
class PrepareParameterException
{
/**
* @param $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
// Set the default parameter because it is
namespace App\Middleware\Version;
use Closure;
use Illuminate\Http\Request;
class ThrowHumanException
{
/**
* @param $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
/** @var \Illuminate\Http\Response $response */
$response = $next($request);
// Catch the exception to return an exception in a different format.
if ($response->exception) {
$response->setContent(
[
"errors" => [
[
"human" => $response->exception->getMessage(),
],
],
]
);
}
return $response;
}
}
public function index(OrderIndexRequest $request, OrderResource $resource): ResourceCollection
{
$orders = Order::query()
->productIds($request->productIds())
->with($resource->withRelationships())
->paginate($request->limit());
return $resource::collection($orders);
}
use ReindertVetter\ApiVersionControl\Middleware\Version\Bind;
return [
'releases' => [
'orders.index' => [
'<=1' => [
new Bind(OrderIndexRequest::class, OrderIndexRequestV1::class),
new Bind(OrderIndexResource::class, OrderIndexResourceV1::class),
],
'>=2' => [
new Bind(OrderIndexRequest::class, OrderIndexRequestV2::class),
new Bind(OrderIndexResource::class, OrderIndexResourceV2::class),
],
],
]
]