-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
…g + improve config json deserialization error reporting
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ConfigCat\ConfigJson; | ||
|
||
use ValueError; | ||
|
||
/** | ||
* 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'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function fixup(array &$config) | ||
Check failure on line 32 in src/ConfigJson/Config.php
|
||
{ | ||
$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; | ||
} | ||
} | ||
} | ||
|
||
public static function deserialize(string $json): array | ||
Check failure on line 46 in src/ConfigJson/Config.php
|
||
{ | ||
$config = json_decode($json, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new ValueError('JSON error: '.json_last_error_msg()); | ||
} | ||
|
||
if (!is_array($config)) { | ||
throw new ValueError('Invalid config JSON content: '.$json); | ||
} | ||
|
||
self::fixup($config); | ||
|
||
return $config; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ConfigCat\ConfigJson; | ||
|
||
use ValueError; | ||
|
||
/** | ||
* Represents the JSON keys of a percentage option. | ||
*/ | ||
abstract class PercentageOption extends SettingValueContainer | ||
{ | ||
public const PERCENTAGE = 'p'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function ensureList(mixed $percentageOptions): array | ||
Check failure on line 19 in src/ConfigJson/PercentageOption.php
|
||
{ | ||
if (!is_array($percentageOptions) || !array_is_list($percentageOptions)) { | ||
throw new ValueError('Percentage option list is invalid.'); | ||
} | ||
|
||
return $percentageOptions; | ||
} | ||
} |