Skip to content
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

Add support for Remote Config personalization values #733

Merged
merged 1 commit into from
Sep 15, 2022
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
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

## [Unreleased]

### Added

* Added support for Remote Config Personalization
([#731](https://github.com/kreait/firebase-php/pull/731)/[#733](https://github.com/kreait/firebase-php/pull/733))
* Note: Personalization (currently) can not be added programmatically. The values can only be read and removed from a
Remote Config Template. To add Personalization, use the Firebase Web Console.
* Added `Kreait\Firebase\RemoteConfig\Template::withRemovedParameter(string $name)` to remove an existing parameter
from a Remote Config Template
* Added method `Kreait\Firebase\RemoteConfig\Template::withRemovedParameterGroup(string $name)` to remove an existing
parameter group from a Remote Config Template

### Changed

* Added `Kreait\Firebase\RemoteConfig\DefaultValue::useInAppDefault()` and deprecated
`\Kreait\Firebase\RemoteConfig\DefaultValue::none()`

### Deprecated

* `Kreait\Firebase\RemoteConfig\DefaultValue::IN_APP_DEFAULT_VALUE`
* `Kreait\Firebase\RemoteConfig\DefaultValue::none()`
* `Kreait\Firebase\RemoteConfig\DefaultValue::value()`

## [6.8.0] - 2022-08-20

### Added
Expand Down
15 changes: 15 additions & 0 deletions docs/remote-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ Parameter Groups

$template = $template->withParameterGroup($parameterGroup);

*******************************
Removing Remote Config Elements
*******************************

You can remove elements from a Remote Config template with the following methods:

.. code-block:: php

$template = Template::new()
->withParameterGroup(ParameterGroup::named('group'))
->withParameter(Parameter::named('parameter'))

$template = $template
->withRemovedParameter('parameter')
->withRemovedParameterGroup('group');

**********
Validation
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