/** * @param mixed $data * @return string */ public static function encode($data) { $res = @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // encode не прошел. Если ошибка говорит что неподдерживаемый тип, то фильтруем данные и снова encode if (!$res && json_last_error() == JSON_ERROR_UNSUPPORTED_TYPE) { JsonHelper::filterData($data); $res = @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } return $res; }
/** * @return array * @throws AppException * @throws Exception */ private function getMethods() { $saveVersion = ParamsHelper::getVersion(); Yii::$app->params['version'] = $this->getVersion(); $dir = $this->getMethodsDir(); $files = $this->dirToArray($dir); $result = []; foreach ($files as $methodDir => $methodFiles) { if (is_array($methodFiles)) { $result[$methodDir]['meta'] = null; $result[$methodDir]['actions'] = []; foreach ($methodFiles as $methodFile) { if (pathinfo($methodFile)['extension'] == 'json') { $file = $dir . '/' . $methodDir . '/' . $methodFile; $filename = pathinfo($methodFile)['filename']; $md = "{$dir}/{$methodDir}/{$filename}.md'"; $content = JsonHelper::decode(file_get_contents($file)); if (isset($content['request']['properties'])) { foreach ($content['request']['properties'] as $key => $value) { $content['request']['properties'][$key]['id'] = $key; } } if ($content) { if (in_array(basename($md), $methodFiles)) { $content['meta']['md'] = file_get_contents($md); } if ($methodFile == 'meta.json') { $result[$methodDir]['meta']['id'] = $methodDir; $result[$methodDir]['meta'] = array_merge($result[$methodDir]['meta'], $content); } else { $result[$methodDir]['actions'][$filename] = $content; } } else { throw new Exception('Invalid json in ' . $file); } if ($filename !== 'meta') { if (isset($content['request']['links'])) { throw new Exception("Links should not be set for controller {$methodDir} and action {$filename}"); } $oldMethod = isset($content['request']['method']) ? $content['request']['method'] : null; list($path, $method) = $this->getLinkParams($methodDir, $filename, null); if ($method && $oldMethod) { throw new Exception("Request method {$oldMethod} should not be set for controller {$methodDir} and action {$filename}"); } if (!$method) { $method = $oldMethod ?: 'POST'; } $result[$methodDir]['actions'][$filename]['request']['method'] = $method; $result[$methodDir]['actions'][$filename]['request']['links'] = [['href' => $path]]; // $relations = $this->getRelations($methodDir, $filename, $content); // $relationsJson = str_replace('[]', '{}', JsonHelper::encode($relations)); // $result[$methodDir]['actions'][$filename]['request']['properties']['relations'] = [ // 'default' => $relationsJson, // 'id' => 'relations', // 'type' => 'string', // ]; } } } } } Yii::$app->params['version'] = $saveVersion; return $result; }
/** * Get an error response for the API. * * @param string $code The error code. * @param string $msg The error message. * @return array The error response. * @static */ public static function getErrorResponse($code, $msg) { return \App\Helpers\JsonHelper::getErrorResponse($code, $msg); }