Framework-agnostic PHP package for retrieving and creating users through the DummyJSON API.
This package targets PHP 8.4 and does not depend on Laravel, Drupal, WordPress, or any other framework runtime.
This package is not published to Packagist. Install it from a Git repository by adding a Composer repositories entry in the consuming project:
{
"repositories": [
{
"type": "vcs",
"url": "git@github.com:your-org/dummy-json-users.git"
}
],
"require": {
"challenge/dummy-json-users": "dev-main"
}
}For local development, use a path repository instead:
{
"repositories": [
{
"type": "path",
"url": "../dummy-json-users",
"options": {
"symlink": true
}
}
],
"require": {
"challenge/dummy-json-users": "*"
}
}Then install dependencies:
composer install<?php
use Challenge\DummyJsonUsers\Service\UserService;
require __DIR__ . '/vendor/autoload.php';
$users = UserService::createDefault();
$user = $users->getUserById(1);
$page = $users->listUsers(page: 1, perPage: 10);
$newUserId = $users->addUser('Ada', 'Lovelace', 'ada@example.com');For dependency injection, pass any Symfony HttpClientInterface implementation:
use Challenge\DummyJsonUsers\Service\UserService;
use Symfony\Component\HttpClient\HttpClient;
$service = new UserService(HttpClient::create());The base URL can be overridden for testing or alternate environments:
$service = new UserService($httpClient, 'https://example.test/api');This package intentionally models only the user operations required by the assessment. For other DummyJSON endpoints, request() provides a small escape hatch without turning the package into a full SDK.
request() keeps package-controlled behavior for:
- base URL handling
- Symfony HTTP options
- failed status-code handling
- transport error wrapping
- JSON decoding
It returns the decoded JSON response as an array:
$carts = $users->request('GET', '/carts', [
'query' => [
'limit' => 10,
],
]);Custom requests do not map responses to DTOs. DTOs are only provided for the modeled user operations.
Users are returned as typed DTOs:
$user->id;
$user->firstName;
$user->lastName;
$user->email;
$user->toArray();
json_encode($user);Only the required fields are mapped from the API response:
idfirstNamelastNameemail
The service converts HTTP client and API failures into package-specific exceptions:
UserNotFoundApiRequestFailedInvalidApiResponse
This keeps remote API failures explicit for developers using the package.
Unit tests use Symfony's MockHttpClient, so they do not require the live DummyJSON API and do not depend on remote data remaining stable.
Run tests:
composer testRun static analysis:
composer analyseRun PSR-12 style checks:
composer styleValidate Composer metadata:
composer validate --strictThe included Dockerfile is only a local development helper. The package itself does not depend on Docker.
Build it:
docker build -t local-php .Run tools through the container:
docker run --rm -i --user "$(id -u):$(id -g)" -v "$PWD":/app local-php composer test