-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
849 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Bugsnag; | ||
|
||
interface FeatureDataStore | ||
{ | ||
/** | ||
* Add a single feature flag. | ||
* | ||
* @param string $name | ||
* @param string|null $variant | ||
* | ||
* @return void | ||
*/ | ||
public function addFeatureFlag($name, $variant = null); | ||
|
||
/** | ||
* Add multiple feature flags. | ||
* | ||
* The new flags will be merged with any existing feature flags, with the | ||
* newer variant values taking precedence | ||
* | ||
* @param array $featureFlags | ||
* @phpstan-param list<FeatureFlag> $featureFlags | ||
* | ||
* @return void | ||
*/ | ||
public function addFeatureFlags(array $featureFlags); | ||
|
||
/** | ||
* Remove a single feature flag by name. | ||
* | ||
* @param string $name | ||
* | ||
* @return void | ||
*/ | ||
public function clearFeatureFlag($name); | ||
|
||
/** | ||
* Remove all feature flags. | ||
* | ||
* @return void | ||
*/ | ||
public function clearFeatureFlags(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Bugsnag; | ||
|
||
final class FeatureFlag | ||
{ | ||
/** | ||
* A name that identifies this feature flag. | ||
* | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* An optional variant for this feature flag. | ||
* | ||
* @var string|null | ||
*/ | ||
private $variant; | ||
|
||
/** | ||
* @param string $name a name that identifies this feature flag | ||
* @param string|null $variant an optional variant for this feature flag. | ||
*/ | ||
public function __construct($name, $variant = null) | ||
{ | ||
$this->name = $name; | ||
|
||
// ensure the variant can only be null or a string as the API only | ||
// accepts strings (null values will be omitted from the payload) | ||
if ($variant !== null && !is_string($variant)) { | ||
$json = json_encode($variant); | ||
|
||
// if JSON encoding fails, omit the variant | ||
$variant = $json === false ? null : $json; | ||
} | ||
|
||
$this->variant = $variant; | ||
} | ||
|
||
/** | ||
* Get the feature flag's name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Get the feature flag's variant. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getVariant() | ||
{ | ||
return $this->variant; | ||
} | ||
|
||
/** | ||
* Convert this feature flag into the format used by the Bugsnag Event API. | ||
* | ||
* This has two forms, either with a variant: | ||
* { "featureFlag": "name", "variant": "variant" } | ||
* | ||
* or if the feature flag has no variant: | ||
* { "featureFlag": "no variant" } | ||
* | ||
* @return array[] | ||
* @phpstan-return array{featureFlag: string, variant?: string} | ||
*/ | ||
public function toArray() | ||
{ | ||
if (is_string($this->variant)) { | ||
return ['featureFlag' => $this->name, 'variant' => $this->variant]; | ||
} | ||
|
||
return ['featureFlag' => $this->name]; | ||
} | ||
} |
Oops, something went wrong.