-
Notifications
You must be signed in to change notification settings - Fork 78
Simplify metric explorer getFormat #2189
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,41 +227,6 @@ public static function writeHeader( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the output format from a request. | ||
| * | ||
| * @param array $request The HTTP request data. | ||
| * @param string $default The default output format | ||
| * (defaults to "jsonstore"). | ||
| * @param array $format_subset The allowed subset of formats. If | ||
| * the format specified by the request is not in this array, | ||
| * the default format will be used. | ||
| */ | ||
| public static function getFormat( | ||
| array $request, | ||
| $default = 'jsonstore', | ||
| array $formats_subset = array() | ||
| ) { | ||
| $format = $default; | ||
|
|
||
| if (isset($request['format'])) { | ||
| $f = strtolower($request['format']); | ||
|
|
||
| if ( | ||
| isset(static::$supported_formats[$f]) | ||
| && ( | ||
| count($formats_subset) == 0 | ||
| || | ||
| (count($formats_subset) > 0 && array_search($f, $formats_subset) !== false) | ||
| ) | ||
| ) { | ||
| $format = $f; | ||
| } | ||
| } | ||
|
|
||
| return $format; | ||
| } | ||
|
|
||
| /** | ||
| * Export data. | ||
| * | ||
|
|
@@ -620,4 +585,28 @@ private static function formatElement($name) | |
|
|
||
| return $name; | ||
| } | ||
|
|
||
| /** Validates that the format requested by the user is located in the set of formats that are supported and either | ||
| * all formats are allowed ( signified by there being no $allowedFormats ) or the requested format was found in the | ||
| * set of allowed formats. If valid the requested format is returned. If no requested format is provided then the | ||
| * default value will be returned. | ||
| * | ||
| * @param string $requestedFormat | ||
| * @param string $default | ||
| * @param array $allowedFormats | ||
| * @return string | ||
| */ | ||
| public static function validateFormat(string $requestedFormat, string $default, array $allowedFormats): string | ||
| { | ||
| if (!isset($requestedFormat)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for isset() to return false here? |
||
| return $default; | ||
| } | ||
| $requestedFormat = strtolower($requestedFormat); | ||
| $formatSupported = isset(self::$supported_formats[$requestedFormat]); | ||
| $noFormatSubset = count($allowedFormats) === 0; | ||
| $requestedFormatInSubset = in_array($requestedFormat, $allowedFormats); | ||
|
|
||
|
|
||
| return $formatSupported && ($noFormatSubset || $requestedFormatInSubset) ? $requestedFormat : $default; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,10 @@ | |
|
|
||
| $start = microtime(true); | ||
| try { | ||
| if (isset($_REQUEST['config'])) { | ||
| $config = json_decode($_REQUEST['config'], true); | ||
| $_REQUEST = array_merge($config, $_REQUEST); | ||
| } | ||
|
|
||
| $format = \DataWarehouse\ExportBuilder::getFormat( | ||
| $_REQUEST, | ||
| 'jsonstore', | ||
| array( | ||
| 'jsonstore' | ||
| ) | ||
| ); | ||
| $requestedFormat = $_REQUEST['format'] ?? null; | ||
|
|
||
| $format = \DataWarehouse\ExportBuilder::validateFormat($requestedFormat, 'jsonstore', ['jsonstore']); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The definition for the new validateFormat function requires a |
||
|
|
||
| $user = \xd_security\detectUser( | ||
| array(XDUser::INTERNAL_USER, XDUser::PUBLIC_USER) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you know that the json_decode is not necessary any more?