Skip to content

Adding Custom Fields Support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
on:
push:
pull_request:
schedule:
- cron: '0 0 */3 * *'
name: CI

jobs:
Expand Down
78 changes: 78 additions & 0 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ABSmartly\SDK\Assignment;
use ABSmartly\SDK\AudienceMatcher;
use ABSmartly\SDK\ContextCustomFieldValue;
use ABSmartly\SDK\Exception\InvalidArgumentException;
use ABSmartly\SDK\Exception\LogicException;
use ABSmartly\SDK\Experiment;
Expand Down Expand Up @@ -44,6 +45,7 @@ class Context {

private array $index;
private array $indexVariables;
private array $contextCustomFields;


private array $hashedUnits;
Expand Down Expand Up @@ -124,6 +126,8 @@ private function setData(ContextData $data): void {
$this->data = $data;
$this->index = [];
$this->indexVariables = [];
$this->contextCustomFields = [];
$experimentCustomFields = [];

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

if (
property_exists($experiment, 'customFieldValues') &&
$experiment->customFieldValues !== null) {
$experimentCustomFields = [];
foreach ($experiment->customFieldValues as $customFieldValue) {
$type = $customFieldValue->type;
$value = new ContextCustomFieldValue();
$value->type = $type;

if ($customFieldValue->value !== null) {
$customValue = $customFieldValue->value;

if (strpos($type, 'json') > -1) {
$value->value = $this->variableParser->parse($experiment->name, $customValue);
} else if (strpos($type, 'boolean') > -1) {
$value->value = settype($customValue, 'boolean');
} else if (strpos($type, 'number') > -1) {
$value->value = settype($customValue, 'int');
} else {
$value->value = $customValue;
}
}
$experimentCustomFields[$customFieldValue->name] = $value;
}
}


$this->index[$experiment->name] = $experimentVariables;
$this->contextCustomFields[$experiment->name] = $experimentCustomFields;
}
}

Expand Down Expand Up @@ -318,6 +350,52 @@ public function getVariableValue(string $key, $defaultValue = null) {
return $assignment->variables->{$key} ?? $defaultValue;
}

public function getCustomFieldKeys(): array{
$return = [];
foreach ($this->data->experiments as $experiment) {
if (property_exists($experiment, 'customFieldValues')) {
$customFieldValues = $experiment->customFieldValues;
if ($customFieldValues != null) {
foreach ($experiment->customFieldValues as $customFieldValue) {
$return[] = $customFieldValue->name;
}
}
}
}
$return = array_unique($return);
sort($return);
return $return;
}

public function getCustomFieldValue(string $environmentName, string $key){
if (array_key_exists($environmentName, $this->contextCustomFields)) {
$customFieldValues = $this->contextCustomFields[$environmentName];
if (array_key_exists($key, $customFieldValues)) {
$field = $customFieldValues[$key];
if ($field != null) {
return $field->value;
}
}
}

return null;
}

public function getCustomFieldValueType(string $environmentName, string $key)
{
if (array_key_exists($environmentName, $this->contextCustomFields)) {
$customFieldValues = $this->contextCustomFields[$environmentName];
if (array_key_exists($key, $customFieldValues)) {
$field = $customFieldValues[$key];
if ($field != null) {
return $field->type;
}
}
}

return null;
}

public function getVariableKeys(): array {
$return = [];
foreach ($this->indexVariables as $variable => $experimentVars) {
Expand Down
8 changes: 8 additions & 0 deletions src/ContextCustomFieldValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ABSmartly\SDK;

class ContextCustomFieldValue{
public string $type;
public $value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the type is not mandatory right? but if you put it, you get some help from the compiler?
Another thing: the value seems to be badly formatted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with PHP but is not required the type. I tried with object but don't work when I need to assign integer, booleans etc. The way I could make it work for all types is without the type declaration.
If you have another idea how to do that let me know.
The ideia is the value to be of Any type.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format was resolved already.

}
2 changes: 2 additions & 0 deletions src/Experiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Experiment {
public bool $audienceStrict;
public array $applications;
public array $variants;
public ?array $customFieldValues;

public function __construct(object $data) {
if (!empty($data->audience)) {
Expand All @@ -34,6 +35,7 @@ public function __construct(object $data) {
$this->audience = null;
}

$this->customFieldValues = null;

$data = get_object_varsAlias($data);
foreach ($data as $field => $value) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Context/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,38 @@ public function testGetVariableKeys(): void {
self::assertEquals($this->variableExperiments, $context->getVariableKeys());
}

public function testGetFieldValueKeys(): void{
$context = $this->createReadyContext();
self::assertEquals(['country', 'languages', 'overrides'], $context->getCustomFieldKeys());
}

public function testGetFieldValueValues(): void {
$context = $this->createReadyContext();
self::assertEquals(null, $context->getCustomFieldValue('not_found', 'not_found'));
self::assertEquals(null, $context->getCustomFieldValue('exp_test_ab', 'not_found'));
self::assertEquals("US,PT,ES,DE,FR", $context->getCustomFieldValue('exp_test_ab', 'country'));
self::assertEquals((object)array('123' => 1, '456' => 0), $context->getCustomFieldValue('exp_test_ab', 'overrides'));
self::assertEquals("json", $context->getCustomFieldValueType('exp_test_ab', 'overrides'));

self::assertEquals(null, $context->getCustomFieldValue('exp_test_ab', 'languages'));
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_ab', 'languages'));

self::assertEquals(null, $context->getCustomFieldValue('exp_test_abc', 'overrides'));
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_abc', 'overrides'));

self::assertEquals("en-US,en-GB,pt-PT,pt-BR,es-ES,es-MX", $context->getCustomFieldValue('exp_test_abc', 'languages'));
self::assertEquals("string", $context->getCustomFieldValueType('exp_test_abc', 'languages'));

self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'country'));
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'country'));

self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'overrides'));
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'overrides'));

self::assertEquals(null, $context->getCustomFieldValue('exp_test_no_custom_fields', 'languages'));
self::assertEquals(null, $context->getCustomFieldValueType('exp_test_no_custom_fields', 'languages'));
}

public function testPeekTreatmentReturnsOverrideVariant(): void {
$context = $this->createReadyContext();

Expand Down
33 changes: 29 additions & 4 deletions tests/Fixtures/json/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@
],
"variants":[
{
"name":"A",
"name":"J",
"config":null
},
{
"name":"B",
"config":"{\"banner.border\":1,\"banner.size\":\"large\"}"
}
],
"audience": null
"audience": null,
"customFieldValues": [
{
"name": "country",
"value": "US,PT,ES,DE,FR",
"type": "string"
},
{
"name": "overrides",
"value": "{\"123\":1,\"456\":0}",
"type": "json"
}
]
},
{
"id":2,
Expand Down Expand Up @@ -73,7 +85,19 @@
"config":"{\"button.color\":\"red\"}"
}
],
"audience": ""
"audience": "",
"customFieldValues": [
{
"name": "country",
"value": "US,PT,ES,DE,FR",
"type": "string"
},
{
"name": "languages",
"value": "en-US,en-GB,pt-PT,pt-BR,es-ES,es-MX",
"type": "string"
}
]
},
{
"id":3,
Expand Down Expand Up @@ -113,7 +137,8 @@
"config":"{\"card.width\":\"75%\"}"
}
],
"audience": "{}"
"audience": "{}",
"customFieldValues": null
},
{
"id":4,
Expand Down