飞艇/飞艇-php

Airship PHP SDK

0.3.0 2018-09-28 18:06 UTC

This package is not auto-updated.

Last update: 2019-07-20 02:25:50 UTC


README

Airship

Airship PHP

需求

PHP 5.5 或更高版本

先决条件

此 SDK 与 Airship 微服务配合使用。在进行下一步之前,请参阅其文档。

内容

01 安装

php composer.phar require airship/airship-php

02 关键概念

在飞艇中,功能 标志 控制流量到通用对象(称为 实体)。最常用的实体类型是 User,但它们也可以是其他东西(例如 PageGroupTeamApp 等)。默认情况下,所有实体都具有 User 类型。

实体可以通过字典或使用 Entity 类来表示。

03 配置标志

要配置飞艇,我们需要传递一个新的客户端实例。

require 'vendor/autoload.php';

// Create an instance with an env key
$airship = new Airship\Airship(new Airship\Client\GuzzleClient('<env_key>'));

04 使用方法

if ($airship->flag('bitcoin-pay')->isEnabled(['id' => 5])) {
  // ...
}

// Define your entity
$entity = [
  'type' => 'User', // 'type' starts with a capital letter '[U]ser', '[H]ome', '[C]ar'. If omittied, it will default to 'User'
  'id' => '1234', // 'id' must be a string or integer
  'displayName' => 'ironman@stark.com', // must be a string. If omitted, the SDK will use the same value as 'id' (converted to a string)
];
// or
$entity = new Entity(1234, 'User', 'ironman@stark.com');

// The most compact form can be:
$entity = [
  'id' => 1234
];
// or
$entity = new Entity(1234);

// as this will translate into:
$entity = [
  'type' => 'User',
  'id' => '1234',
  'displayName' => '1234',
];

$airship->flag('bitcoin-pay')->isEnabled($entity); // Does the entity have the feature 'bitcoin-pay'?
$airship->flag('bitcoin-pay')->getTreatment($entity); // Get the treatment associated with the flag
$airship->flag('bitcoin-pay')->isEligible($entity);
// Returns true if the entity can potentially receive the feature via sampling
// or is already receiving the feature.

// Note: It may take up to a minute for entities gated to show up on our web app.

属性(用于复杂定位)

// Define your entity with an attributes dictionary of key-value pairs.
// Values must be a string, a number, or a boolean. nil values are not accepted.
// For date or datetime string value, use iso8601 format.
$entity = [
  'type' => 'User',
  'id' => '1234',
  'displayName' => 'ironman@stark.com',
  'attributes' => [
    't_shirt_size' => 'M',
    'date_created' => '2018-02-18',
    'time_converted' => '2018-02-20T21:54:00.630815+00:00',
    'owns_property' => true,
    'age' => 39,
  ],
];
// or
$entity = new Entity(
  1234,
  'User',
  'ironman@stark.com',
  [
    't_shirt_size' => 'M',
    'date_created' => '2018-02-18',
    'time_converted' => '2018-02-20T21:54:00.630815+00:00',
    'owns_property' => true,
    'age' => 39,
  ]
);

// Now in app.airshiphq.com, you can target this particular user using its
// attributes

组(用于类似成员的级联行为)

// An entity can be a member of a group.
// The structure of a group entity is just like that of the base entity.
$entity = [
  'type' => 'User',
  'id' => '1234',
  'displayName' => 'ironman@stark.com',
  'attributes' => [
    't_shirt_size' => 'M',
    'date_created' => '2018-02-18',
    'time_converted' => '2018-02-20T21:54:00.630815+00:00',
    'owns_property' => true,
    'age' => 39,
  ],
  'group' => [
    'type' => 'Club',
    'id' => '5678',
    'displayName' => 'SF Homeowners Club',
    'attributes' => [
      'founded' => '2016-01-01',
      'active' => true,
    ],
  ],
];
// or
$group = new Entity(
  5678,
  'Club',
  'SF Homeowners Club',
  [
    'founded' => '2016-01-01',
    'active' => true,
  ]
);
$user = new Entity(
  1234,
  'User',
  'ironman@stark.com',
  [
    't_shirt_size' => 'M',
    'date_created' => '2018-02-18',
    'time_converted' => '2018-02-20T21:54:00.630815+00:00',
    'owns_property' => true,
    'age' => 39,
  ],
  $group
);

// Inheritance of values `isEnabled`, `getTreatment`, `getPayload`, and `isEligible` works as follows:
// 1. If the group is enabled, but the base entity is not,
//    then the base entity will inherit the values `isEnabled`, `getTreatment`, `getPayload`, and `isEligible` of the group entity.
// 2. If the base entity is explicitly blacklisted, then it will not inherit.
// 3. If the base entity is not given a variation in rule-based variation assignment,
//    but the group is and both are enabled, then the base entity will inherit
//    the variation of the group's.


// You can ask questions about the group directly (use the `is_group` flag):
$entity = [
  'isGroup' => true,
  'type' => 'Club',
  'id' => '5678',
  'displayName' => 'SF Homeowners Club',
  'attributes' => [
    'founded' => '2016-01-01',
    'active' => true,
  ],
];

$airship->flag('bitcoin-pay')->isEnabled($entity);

贡献

依赖项管理使用 Composer,以下文档假定 composer 已安装在您的可执行路径上。

代码风格

强制执行 PSR-2 代码风格。使用 composer check-style 检查代码风格,并使用 composer fix-style 修复它。

运行测试

测试通过 PhpUnit 运行

composer test

许可证

MIT

StackShare