Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/techlord001/user-bridge
A service for retrieving and creating users via a remote API
https://github.com/techlord001/user-bridge
api composer guzzlehttp library php php-di php8 phpunit
Last synced: 23 days ago
JSON representation
A service for retrieving and creating users via a remote API
- Host: GitHub
- URL: https://github.com/techlord001/user-bridge
- Owner: techlord001
- Created: 2023-10-15T13:55:23.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-17T12:17:59.000Z (about 1 year ago)
- Last Synced: 2024-10-31T04:22:32.404Z (2 months ago)
- Topics: api, composer, guzzlehttp, library, php, php-di, php8, phpunit
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UserBridge Library
UserBridge is a PHP library that provides a streamlined interface for interacting with user data from [ReqRes](https://reqres.in/). 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.
## Installation
Clone the repository and navigate to the project directory. Install the dependencies using Composer:
```bash
composer install
```## Running Tests
Execute the PHPUnit tests to ensure the library's functionality:
```bash
./vendor/bin/phpunit tests
```Replace `tests` with the path to your test files if they are located elsewhere.
## Usage
### Building the Project
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.
### Using the `UserService` Class
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:
```bash
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:
```php
// 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);
```### Available Methods in `UserService`
- `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.### Explanation of Core Classes
- `UserService`: This is the main service class that provides methods to interact with the user data. It depends on the `ListUsersRetriever`, `SingleUserRetriever`, `UserCreator`, and `UserSerialiser` 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.
### Connecting to ReqRes API
This library is configured to connect to the [ReqRes API](https://reqres.in/) by default. It provides a mock API for testing and development purposes, offering a collection of endpoints that simulate real-world API behavior.