fetzi/flipt-php

A PHP package for integrating the Flipt REST API

Fund package maintenance!
fetzi

v0.1.0 2021-05-26 14:03 UTC

This package is auto-updated.

Last update: 2024-04-18 12:19:39 UTC


README

GitHub Workflow Status (main) Total Downloads License

flipt-php is a small wrapper package for the Flipt REST API to be able to easily integrate Flipt into your PHP applications.

Installation

composer require fetzi/flipt-php

Usage

The Flipt class uses HTTPlug a HTTP Client abstraction to make the API requests. You have to pass a base URL to the static create function. Everything else (HttpClient, RequestFactory & StreamFactory) is automatically determined.

$flipt = Flipt::create('http://localhost:8080');

By calling the evaluate method you can check if a certain user (entity) should get a certain feature or not. To perform a evaluation you need to create a EvaluateRequest that contains the data to evaluate.

$evaluateRequest = new EvaluateRequest('sample-flag', 'user-id', ['foo' => 'bar']);
$evaluateResponse = $flipt->evaluate($evaluateRequest);

if ($evaluateResponse->isMatch()) {
    // awesome new feature
} else {
    // old boring feature
}

Variants

To be able to determine which variant of a feature-flag should be displayed you need to use the getVariant() method on the response.

The EvaluateResponse also provides a variant that can be accessed via the getValue() or getVariant() method:

$evaluateRequest = new EvaluateRequest('sample-flag', 'user-id', ['foo' => 'bar']);
$evaluateResponse = $flipt->evaluate($evaluateRequest);

if ($evaluateResponse->isMatch()) {
    switch ($evaluateResponse->getVariant()) {
        case 'a':
            // show A variant
            break;
        case 'b':
            // show B variant
            break;
    }
}

flipt-php was created by Johannes Pichler under the MIT license.