Skip to content

Commit 7be632b

Browse files
Adding Custom Fields Support
1 parent fd81415 commit 7be632b

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
on:
22
push:
3-
pull_request:
4-
schedule:
5-
- cron: '0 0 */3 * *'
63
name: CI
74

85
jobs:

src/Context/Context.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ABSmartly\SDK\Assignment;
66
use ABSmartly\SDK\AudienceMatcher;
7+
use ABSmartly\SDK\ContextCustomFieldValue;
78
use ABSmartly\SDK\Exception\InvalidArgumentException;
89
use ABSmartly\SDK\Exception\LogicException;
910
use ABSmartly\SDK\Experiment;
@@ -44,6 +45,7 @@ class Context {
4445

4546
private array $index;
4647
private array $indexVariables;
48+
private array $contextCustomFields;
4749

4850

4951
private array $hashedUnits;
@@ -124,6 +126,8 @@ private function setData(ContextData $data): void {
124126
$this->data = $data;
125127
$this->index = [];
126128
$this->indexVariables = [];
129+
$this->contextCustomFields = [];
130+
$experimentCustomFields = [];
127131

128132
foreach ($data->experiments as $experiment) {
129133
$experimentVariables = new ExperimentVariables();
@@ -148,7 +152,35 @@ private function setData(ContextData $data): void {
148152
$experimentVariables->variables[] = $parsed;
149153
}
150154

155+
if (
156+
property_exists($experiment, 'customFieldValues') &&
157+
$experiment->customFieldValues !== null) {
158+
$experimentCustomFields = [];
159+
foreach ($experiment->customFieldValues as $customFieldValue) {
160+
$type = $customFieldValue->type;
161+
$value = new ContextCustomFieldValue();
162+
$value->type = $type;
163+
164+
if ($customFieldValue->value !== null) {
165+
$customValue = $customFieldValue->value;
166+
167+
if (strpos($type, 'json') > -1) {
168+
$value->value = $this->variableParser->parse($experiment->name, $customValue);
169+
} else if (strpos($type, 'boolean') > -1) {
170+
$value->value = settype($customValue, 'boolean');
171+
} else if (strpos($type, 'number') > -1) {
172+
$value->value = settype($customValue, 'int');
173+
} else {
174+
$value->value = $customValue;
175+
}
176+
}
177+
$experimentCustomFields[$customFieldValue->name] = $value;
178+
}
179+
}
180+
181+
151182
$this->index[$experiment->name] = $experimentVariables;
183+
$this->contextCustomFields[$experiment->name] = $experimentCustomFields;
152184
}
153185
}
154186

src/ContextCustomFieldValue.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ABSmartly\SDK;
4+
5+
class ContextCustomFieldValue {
6+
public string $type;
7+
public $value;
8+
}

src/Experiment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Experiment {
2525
public bool $audienceStrict;
2626
public array $applications;
2727
public array $variants;
28+
public ?array $customFieldValues;
2829

2930
public function __construct(object $data) {
3031
if (!empty($data->audience)) {
@@ -34,6 +35,7 @@ public function __construct(object $data) {
3435
$this->audience = null;
3536
}
3637

38+
$this->customFieldValues = null;
3739

3840
$data = get_object_varsAlias($data);
3941
foreach ($data as $field => $value) {

tests/Context/ContextTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,38 @@ public function testGetVariableKeys(): void {
505505
self::assertEquals($this->variableExperiments, $context->getVariableKeys());
506506
}
507507

508+
public function testGetFieldValueKeys(): void{
509+
$context = $this->createReadyContext();
510+
self::assertEquals(['country', 'languages', 'overrides'], $context->getCustomFieldKeys());
511+
}
512+
513+
public function testGetFieldValueValues(): void {
514+
$context = $this->createReadyContext();
515+
self::assertEquals(null, $context->getCustomFieldValue('not_found', 'not_found'));
516+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_ab', 'not_found'));
517+
self::assertEquals("US,PT,ES,DE,FR", $context->getCustomFieldValue('exp_test_ab', 'country'));
518+
self::assertEquals((object)array('123' => 1, '456' => 0), $context->getCustomFieldValue('exp_test_ab', 'overrides'));
519+
self::assertEquals("json", $context->getCustomFieldValueType('exp_test_ab', 'overrides'));
520+
521+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_ab', 'languages'));
522+
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_ab', 'languages'));
523+
524+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_abc', 'overrides'));
525+
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_abc', 'overrides'));
526+
527+
self::assertEquals("en-US,en-GB,pt-PT,pt-BR,es-ES,es-MX", $context->getCustomFieldValue('exp_test_abc', 'languages'));
528+
self::assertEquals("string", $context->getCustomFieldValueType('exp_test_abc', 'languages'));
529+
530+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'country'));
531+
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'country'));
532+
533+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'overrides'));
534+
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'overrides'));
535+
536+
self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'languages'));
537+
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'languages'));
538+
}
539+
508540
public function testPeekTreatmentReturnsOverrideVariant(): void {
509541
$context = $this->createReadyContext();
510542

tests/Fixtures/json/context.json

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,27 @@
2525
],
2626
"variants":[
2727
{
28-
"name":"A",
28+
"name":"J",
2929
"config":null
3030
},
3131
{
3232
"name":"B",
3333
"config":"{\"banner.border\":1,\"banner.size\":\"large\"}"
3434
}
3535
],
36-
"audience": null
36+
"audience": null,
37+
"customFieldValues": [
38+
{
39+
"name": "country",
40+
"value": "US,PT,ES,DE,FR",
41+
"type": "string"
42+
},
43+
{
44+
"name": "overrides",
45+
"value": "{\"123\":1,\"456\":0}",
46+
"type": "json"
47+
}
48+
]
3749
},
3850
{
3951
"id":2,
@@ -73,7 +85,19 @@
7385
"config":"{\"button.color\":\"red\"}"
7486
}
7587
],
76-
"audience": ""
88+
"audience": "",
89+
"customFieldValues": [
90+
{
91+
"name": "country",
92+
"value": "US,PT,ES,DE,FR",
93+
"type": "string"
94+
},
95+
{
96+
"name": "languages",
97+
"value": "en-US,en-GB,pt-PT,pt-BR,es-ES,es-MX",
98+
"type": "string"
99+
}
100+
]
77101
},
78102
{
79103
"id":3,
@@ -113,7 +137,8 @@
113137
"config":"{\"card.width\":\"75%\"}"
114138
}
115139
],
116-
"audience": "{}"
140+
"audience": "{}",
141+
"customFieldValues": null
117142
},
118143
{
119144
"id":4,

0 commit comments

Comments
 (0)