/** * Called during an API request for data generation. This does the same thing as initUIGenerator: it pulls the * core values out and stores them on the current DataGenerator object. The only difference is the source: this * pulls them from the JSON content POSTed, rather than relies on the browser form POST used by the UI. * @param $json */ private function initAPIGenerator($json) { $this->exportTarget = "newTab"; // should be a constant $this->batchSize = $json->numRows; $this->batchNum = 1; $this->numResults = $json->numRows; $this->applyRowsGeneratedLimit(); $this->countries = property_exists($json, "countries") ? $json->countries : array(); $this->apiData = $json; if (isset($postData["configurationID"])) { $this->configurationID = $postData["configurationID"]; } // make a note of whether this batch is the first / last. This is useful information for the // Export Types to know whether to output special content at the top or bottom $this->isFirstBatch = true; $this->isLastBatch = true; $this->currentBatchFirstRow = 1; $this->currentBatchLastRow = $this->numResults; // figure out what we're going to need to generate $this->createDataSetTemplateAPI($json); $this->exportType = ExportTypePluginHelper::getExportTypeByFolder($json->export->type); $this->isCompressionRequired = false; }
/** * Examines the contents of the `export` property, which is an object of the following form: * { type: "N", settings: {} }. "N" is the folder name of the desired Export Type (see * /plugins/axportTypes); `settings` is an object containing whatever settings have been specified by that * Export Type's schema.json file. * @param $json * @return array */ private function validateExportTypeSettings($json) { // first, find the Export Type and check it's valid if (!property_exists($json->export, "type")) { return array("error" => ErrorCodes::API_INVALID_JSON_CONTENT, "error_details" => "Missing `type` property in the `export` object"); } $exportTypeFolders = ExportTypePluginHelper::getExportTypeFolders(); $exportType = $json->export->type; if (!in_array($exportType, $exportTypeFolders)) { return array("error" => ErrorCodes::API_UNKNOWN_EXPORT_TYPE, "error_details" => "invalid `type` attribute of the `export` object: `{$exportType}`"); } $exportType = ExportTypePluginHelper::getExportTypeByFolder($exportType); $schema = json_decode($exportType->getSchema()); // only validate if there's actually a schema for this Export Type if ($schema !== null) { $json = property_exists($json->export, "settings") ? $json->export->settings : json_decode("{}"); // verify the settings for this data type $result = Jsv4::validate($json, $schema); if (!$result->valid) { return array("error" => ErrorCodes::API_INVALID_EXPORT_TYPE_JSON, "error_details" => "Invalid Export Type JSON `settings` content passed", "path" => $result->errors[0]->dataPath, "validation_error" => $result->errors[0]->message); } } }