FlagBuilder
in package
A builder for feature flag configurations to be used with TestData.
Tags
Table of Contents
- __construct() : mixed
- addRule() : FlagBuilder
- booleanFlag() : FlagBuilder
- A shortcut for setting the flag to use the standard boolean configuration.
- build() : array<string|int, mixed>
- Creates a Feature Flag
- clearRules() : FlagBuilder
- Removes any existing rules from the flag. This undoes the effect of methods like `ifMatch()`.
- clearTargets() : FlagBuilder
- Removes any existing targets for individual user/context keys from the flag. This undoes the effect of the `variationForUser` and `variationForKey` methods.
- copy() : FlagBuilder
- Creates a deep copy of the flag builder. Subsequent updates to the original FlagBuilder object will not update the copy and vice versa.
- excludeFromSummaries() : FlagBuilder
- Control whether or not this flag should should be included in flag summary counts.
- fallthroughVariation() : FlagBuilder
- Specifies the fallthrough variation. The fallthrough is the value that is returned if targeting is on and the user was not matched by a more specific target or rule.
- getKey() : string
- Returns the key of the Flag Builder
- ifMatch() : FlagRuleBuilder
- Starts defining a flag rule, using the "is one of" operator.
- ifMatchContext() : FlagRuleBuilder
- Starts defining a flag rule, using the "is one of" operator. This matching expression only applies to contexts of a specific kind.
- ifNotMatch() : FlagRuleBuilder
- Starts defining a flag rule, using the "is not one of" operator.
- ifNotMatchContext() : FlagRuleBuilder
- Starts defining a flag rule, using the "is not one of" operator.
- migrationSettings() : FlagBuilder
- offVariation() : FlagBuilder
- Specifies the off variation for a boolean flag or index of variation.
- on() : FlagBuilder
- Sets targeting to be on or off for this flag.
- samplingRatio() : FlagBuilder
- Control the rate at which events from this flag will be sampled.
- valueForAll() : FlagBuilder
- Sets the flag to always return the specified variation value for all users.
- variationForAll() : FlagBuilder
- Sets the flag to always return the specified variation for all users.
- variationForBoolean() : int
- Converts a boolean to the corresponding variation index.
- variationForKey() : FlagBuilder
- Sets the flag to return the specified boolean variation for a specific context, identified by context kind and key, when targeting is on.
- variationForUser() : FlagBuilder
- Sets the flag to return the specified variation for a specific user key when targeting is on.
- variations() : FlagBuilder
- Changes the allowable variation values for the flag.
Methods
__construct()
public
__construct(string $key) : mixed
Parameters
- $key : string
Return values
mixed —addRule()
public
addRule(FlagRuleBuilder $flagRuleBuilder) : FlagBuilder
Parameters
- $flagRuleBuilder : FlagRuleBuilder
Return values
FlagBuilder —the flag builder
booleanFlag()
A shortcut for setting the flag to use the standard boolean configuration.
public
booleanFlag() : FlagBuilder
This is the default for all new flags created with TestData::flag().
The flag will have two variations, true
and false
(in that order);
it will return false
whenever targeting is off, and true
when targeting is on
if no other settings specify otherwise.
Return values
FlagBuilder —the flag builder
build()
Creates a Feature Flag
public
build(int $version) : array<string|int, mixed>
Parameters
- $version : int
Return values
array<string|int, mixed> —the feature flag
clearRules()
Removes any existing rules from the flag. This undoes the effect of methods like `ifMatch()`.
public
clearRules() : FlagBuilder
Return values
FlagBuilder —the same builder
clearTargets()
Removes any existing targets for individual user/context keys from the flag. This undoes the effect of the `variationForUser` and `variationForKey` methods.
public
clearTargets() : FlagBuilder
Return values
FlagBuilder —the same builder
copy()
Creates a deep copy of the flag builder. Subsequent updates to the original FlagBuilder object will not update the copy and vice versa.
public
copy() : FlagBuilder
Return values
FlagBuilder —A copy of the flag builder object
excludeFromSummaries()
Control whether or not this flag should should be included in flag summary counts.
public
excludeFromSummaries(bool $excludeFromSummaries) : FlagBuilder
Parameters
- $excludeFromSummaries : bool
Return values
FlagBuilder —fallthroughVariation()
Specifies the fallthrough variation. The fallthrough is the value that is returned if targeting is on and the user was not matched by a more specific target or rule.
public
fallthroughVariation(bool|int $variation) : FlagBuilder
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
Parameters
- $variation : bool|int
-
true or false or the desired fallthrough variation index
0
for the first,1
for the second, etc.
Return values
FlagBuilder —the flag builder
getKey()
Returns the key of the Flag Builder
public
getKey() : string
Return values
string —the key of the flag builder
ifMatch()
Starts defining a flag rule, using the "is one of" operator.
public
ifMatch(string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
This is a shortcut for calling FlagBuilder::ifMatchContext()
with LDContext::DEFAULT_KIND
as the context kind.
For example, this creates a rule that returns true
if the name is "Patsy" or "Edina":
$testData->flag("flag")
->ifMatch("name", "Patsy", "Edina")
->thenReturn(true);
Parameters
- $attribute : string
-
the user attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —call thenReturn(boolean)
or
thenReturn(int)
to finish the rule, or add more tests with another
method like andMatch()
ifMatchContext()
Starts defining a flag rule, using the "is one of" operator. This matching expression only applies to contexts of a specific kind.
public
ifMatchContext(string $contextKind, string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
For example, this creates a rule that returns true
if the name attribute for the
"company" context is "Ella" or "Monsoon":
$testData->flag("flag")
->ifMatchContext("company", "name", "Ella", "Monsoon")
->thenReturn(true);
Parameters
- $contextKind : string
-
the context kind
- $attribute : string
-
the context attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —call thenReturn(boolean)
or
thenReturn(int)
to finish the rule, or add more tests with another
method like andMatch()
ifNotMatch()
Starts defining a flag rule, using the "is not one of" operator.
public
ifNotMatch(string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
This is a shortcut for calling FlagBuilder::ifNotMatchContext()
with LDContext::DEFAULT_KIND
as the context kind.
For example, this creates a rule that returns true
if the name is neither "Saffron" nor "Bubble":
$testData->flag("flag") ->ifNotMatch("name", "Saffron", "Bubble") ->thenReturn(true);
Parameters
- $attribute : string
-
the context attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —call thenReturn(boolean)
or
thenReturn(int)
to finish the rule, or add more tests with another
method like andMatch()
ifNotMatchContext()
Starts defining a flag rule, using the "is not one of" operator.
public
ifNotMatchContext(string $contextKind, string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
This is a shortcut for calling FlagRuleBuilder::ifMatchContext()
with ContextKind::Default
as the context kind.
For example, this creates a rule that returns true
if the name attribute for the
"company" context is neither "Pendant" nor "Sterling Cooper":
$testData->flag("flag") ->ifNotMatchContext("company", "name", "Pendant", "Sterling Cooper") ->thenReturn(true);
Parameters
- $contextKind : string
-
the context kind
- $attribute : string
-
the context attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —call thenReturn(boolean)
or
thenReturn(int)
to finish the rule, or add more tests with another
method like andMatch()
migrationSettings()
public
migrationSettings(MigrationSettingsBuilder $builder) : FlagBuilder
Parameters
- $builder : MigrationSettingsBuilder
Return values
FlagBuilder —offVariation()
Specifies the off variation for a boolean flag or index of variation.
public
offVariation(bool|int $variation) : FlagBuilder
This is the variation that is returned whenever targeting is off.
Parameters
- $variation : bool|int
-
either boolean variation or integer index of variation
Return values
FlagBuilder —the flag builder
on()
Sets targeting to be on or off for this flag.
public
on(bool $on) : FlagBuilder
The effect of this depends on the rest of the flag configuration, just as it does on the real LaunchDarkly dashboard. In the default configuration that you get from calling TestData::flag() with a new flag key, the flag will return false whenever targeting is off, and true when targeting is on.
Parameters
- $on : bool
-
true if targeting should be on
Return values
FlagBuilder —the flag builder object
samplingRatio()
Control the rate at which events from this flag will be sampled.
public
samplingRatio(int $samplingRatio) : FlagBuilder
Parameters
- $samplingRatio : int
Return values
FlagBuilder —valueForAll()
Sets the flag to always return the specified variation value for all users.
public
valueForAll(mixed $value) : FlagBuilder
The value may be of any JSON-serializable type. This method changes the flag to have only a single variation, which is this value, and to return the same variation regardless of whether targeting is on or off. Any existing targets or rules are removed.
Parameters
- $value : mixed
-
the desired value to be returned for all users
Tags
Return values
FlagBuilder —the flag builder
variationForAll()
Sets the flag to always return the specified variation for all users.
public
variationForAll(bool|int $variation) : FlagBuilder
The variation is specified, targeting is switched on, and any existing targets or rules are removed. The fallthrough variation is set to the specified value. The off variation is left unchanged.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
Parameters
- $variation : bool|int
-
true
orfalse
or the desired variation index to return:0
for the first,1
for the second, etc.
Tags
Return values
FlagBuilder —the flag builder
variationForBoolean()
Converts a boolean to the corresponding variation index.
public
static variationForBoolean(bool $variation) : int
Parameters
- $variation : bool
-
the boolean variation value
Return values
int —the variation index for the given boolean
variationForKey()
Sets the flag to return the specified boolean variation for a specific context, identified by context kind and key, when targeting is on.
public
variationForKey(string $contextKind, string $key, bool|int $variation) : FlagBuilder
This has no effect when targeting is turned off for the flag.
Parameters
- $contextKind : string
- $key : string
-
string the context key
- $variation : bool|int
-
true
orfalse
or the desired variation index to return:0
for the first,1
for the second, etc.
Return values
FlagBuilder —the flag builder
variationForUser()
Sets the flag to return the specified variation for a specific user key when targeting is on.
public
variationForUser(string $userKey, bool|int $variation) : FlagBuilder
This is a shortcut for calling FlagBuilder::variationForKey()
with LDContext::DEFAULT_KIND
as the context kind.
This has no effect when targeting is turned off for the flag.
Parameters
- $userKey : string
-
string a user key
- $variation : bool|int
-
true
orfalse
or the desired variation index to return:0
for the first,1
for the second, etc.
Tags
Return values
FlagBuilder —the flag builder
variations()
Changes the allowable variation values for the flag.
public
variations(array<string|int, mixed> ...$variations) : FlagBuilder
The values may be of any JSON-serializable types. For instance, a boolean flag normally has true, false; a string-valued flag might have 'red', 'green'; etc.
Example: A single variation
$td->flag('new-flag')->variations(true)
Example: Multiple variations
$td->flag('new-flag')->variations('red', 'green', 'blue')
Parameters
- $variations : array<string|int, mixed>
-
the the desired variations
Return values
FlagBuilder —the flag builder object