Download the PHP package usmanzahid/service-response without Composer

On this page you can find all versions of the php package usmanzahid/service-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package service-response

Service Response

A minimal and structured way to handle internal service communication in PHP applications.

Instead of returning null, false, throwing exceptions for expected failures, or using loosely structured arrays, ServiceResponse provides a consistent object containing success status, message, data, and errors. This standardization improves service orchestration, error handling, debugging, and API response consistency.

The class supports response chaining, allowing higher-level services to preserve and expose root causes from lower-level operations.


Includes


Response Structure

[
    'success' => true|false,
    'message' => ?string,
    'data' => mixed,
    'errors' => array,
]

Basic Usage

Success Response

return (new ServiceResponse())
    ->withMessage('Payment processed successfully')
    ->withData(['transaction_id' => $transactionId]);

Failure Response

return (new ServiceResponse())
    ->withSuccess(false)
    ->withMessage('Payment failed')
    ->withError('card', 'Card declined');

Adding Multiple Errors

return (new ServiceResponse())
    ->withSuccess(false)
    ->withMessage('Validation failed')
    ->withErrors([
        'email' => ['Email is required'],
        'password' => ['Password must be at least 8 characters'],
    ]);

Chaining Responses Between Services

When one service depends on another, you can attach the previous response to preserve context.

$paymentResponse = $paymentService->charge($order);

if ($paymentResponse->wasNotSuccessful()) {
    return (new ServiceResponse())
        ->withSuccess(false)
        ->withMessage('Order failed')
        ->withPrevious($paymentResponse);
}

return (new ServiceResponse())
    ->withMessage('Order completed')
    ->withPrevious($paymentResponse);

Accessing Aggregated Errors

$errors = $response->getAllErrors();

Getting the Root Cause

$root = $response->getRootCause();

Helper Functions

Helper functions provide a concise way to create responses without instantiating the class directly.

Success Helper

return service_response_success(
    data: ['user_id' => $user->id],
    message: 'User authenticated'
);

Failure Helper

return service_response_fail(
    errors: ['auth' => ['Invalid credentials']],
    message: 'Authentication failed'
);

From Exception

try {
    $gateway->charge($card);
} catch (Throwable $e) {
    return service_response_from_exception($e);
}

Real-World Examples

Authentication Service

public function authenticate(string $email, string $password): ServiceResponse
{
    $user = $this->repo->findByEmail($email);

    if (!$user || !$this->hasher->check($password, $user->password)) {
        return service_response_fail(
            ['auth' => ['Invalid credentials']],
            'Authentication failed'
        );
    }

    return service_response_success(
        ['user_id' => $user->id],
        'Authenticated successfully'
    );
}

Payment Service

public function charge(Order $order): ServiceResponse
{
    if ($order->total <= 0) {
        return service_response_fail(
            ['amount' => ['Invalid order total']],
            'Payment failed'
        );
    }

    $transactionId = $this->gateway->charge($order);

    return service_response_success(
        ['transaction_id' => $transactionId],
        'Payment processed'
    );
}

Orchestrating Multiple Services

$auth = $authService->authenticate($email, $password);

if ($auth->wasNotSuccessful()) {
    return $auth;
}

$payment = $paymentService->charge($order);

if ($payment->wasNotSuccessful()) {
    return (new ServiceResponse())
        ->withSuccess(false)
        ->withMessage('Checkout failed')
        ->withPrevious($payment);
}

return (new ServiceResponse())
    ->withMessage('Checkout completed')
    ->withPrevious($payment);

JSON Serialization

ServiceResponse implements JsonSerializable, allowing it to be safely encoded:

Frameworks like Laravel will automatically serialize the response:


Exceptionless Flow Control

Use the response status instead of exceptions for expected outcomes.

$response = $paymentService->charge($order);

if ($response->wasSuccessful()) {
    // proceed with fulfillment
    $fulfillmentService->dispatch($order);
} else {
    // log and return structured failure
    logger()->warning('Payment failed', $response->toArray());
    return $response;
}

Notes


All versions of service-response with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package usmanzahid/service-response contains the following files

Loading the files please wait ...