-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update config JSON model to v6 + fix inconsistent error 1000 reportin…
…g + improve config json deserialization error reporting
- Loading branch information
Showing
36 changed files
with
916 additions
and
264 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ConfigCat\ConfigJson; | ||
|
||
/** | ||
* Represents the JSON keys of a condition. | ||
*/ | ||
abstract class Condition | ||
{ | ||
public const USER_CONDITION = 'u'; | ||
public const PREREQUISITE_FLAG_CONDITION = 'p'; | ||
public const SEGMENT_CONDITION = 's'; | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ConfigCat\ConfigJson; | ||
|
||
use UnexpectedValueException; | ||
|
||
/** | ||
* Represents the root JSON keys of a ConfigCat config_v6.json file. | ||
*/ | ||
abstract class Config | ||
{ | ||
/** | ||
* Preferences of the config.json, mostly for controlling the redirection behaviour of the SDK. | ||
*/ | ||
public const PREFERENCES = 'p'; | ||
|
||
/** | ||
* Segment definitions for re-using segment rules in targeting rules. | ||
*/ | ||
public const SEGMENTS = 's'; | ||
|
||
/** | ||
* Setting definitions. | ||
*/ | ||
public const SETTINGS = 'f'; | ||
|
||
/** | ||
* @param array<string, mixed> $config | ||
* @internal | ||
*/ | ||
public static function fixup(array &$config): void | ||
{ | ||
$settings = &$config[self::SETTINGS] ?? []; | ||
if (is_array($settings) && !empty($settings)) { | ||
$salt = $config[self::PREFERENCES][Preferences::SALT] ?? null; | ||
$segments = $config[self::SEGMENTS] ?? []; | ||
|
||
foreach ($settings as &$setting) { | ||
$setting[Setting::CONFIG_JSON_SALT] = $salt; | ||
$setting[Setting::CONFIG_SEGMENTS] = $segments; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
* @throws UnexpectedValueException | ||
*/ | ||
public static function deserialize(string $json): array | ||
{ | ||
$config = json_decode($json, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new UnexpectedValueException('JSON error: '.json_last_error_msg()); | ||
} | ||
|
||
if (!is_array($config)) { | ||
throw new UnexpectedValueException('Invalid config JSON content: '.$json); | ||
} | ||
|
||
self::fixup($config); | ||
|
||
return $config; | ||
} | ||
} |
Oops, something went wrong.