public function handle($request, $serviceId) { $encoder = new JsonEncoder(); try { $request = $encoder->decode($request, JsonEncoder::FORMAT); } catch (UnexpectedValueException $exception) { return new ErrorResponse(ErrorResponse::ERROR_CODE_PARSE_ERROR, 'Invalid JSON'); } $request = $this->resolveOptions($request); if (!$this->isAllowed($serviceId, $request['method'])) { return new ErrorResponse(ErrorResponse::ERROR_CODE_METHOD_NOT_FOUND, sprintf('%s does not exist', $request['method'])); } $service = $this->container->get($serviceId); $result = call_user_func_array(array($service, $request['method']), $request['params']); return new SuccessResponse($request['id'], $result); }
/** * Encodes PHP data to a JSON string. * * {@inheritdoc} */ public function encode($data, $format, array $context = array()) { $context = $this->resolveContext($context); $encodedJson = json_encode($data, $context['json_encode_options']); if (JSON_ERROR_NONE !== ($this->lastError = json_last_error())) { throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); } return $encodedJson; }
/** * Dump * * @return string */ public function dump() { $gherkin = $this->feature->getGherkin(); $description = array_pad(preg_split('!\\n!', $gherkin->getDescription(), 4), 4, ''); $result = array('title' => $gherkin->getTitle(), 'notes' => $description[3], 'inorder' => $description[0], 'as' => $description[1], 'should' => $description[2], 'scenarios' => array(), 'background' => array()); // // Scenarios foreach ($gherkin->getScenarios() as $scenario) { array_push($result['scenarios'], $this->_dumpSteppable($scenario)); } // // Background if ($gherkin->getBackground()) { $result['background'] = $this->_dumpSteppable($gherkin->getBackground()); } $jsonEncoder = new JsonEncoder(); return $jsonEncoder->encode($result, 'json'); }
/** * Decodes data. * * @param string $data The encoded JSON string to decode * @param string $format Must be set to JsonEncoder::FORMAT * @param array $context An optional set of options for the JSON decoder; see below * * The $context array is a simple key=>value array, with the following supported keys: * * json_decode_associative: boolean * If true, returns the object as associative array. * If false, returns the object as nested stdClass * If not specified, this method will use the default set in JsonDecode::__construct * * json_decode_recursion_depth: integer * Specifies the maximum recursion depth * If not specified, this method will use the default set in JsonDecode::__construct * * json_decode_options: integer * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher * * @return mixed * * @throws UnexpectedValueException * * @see http://php.net/json_decode json_decode */ public function decode($data, $format, array $context = array()) { $context = $this->resolveContext($context); $associative = $context['json_decode_associative']; $recursionDepth = $context['json_decode_recursion_depth']; $options = $context['json_decode_options']; $decodedData = json_decode($data, $associative, $recursionDepth, $options); if (JSON_ERROR_NONE !== ($this->lastError = json_last_error())) { throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); } return $decodedData; }
/** * @param callable[] $decoders */ public static function decode(Request $request, array $decoders = null) { if (null === $decoders) { $decoders = ['json' => function ($content) { $encoder = new JsonEncoder(); return $encoder->decode($content, 'json'); }, 'xml' => function ($content) { $encoder = new XmlEncoder(); return $encoder->decode($content, 'xml'); }]; } if (!self::isDecodeable($request)) { return; } $contentType = $request->headers->get('Content-Type'); $format = null === $contentType ? $request->getRequestFormat() : $request->getFormat($contentType); if (!$format || !isset($decoders[$format])) { return; } if (!is_callable($decoders[$format])) { return; } $content = $request->getContent(); if (!$content) { return; } try { $data = call_user_func($decoders[$format], $content); } catch (\Exception $e) { throw new BadRequestHttpException('Invalid ' . $format . ' message received', $e); } if (!is_array($data)) { throw new BadRequestHttpException('Invalid ' . $format . ' message received'); } $request->request->replace($data); }
/** * * Write configuration to file in given format * * @param $file */ public function writeToFile($file) { if ($this->getFormat() == 'yaml') { $yaml = new Yaml(); $serialized = $yaml->dump($this->getConfig(), 10); if ($serialized == 'null') { $serialized = '{}'; } } elseif ($this->getFormat() == 'json') { $encoder = new JsonEncoder(); $serialized = $encoder->encode($this->getConfig(), $encoder::FORMAT, ['json_encode_options' => JSON_PRETTY_PRINT]); } else { throw new InputOperationException("Invalid configuration format {$this->format}."); } $fs = new Filesystem(); $fs->dumpFile($file, $serialized); }
/** * @Route("/", name="admin_mainpage") * * @return string */ public function indexAction() { $mainMenuData = [['id' => "1", 'type' => "folder", 'value' => "Benutzer", 'css' => "folder_music"], ['id' => "2", 'type' => "folder", 'value' => "Lizenznehmer", 'css' => "folder_music"], ['id' => "3", 'type' => "folder", 'value' => "Themenfelder", 'css' => "folder_music"], ['id' => "4s", 'type' => "folder", 'value' => "Geschichten", 'css' => "folder_music"]]; $jsonEncoder = new JsonEncoder(); return $this->render('AdminBundle::index.html.twig', array('mainMenuData' => $jsonEncoder->encode($mainMenuData, 'json'))); }