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
7 changes: 4 additions & 3 deletions src/EvaluationContextConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ private function getTargetingKey(?string $targetingKey, $key): string
$this->logger->warning("A non-string 'key' attribute was provided.");
}

if ($key !== null && is_string($key)) {
// Targeting key takes precedence over key, because targeting key is in the spec.
$targetingKey = $targetingKey ?: $key;
// Targeting key takes precedence over key, because targeting key is in the spec.
// Compare against "" explicitly, because "0" is falsy in PHP but is a valid key.
if (($targetingKey === null || $targetingKey === "") && is_string($key)) {
$targetingKey = $key;
}

if ($targetingKey === null || $targetingKey === "") {
Expand Down
28 changes: 28 additions & 0 deletions tests/EvaluationContextConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,34 @@ public function testTargetingKeyTakesPrecedenceOverAttributeKey(): void
$this->assertEquals("should-use", $ldContext->getKey());
}

public function testZeroTargetingKeyTakesPrecedenceOverAttributeKey(): void
{
$attributes = new Attributes(["key" => "do-not-use"]);
$context = new EvaluationContext("0", $attributes);

$ldContext = $this->contextConverter->toLdContext($context);

$this->assertTrue($ldContext->isValid());
$this->assertEquals("0", $ldContext->getKey());
}

public function testZeroTargetingKeyTakesPrecedenceOverAttributeKeyInMultiContext(): void
{
$attributes = [
'kind' => 'multi',
'user' => ['targetingKey' => '0', 'key' => 'do-not-use'],
];
$context = new EvaluationContext(null, new Attributes($attributes));

$ldContext = $this->contextConverter->toLdContext($context);

$this->assertTrue($ldContext->isValid());

/** @var \LaunchDarkly\LDContext */
$userContext = $ldContext->getIndividualContext("user");
$this->assertEquals("0", $userContext->getKey());
}

public function testCreateContextWithKeyAndKind(): void
{
$attributes = new Attributes(["kind" => "org"]);
Expand Down
Loading