FlagRuleBuilder
in package
A builder for feature flag rules to be used with FlagBuilder.
In the LaunchDarkly model, a flag can have any number of rules, and a rule can have any number of clauses. A clause is an individual test such as "name is 'X'". A rule matches a user if all of the rule's clauses match the user.
To start defining a rule, use one of the flag builder's matching methods such as
ifMatch('country', 'us'...). This defines the first clause for the rule.
Optionally, you may add more clauses with the rule builder's methods such as
andMatch('age', '20'...). Finally, call thenReturn(boolean) or
thenReturn(int) to finish defining the rule.
Tags
Table of Contents
- __construct() : mixed
- andMatch() : FlagRuleBuilder
- Adds another clause, using the "is one of" operator.
- andMatchContext() : FlagRuleBuilder
- Adds another clause, using the "is one of" operator. This matching expression only applies to contexts of a specific kind.
- andNotMatch() : FlagRuleBuilder
- Adds another clause, using the "is not one of" operator.
- andNotMatchContext() : FlagRuleBuilder
- Adds another clause, using the "is not one of" operator. This matching expression only applies to contexts of a specific kind.
- build() : array<string|int, mixed>
- Creates an associative array representation of the flag
- thenReturn() : FlagBuilder
- Finishes defining the rule, specifying the result value as a boolean or variation index.
Methods
__construct()
public
__construct(FlagBuilder $flagBuilder) : mixed
Parameters
- $flagBuilder : FlagBuilder
Return values
mixed —andMatch()
Adds another clause, using the "is one of" operator.
public
andMatch(string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
This is a shortcut for calling FlagRuleBuilder::andMatchContext()
with LDContext::DEFAULT_KIND as the context kind.
For example, this creates a rule that returns true if the name is "Patsy" and the
country is "gb":
$testData->flag("flag")
->ifMatch("name", "Patsy")
->andMatch("country", "gb")
->thenReturn(true);
Parameters
- $attribute : string
-
the user attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —the rule builder
andMatchContext()
Adds another clause, using the "is one of" operator. This matching expression only applies to contexts of a specific kind.
public
andMatchContext(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", and the country attribute for the "company" context is "gb":
$testData->flag("flag")
->ifMatchContext("company", "name", "Ella")
->andMatchContext("company", "country", "gb")
->thenReturn(true);
Parameters
- $contextKind : string
- $attribute : string
-
the user attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —the rule builder
andNotMatch()
Adds another clause, using the "is not one of" operator.
public
andNotMatch(string $attribute, array<string|int, mixed> ...$values) : FlagRuleBuilder
This is a shortcut for calling FlagRuleBuilder::andNotMatchContext()
withLDContext::DEFAULT_KIND as the context kind.
For example, this creates a rule that returns true if
the name is "Patsy" and the country is not "gb":
$testData->flag("flag") ->ifMatch("name", "Patsy") ->andNotMatch("country", "gb") ->thenReturn(true);
Parameters
- $attribute : string
-
the user attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —the rule builder
andNotMatchContext()
Adds another clause, using the "is not one of" operator. This matching expression only applies to contexts of a specific kind.
public
andNotMatchContext(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", and the country attribute for the "company" context is not "gb":
$testData->flag("flag") ->ifMatchContext("company", "name", "Ella") ->andNotMatchContext("company", "country", "gb") ->thenReturn(true);
Parameters
- $contextKind : string
- $attribute : string
-
the user attribute to match against
- $values : array<string|int, mixed>
-
values to compare to
Return values
FlagRuleBuilder —the rule builder
build()
Creates an associative array representation of the flag
public
build(int $id) : array<string|int, mixed>
Parameters
- $id : int
-
the rule id
Return values
array<string|int, mixed> —the array representation of the flag
thenReturn()
Finishes defining the rule, specifying the result value as a boolean or variation index.
public
thenReturn(bool|int $variation) : FlagBuilder
Parameters
- $variation : bool|int
-
the value to return if the rule matches the user
Return values
FlagBuilder —the flag builder