Skip to content

Commit

Permalink
Add support for Remote Config personalization values
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Sep 14, 2022
1 parent d3402c9 commit ea87b0d
Show file tree
Hide file tree
Showing 18 changed files with 622 additions and 133 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Added

* Added support for Remote Config personalization values
([#731](https://github.com/kreait/firebase-php/pull/731)/[#733](https://github.com/kreait/firebase-php/pull/733))

## [6.8.0] - 2022-08-20

### Added
Expand Down
8 changes: 5 additions & 3 deletions src/Firebase/Contract/RemoteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
* @see https://firebase.google.com/docs/remote-config/use-config-rest
* @see https://firebase.google.com/docs/remote-config/rest-reference
*
* @phpstan-import-type RemoteConfigTemplateShape from Template
*/
interface RemoteConfig
{
Expand All @@ -29,19 +31,19 @@ public function get(): Template;
/**
* Validates the given template without publishing it.
*
* @param Template|array<string, mixed> $template
* @param Template|RemoteConfigTemplateShape $template
*
* @throws ValidationFailed if the validation failed
* @throws RemoteConfigException
*/
public function validate($template): void;

/**
* @param Template|array<string, mixed> $template
* @param Template|RemoteConfigTemplateShape $template
*
* @throws RemoteConfigException
*
* @return string The etag value of the published template that can be compared to in later calls
* @return non-empty-string The etag value of the published template that can be compared to in later calls
*/
public function publish($template): string;

Expand Down
13 changes: 11 additions & 2 deletions src/Firebase/RemoteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use Traversable;

use function array_shift;
use function is_string;

/**
* @internal
*
* @phpstan-import-type RemoteConfigTemplateShape from Template
*/
final class RemoteConfig implements Contract\RemoteConfig
{
Expand All @@ -44,7 +47,13 @@ public function publish($template): string
->publishTemplate($this->ensureTemplate($template))
->getHeader('ETag');

return array_shift($etag) ?: '';
$etag = array_shift($etag);

if (is_string($etag) && $etag !== '') {
return $etag;
}

return '*';
}

public function getVersion($versionNumber): Version
Expand Down Expand Up @@ -93,7 +102,7 @@ public function listVersions($query = null): Traversable
}

/**
* @param Template|array<string, mixed> $value
* @param Template|RemoteConfigTemplateShape $value
*/
private function ensureTemplate($value): Template
{
Expand Down
63 changes: 50 additions & 13 deletions src/Firebase/RemoteConfig/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@

use JsonSerializable;

use function array_filter;

/**
* @phpstan-type RemoteConfigConditionShape array{
* name: non-empty-string,
* expression: non-empty-string,
* tagColor?: ?non-empty-string
* }
*/
class Condition implements JsonSerializable
{
/**
* @var non-empty-string
*/
private string $name;

/**
* @var non-empty-string
*/
private string $expression;
private ?TagColor $tagColor;

/**
* @param non-empty-string $name
* @param non-empty-string $expression
*/
private function __construct(string $name, string $expression, ?TagColor $tagColor = null)
{
$this->name = $name;
Expand All @@ -22,11 +38,7 @@ private function __construct(string $name, string $expression, ?TagColor $tagCol
}

/**
* @param array{
* name: string,
* expression: string,
* tagColor?: ?string
* } $data
* @param RemoteConfigConditionShape $data
*/
public static function fromArray(array $data): self
{
Expand All @@ -37,21 +49,33 @@ public static function fromArray(array $data): self
);
}

/**
* @param non-empty-string $name
*/
public static function named(string $name): self
{
return new self($name, 'false', null);
}

/**
* @return non-empty-string
*/
public function name(): string
{
return $this->name;
}

/**
* @return non-empty-string
*/
public function expression(): string
{
return $this->expression;
}

/**
* @param non-empty-string $expression
*/
public function withExpression(string $expression): self
{
$condition = clone $this;
Expand All @@ -61,7 +85,7 @@ public function withExpression(string $expression): self
}

/**
* @param TagColor|string $tagColor
* @param TagColor|non-empty-string $tagColor
*/
public function withTagColor($tagColor): self
{
Expand All @@ -74,14 +98,27 @@ public function withTagColor($tagColor): self
}

/**
* @return array<string, string>
* @return RemoteConfigConditionShape
*/
public function jsonSerialize(): array
public function toArray(): array
{
return array_filter([
$array = [
'name' => $this->name,
'expression' => $this->expression,
'tagColor' => $this->tagColor !== null ? $this->tagColor->value() : null,
], static fn ($value) => $value !== null);
];

if ($this->tagColor !== null) {
$array['tagColor'] = $this->tagColor->value();
}

return $array;
}

/**
* @return RemoteConfigConditionShape
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
61 changes: 48 additions & 13 deletions src/Firebase/RemoteConfig/ConditionalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,88 @@

use JsonSerializable;

use function is_string;

/**
* @phpstan-import-type RemoteConfigPersonalizationValueShape from PersonalizationValue
* @phpstan-import-type RemoteConfigExplicitValueShape from ExplicitValue
* @phpstan-import-type RemoteConfigInAppDefaultValueShape from DefaultValue
*/
class ConditionalValue implements JsonSerializable
{
/**
* @var non-empty-string
*/
private string $conditionName;
private string $value;

/**
* @var RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape|string
*/
private $data;

/**
* @internal
*
* @param non-empty-string $conditionName
* @param RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape|string $data
*/
public function __construct(string $conditionName, string $value)
public function __construct(string $conditionName, $data)
{
$this->conditionName = $conditionName;
$this->value = $value;
$this->data = $data;
}

/**
* @return non-empty-string
*/
public function conditionName(): string
{
return $this->conditionName;
}

/**
* @param string|Condition $condition
* @param non-empty-string|Condition $condition
*/
public static function basedOn($condition): self
{
$name = $condition instanceof Condition ? $condition->name() : $condition;

return new self($name, '');
return new self($name, ['value' => '']);
}

public function value(): string
/**
* @return RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape|string
*/
public function value()
{
return $this->data;
}

/**
* @param RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape|string $value
*/
public function withValue($value): self
{
return $this->value;
return new self($this->conditionName, $value);
}

public function withValue(string $value): self
/**
* @return RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape
*/
public function toArray(): array
{
$conditionalValue = clone $this;
$conditionalValue->value = $value;
if (is_string($this->data)) {
return ExplicitValue::fromString($this->data)->toArray();
}

return $conditionalValue;
return $this->data;
}

/**
* @return array<string, string>
* @return RemoteConfigExplicitValueShape|RemoteConfigInAppDefaultValueShape|RemoteConfigPersonalizationValueShape
*/
public function jsonSerialize(): array
{
return ['value' => $this->value];
return $this->toArray();
}
}
Loading

0 comments on commit ea87b0d

Please sign in to comment.