UserBridge is a PHP library that provides a streamlined interface for interacting with user data from ReqRes. It encapsulates a set of classes and methods to retrieve, create, and serialize user data, offering a simplified and efficient way to interact with the ReqRes API.
Clone the repository and navigate to the project directory. Install the dependencies using Composer:
composer install
Execute the PHPUnit tests to ensure the library's functionality:
./vendor/bin/phpunit tests
Replace tests
with the path to your test files if they are located elsewhere.
The library is structured around core classes that facilitate interaction with the ReqRes API. The UserService
class is central, offering methods to retrieve, create, and serialize users.
The index.php
file included in the library provides a demonstration of how to use the UserService
class. You can run it using the following command:
php index.php
This will execute a series of operations, including retrieving a user by ID, fetching a paginated list of users, creating a new user, and serializing user data.
Here is a basic example of how to use the UserService
class:
// Get the UserService from the DI container
$userService = $container->get(UserBridge\Services\UserService::class);
// Retrieve a user by ID
$user = $userService->getUserById(1);
// Retrieve a paginated list of users
$users = $userService->getPaginatedUsers(2);
// Create a new user
$createdUserId = $userService->createUser('John Doe', 'Developer');
// Serialize a user to an array
$array = $userService->serialiseUserToArray($user);
// Serialize a user to JSON
$json = $userService->serialiseUserToJson($user);
getUserById(int $id): User
: Retrieve a user by their ID.getPaginatedUsers(int $page = 1): Users
: Fetch a paginated list of users.createUser(string $name, string $job): int
: Create a new user and return their ID.serialiseUserToArray(User $user): array
: Serialize a user object to an array.serialiseUserToJson(User $user): string
: Serialize a user object to a JSON string.
-
UserService
: This is the main service class that provides methods to interact with the user data. It depends on theListUsersRetriever
,SingleUserRetriever
,UserCreator
, andUserSerialiser
classes to perform various operations. -
ListUsersRetriever
: Retrieves a paginated list of users from the ReqRes API. -
SingleUserRetriever
: Retrieves a single user by their ID from the ReqRes API. -
UserCreator
: Creates a new user on the ReqRes platform and returns the ID of the newly created user. -
UserSerialiser
: Converts a User object into an array or JSON format for easy handling and response formatting.
This library is configured to connect to the ReqRes API by default. It provides a mock API for testing and development purposes, offering a collection of endpoints that simulate real-world API behavior.