/** * Cleans and re-formats data. * * @param array $object * * @return array */ public static function cleanData(array $object) { foreach ($object as $key => $value) { if ($key === 'count') { unset($object[$key]); continue; } if (is_numeric($key)) { unset($object[$key]); continue; } else { if (is_array($value) && isset($value[0]) && !DataFormatter::isPrintable($value[0])) { unset($object[$key]); continue; } else { if (is_string($value) && !DataFormatter::isPrintable($value)) { unset($object[$key]); continue; } } } if (is_array($value)) { if (ArrayUtils::get($value, 'count') === 1) { $object[$key] = $value[0]; } else { if (ArrayUtils::get($value, 'count') > 1) { unset($object[$key]['count']); } } } } return $object; }
/** * @param \DreamFactory\Core\Contracts\ServiceResponseInterface $response * @param null $accepts * @param null $asFile * @param string $resource * * @return \Symfony\Component\HttpFoundation\Response * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \DreamFactory\Core\Exceptions\NotImplementedException */ public static function sendResponse(ServiceResponseInterface $response, $accepts = null, $asFile = null, $resource = 'resource') { if (empty($accepts)) { $accepts = static::getAcceptedTypes(); } if (empty($asFile)) { $asFile = \Request::input('file'); if (true === filter_var($asFile, FILTER_VALIDATE_BOOLEAN)) { $asFile = $resource . '.json'; } if (is_string($asFile) && strpos($asFile, '.') !== false) { $format = strtolower(pathinfo($asFile, PATHINFO_EXTENSION)); if (!empty($format)) { if ($format === 'csv') { $accepts = ['text/csv']; } else { if ($format === 'xml') { $accepts = ['application/xml']; } else { if ($format === 'json') { $accepts = ['application/json']; } } } } } else { $asFile = null; } } // If no accepts header supplied or a blank is supplied for // accept header (clients like bench-rest) then use default response type from config. if (empty($accepts) || isset($accepts[0]) && empty($accepts[0])) { $accepts[] = config('df.default_response_type'); } $content = $response->getContent(); $format = $response->getContentFormat(); if (empty($content) && is_null($format)) { // No content and type specified. (File stream already handled by service) return null; } $status = $response->getStatusCode(); // In case the status code is not a valid HTTP Status code if (!in_array($status, HttpStatusCodes::getDefinedConstants())) { // Do necessary translation here. Default is Internal server error. $status = HttpStatusCodeInterface::HTTP_INTERNAL_SERVER_ERROR; } if ($content instanceof \Exception) { $status = $content instanceof RestException ? $content->getStatusCode() : ServiceResponseInterface::HTTP_INTERNAL_SERVER_ERROR; $content = self::makeExceptionContent($content); $format = DataFormats::PHP_ARRAY; } // check if the current content type is acceptable for return $contentType = $response->getContentType(); if (empty($contentType)) { $contentType = DataFormats::toMimeType($format, null); } // see if we match an accepts type, if so, go with it. $accepts = ArrayUtils::clean($accepts); if (!empty($contentType) && static::acceptedContentType($accepts, $contentType)) { return DfResponse::create($content, $status, ["Content-Type" => $contentType]); } // we don't have an acceptable content type, see if we can convert the content. $acceptsAny = false; $reformatted = false; foreach ($accepts as $acceptType) { $acceptFormat = DataFormats::fromMimeType($acceptType, null); $mimeType = false !== strpos($acceptType, ';') ? trim(strstr($acceptType, ';', true)) : $acceptType; if (is_null($acceptFormat)) { if ('*/*' === $mimeType) { $acceptsAny = true; } continue; } else { $contentType = $mimeType; } $reformatted = DataFormatter::reformatData($content, $format, $acceptFormat); } $responseHeaders = ["Content-Type" => $contentType]; if (!empty($asFile)) { $responseHeaders['Content-Disposition'] = 'attachment; filename="' . $asFile . '";'; } if ($acceptsAny) { $contentType = empty($contentType) ? DataFormats::toMimeType($format, config('df.default_response_type')) : $contentType; $responseHeaders['Content-Type'] = $contentType; return DfResponse::create($content, $status, $responseHeaders); } else { if (false !== $reformatted) { return DfResponse::create($reformatted, $status, $responseHeaders); } } throw new BadRequestException('Content in response can not be resolved to acceptable content type.'); }
/** * @return bool * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \Exception */ private function insertData() { $data = $this->zip->getFromName('data.json'); $this->zip->deleteName('data.json'); if (false !== $data) { $data = DataFormatter::jsonToArray($data); $services = ArrayUtils::get($data, 'service'); if (!empty($services)) { foreach ($services as $service) { $serviceName = ArrayUtils::get($service, 'name'); $tables = ArrayUtils::get($service, 'table'); foreach ($tables as $table) { $tableName = ArrayUtils::get($table, 'name'); $records = ArrayUtils::get($table, 'record'); $resource = $this->resourceWrapped ? [$this->resourceWrapper => $records] : [$records]; try { ServiceHandler::handleRequest(Verbs::POST, $serviceName, '_table/' . $tableName, [], $resource); } catch (\Exception $e) { if (in_array($e->getCode(), [404, 500])) { throw $e; } else { \Log::alert('Failed to insert data. ' . $e->getMessage()); } } } } } else { throw new BadRequestException("Could not create the database tables for this application.\nDatabase service or data not found."); } return true; } return false; }
/** * Validates data based on $this->rules. * * @param array $data * @param bool|true $throwException * * @return bool * @throws \DreamFactory\Core\Exceptions\BadRequestException */ public function validate(array $data = [], $throwException = true) { if (empty($data)) { $data = $this->attributes; } if (empty($this->rules) || empty($data)) { return true; } else { $validator = \Validator::make($data, $this->rules); if ($validator->fails()) { $this->errors = $validator->errors()->getMessages(); if ($throwException) { $errorString = DataFormatter::validationErrorsToString($this->errors); throw new BadRequestException('Invalid data supplied.' . $errorString, null, null, $this->errors); } else { return false; } } else { return true; } } }
/** * Returns XML payload data. * * @param null $key * @param null $default * * @return array|mixed|null * @throws \DreamFactory\Core\Exceptions\BadRequestException */ protected function xml($key = null, $default = null) { if (!empty($this->contentAsArray)) { if (null === $key) { return $this->contentAsArray; } else { return ArrayUtils::get($this->contentAsArray, $key, $default); } } $content = $this->getContent(); $data = DataFormatter::xmlToArray($content); $rootTag = config('df.xml_request_root'); if (!empty($content) && (empty($data) || empty(ArrayUtils::get($data, $rootTag)))) { throw new BadRequestException('Invalid XML payload supplied.'); } $payload = $data[$rootTag]; // Store the content so that formatting is only done once. $this->contentAsArray = empty($payload) ? [] : $payload; $this->content = $content; if (null === $key) { if (empty($payload)) { return []; } else { return $payload; } } else { if (empty($payload)) { return $default; } else { return ArrayUtils::get($payload, $key, $default); } } }