PHP code example of happyr / json-api-response-factory
1. Go to this page and download the library: Download happyr/json-api-response-factory 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/ */
happyr / json-api-response-factory example snippets
use Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer;
final class FooTransformer extends AbstractTransformer
{
public function getResourceName(): string
{
return 'foo';
}
public function transform(Foo $item): array
{
return [
'id' => $item->getId(),
'bar' => (string)$item->getBar(),
];
}
}
$item = new Foo('bar');
$response = $responseFactory->createWithItem($item, new FooTransformer());
$items = [
new Foo('bar'),
new Foo('baz'),
];
$response = $responseFactory->createWithCollection($items, new FooTransformer());
use Happyr\JsonApiResponseFactory\ResponseModelInterface;
final class InvalidRequestResponseModel implements ResponseModelInterface
{
public function getHttpStatusCode() : int
{
return 400;
}
public function getPayload() : array
{
return [
'error' => 'Invalid request.',
];
}
}
$model = new InvalidRequestResponseModel();
$response = $responseFactory->createWithResponseModel($model);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.