Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Feature
public $rules = [];

/**
* @param array{defaultValue:T,rules:?array{condition:?array<string,mixed>,coverage:?float,force:?T,variations:?T[],key:?string,weights:?float[],namespace:?array{0:string,1:float,2:float},hashAttribute:?string,fallbackAttribute:?string,disableStickyBucketing:?bool,bucketVersion:?int,minBucketVersion:?int,parentConditions:?array<string,mixed>}[]} $feature
* @param array{defaultValue:T,rules:?array{condition:?array<string,mixed>,coverage:?float,force:?T,variations:?T[],key:?string,weights:?float[],namespace:?array{0:string,1:float,2:float},hashAttribute:?string,fallbackAttribute:?string,disableStickyBucketing:?bool,bucketVersion:?int,minBucketVersion:?int,parentConditions:?array<string,mixed>,customFields:?array<string,mixed>}[]} $feature
*/
public function __construct(array $feature)
{
Expand Down
11 changes: 10 additions & 1 deletion src/FeatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ class FeatureRule
/** @var null|array<string, mixed> */
public $parentConditions;

/** @var null|array<string, mixed> */
public $customFields;

/**
* @param array{id?:string,condition:?array<string,mixed>,coverage:?float,force:?T,variations:?T[],key:?string,weights:?float[],namespace:?array{0:string,1:float,2:float},hashAttribute:?string,fallbackAttribute:?string,filters?:array{seed:string,ranges:array{0:float,1:float}[],hashVersion?:int,attribute?:string}[],seed?:string,hashVersion?:int,range?:array{0:float,1:float},meta?:array{key?:string,name?:string,passthrough?:bool}[],ranges?:array{0:float,1:float}[],name?:string,phase?:string,disableStickyBucketing:?bool,bucketVersion:?int,minBucketVersion:?int,parentConditions:?array<string,mixed>} $rule
* @param array{id?:string,condition:?array<string,mixed>,coverage:?float,force:?T,variations:?T[],key:?string,weights:?float[],namespace:?array{0:string,1:float,2:float},hashAttribute:?string,fallbackAttribute:?string,filters?:array{seed:string,ranges:array{0:float,1:float}[],hashVersion?:int,attribute?:string}[],seed?:string,hashVersion?:int,range?:array{0:float,1:float},meta?:array{key?:string,name?:string,passthrough?:bool}[],ranges?:array{0:float,1:float}[],name?:string,phase?:string,disableStickyBucketing:?bool,bucketVersion:?int,minBucketVersion:?int,parentConditions:?array<string,mixed>,customFields:?array<string,mixed>} $rule
*/
public function __construct(array $rule)
{
Expand Down Expand Up @@ -126,6 +129,9 @@ public function __construct(array $rule)
if (array_key_exists("parentConditions", $rule)) {
$this->parentConditions = $rule["parentConditions"];
}
if (array_key_exists("customFields", $rule)) {
$this->customFields = $rule["customFields"];
}
}

/**
Expand Down Expand Up @@ -191,6 +197,9 @@ public function toExperiment(string $featureKey): ?InlineExperiment
if (isset($this->parentConditions)) {
$exp->parentConditions = $this->parentConditions;
}
if (isset($this->customFields)) {
$exp->customFields = $this->customFields;
}

return $exp;
}
Expand Down
8 changes: 7 additions & 1 deletion src/InlineExperiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class InlineExperiment
*/
public $parentConditions;

/**
* @var array<string,mixed>|null
*/
public $customFields;

/**
* @param string $key
* @param T[] $variations
Expand All @@ -67,7 +72,7 @@ public static function create(string $key, $variations): InlineExperiment
/**
* @param string $key
* @param T[] $variations
* @param array{weights?: float[], coverage?: float, force?: int|null, active?: bool, condition?: array<string, mixed>, namespace?: array{0: string, 1: float, 2: float}, hashAttribute?: string, fallbackAttribute?: string, filters?: array{seed: string, ranges: array{0: float, 1: float}[], hashVersion?: int, attribute?: string}[], seed?: string, hashVersion?: int, meta?: array{key?: string, name?: string, passthrough?: bool}[], ranges?: array{0: float, 1: float}[], name?: string, phase?: string, disableStickyBucketing?: bool, bucketVersion?: int, minBucketVersion?: int} $options
* @param array{weights?: float[], coverage?: float, force?: int|null, active?: bool, condition?: array<string, mixed>, namespace?: array{0: string, 1: float, 2: float}, hashAttribute?: string, fallbackAttribute?: string, filters?: array{seed: string, ranges: array{0: float, 1: float}[], hashVersion?: int, attribute?: string}[], seed?: string, hashVersion?: int, meta?: array{key?: string, name?: string, passthrough?: bool}[], ranges?: array{0: float, 1: float}[], name?: string, phase?: string, disableStickyBucketing?: bool, bucketVersion?: int, minBucketVersion?: int, customFields?: array<string, mixed>} $options
*/
public function __construct(string $key, $variations, array $options = [])
{
Expand All @@ -91,6 +96,7 @@ public function __construct(string $key, $variations, array $options = [])
$this->bucketVersion = $options["bucketVersion"] ?? null;
$this->minBucketVersion = $options["minBucketVersion"] ?? null;
$this->parentConditions = $options["parentConditions"] ?? null;
$this->customFields = $options["customFields"] ?? null;
$this->fallbackAttribute = null;
if (!is_null($this->disableStickyBucketing)) {
$this->fallbackAttribute = $options["fallbackAttribute"] ?? null;
Expand Down
42 changes: 42 additions & 0 deletions tests/GrowthbookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,46 @@ public function regexOperatorProvider(): array
'$regexi - invalid pattern' => ['$regexi', 'Hello', '[', false],
];
}

public function testInlineExperimentCustomFields(): void
{
$customFields = ['badge' => 'new', 'priority' => 1];

$exp = new InlineExperiment('my-test', [0, 1], ['customFields' => $customFields]);

$this->assertSame($customFields, $exp->customFields);
}

public function testInlineExperimentCustomFieldsDefaultNull(): void
{
$exp = new InlineExperiment('my-test', [0, 1]);

$this->assertNull($exp->customFields);
}

public function testFeatureRuleCustomFieldsPassedToExperiment(): void
{
$customFields = ['color' => 'blue', 'weight' => 2.5];

/** @phpstan-ignore-next-line */
$rule = new \Growthbook\FeatureRule(['variations' => [false, true], 'customFields' => $customFields]);

$exp = $rule->toExperiment('test-feature');

$this->assertNotNull($exp);
assert($exp !== null);
$this->assertSame($customFields, $exp->customFields);
}

public function testFeatureRuleWithoutCustomFieldsProducesNullOnExperiment(): void
{
/** @phpstan-ignore-next-line */
$rule = new \Growthbook\FeatureRule(['variations' => [false, true]]);

$exp = $rule->toExperiment('test-feature');

$this->assertNotNull($exp);
assert($exp !== null);
$this->assertNull($exp->customFields);
}
}