/** * Automatically set global configuration if provided * * @param InitEvent $event */ public function onInit(InitEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); // For touristic object methods only if (in_array($operation->getName(), ['searchObject', 'searchObjectIdentifier', 'searchAgenda', 'searchAgendaIdentifier', 'searchDetailedAgendaIdentifier', 'searchDetailedAgenda'])) { $data = is_array($command['query']) ? $command['query'] : Utils::jsonDecode($command['query'], true); if (!empty($this->config['responseFields']) && !isset($data['responseFields'])) { $data['responseFields'] = $this->config['responseFields']; } if (!empty($this->config['locales']) && !isset($data['locales'])) { $data['locales'] = $this->config['locales']; } if (!empty($this->config['count']) && !isset($data['count'])) { $data['count'] = $this->config['count']; } $command['query'] = json_encode($data); } else { if ($operation->hasParam('locales') && !isset($command['locales'])) { $command['locales'] = implode(',', $this->config['locales']); } if ($operation->hasParam('responseFields') && !isset($command['responseFields'])) { $command['responseFields'] = implode(',', $this->config['responseFields']); } } }
public function json(array $config = []) { try { return Utils::jsonDecode((string) $this->getBody(), isset($config['object']) ? !$config['object'] : true, 512, isset($config['big_int_strings']) ? JSON_BIGINT_AS_STRING : 0); } catch (\InvalidArgumentException $e) { throw new ParseException($e->getMessage(), $this); } }
/** * {@inheritdoc} */ public function format($response) { try { $response = \GuzzleHttp\Utils::jsonDecode($response); return json_encode($response, JSON_PRETTY_PRINT); } catch (\Exception $e) { return $response; } }
/** * Creates a message object from the raw POST data * * @return Message * @throws \RuntimeException If the POST data is absent, or not a valid JSON document */ public static function fromRawPostData() { if (!isset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE'])) { throw new \RuntimeException('SNS message type header not provided.'); } $data = Utils::jsonDecode(file_get_contents('php://input'), true); if (!is_array($data)) { throw new \RuntimeException('POST data invalid'); } return self::fromArray($data); }
/** * Load the contents of the client session into the data array */ protected function load() { $cookieJar = isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : null; $data = Utils::jsonDecode($cookieJar, true); if (is_array($data)) { foreach ($data as $cookie) { $this->setCookie(new SetCookie($cookie)); } } elseif (strlen($data)) { throw new \RuntimeException("Invalid cookie data"); } }
public function testSetupFieldList() { $json = Utils::jsonDecode($this->json, true, 512, JSON_BIGINT_AS_STRING); $properties = $json['properties']; $fields = new FieldList($properties); $this->assertCount(3, $fields); $labels = []; foreach ($fields as $field) { $labels[] = $field->getLabel(); } $expectedLabels = ["string_1", "number_1", "checkbox_1"]; $this->assertEquals($expectedLabels, $labels); }
/** * Downloads, unzips, and reads a CloudTrail log file from Amazon S3 * * @param string $s3BucketName The bucket name of the log file in Amazon S3 * @param string $logFileKey The key of the log file in Amazon S3 * * @return array */ public function read($s3BucketName, $logFileKey) { // Create a command for getting the log file object $command = $this->s3Client->getCommand('GetObject', array('Bucket' => (string) $s3BucketName, 'Key' => (string) $logFileKey, 'ResponseContentEncoding' => 'x-gzip')); // Make sure gzip encoding header is sent and accepted in order to // inflate the response data. $command->getEmitter()->on('prepared', function (PreparedEvent $e) { $e->getRequest()->setHeader('Accept-Encoding', 'gzip'); }, RequestEvents::LATE); // Get the JSON response data and extract the log records $result = $this->s3Client->execute($command); $logData = Utils::jsonDecode($result['Body'], true); return isset($logData['Records']) ? $logData['Records'] : []; }
/** * Load cookies from a JSON formatted file. * * Old cookies are kept unless overwritten by newly loaded ones. * * @param string $filename Cookie file to load. * @throws \RuntimeException if the file cannot be loaded. */ public function load($filename) { $json = file_get_contents($filename); if (false === $json) { // @codeCoverageIgnoreStart throw new \RuntimeException("Unable to load file {$filename}"); // @codeCoverageIgnoreEnd } $data = Utils::jsonDecode($json, true); if (is_array($data)) { foreach (Utils::jsonDecode($json, true) as $cookie) { $this->setCookie(new SetCookie($cookie)); } } elseif (strlen($data)) { throw new \RuntimeException("Invalid cookie file: {$filename}"); } }
/** * Loads refreshable profile credentials if they are available, otherwise * returns null. * * @return RefreshableCredentials|null */ public function __invoke() { // Pass if the profile cannot be loaded or was not provided. if (!$this->profile) { try { $this->profile = $this->request('meta-data/iam/security-credentials/'); } catch (CredentialsException $e) { return null; } } return new RefreshableCredentials(function () { $response = $this->request("meta-data/iam/security-credentials/{$this->profile}"); $result = Utils::jsonDecode($response, true); if ($result['Code'] !== 'Success') { throw new CredentialsException('Unexpected instance profile response' . " code: {$result['Code']}"); } return new Credentials($result['AccessKeyId'], $result['SecretAccessKey'], $result['Token'], strtotime($result['Expiration'])); }); }
/** * Automatically set apiKey & projetId query parameters when needed * * @param InitEvent $event */ public function onInit(InitEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if ($operation->hasParam('apiKey') && !isset($command['apiKey'])) { $command['apiKey'] = $this->config['apiKey']; } if ($operation->hasParam('projetId') && !isset($command['projetId'])) { $command['projetId'] = $this->config['projectId']; } // Search operations use the authentication inside a query string JSON! if (in_array($operation->getName(), ['searchObject', 'searchObjectIdentifier', 'searchAgenda', 'searchAgendaIdentifier', 'searchDetailedAgendaIdentifier', 'searchDetailedAgenda', 'getReferenceCity', 'getReferenceElement', 'getReferenceInternalCriteria', 'getReferenceSelection'])) { $data = is_array($command['query']) ? $command['query'] : Utils::jsonDecode($command['query'], true); if (!isset($data['apiKey']) && !isset($data['projetId'])) { $data['apiKey'] = $this->config['apiKey']; $data['projetId'] = $this->config['projectId']; $command['query'] = json_encode($data); } } }
public function __construct(RequestException $e) { $this->request = $e->getRequest(); $this->response = $e->getResponse(); $simpleMessage = $e->getMessage(); $code = 0; if ($this->response) { try { $decodedJson = Utils::jsonDecode((string) $this->response->getBody(), true); if ($decodedJson && isset($decodedJson['errorType'])) { $simpleMessage = $decodedJson['errorType'] . ' ' . $decodedJson['message']; } } catch (\InvalidArgumentException $e) { // Not Json } $code = $this->response->getStatusCode(); } $responseDescription = $this->response ? (string) $this->response : 'none'; $requestDescription = $this->request ? (string) $this->request : 'none'; $message = sprintf("%s\n\nRequest: %s\n\nResponse: %s\n\n", $simpleMessage, $requestDescription, $responseDescription); parent::__construct($message, $code, $e); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON */ public function testJsonDecodesWithErrorMessages() { Utils::jsonDecode('!narf!'); }
/** * Load the file containing the API data for the given type/service/version. */ private function loadApiData($type, $service, $version) { // First check for PHP files, then fall back to JSON. $path = "{$this->modelsDir}/{$service}-{$version}.{$type}.php"; if (file_exists($path)) { return require $path; } $path = "{$this->modelsDir}/{$service}-{$version}.{$type}.json"; if (file_exists($path)) { return Utils::jsonDecode(file_get_contents($path), true); } return null; }
private function load($service, $version, $type) { // First check for PHP files, then fall back to JSON. $path = "{$this->path}/{$service}-{$version}.{$type}.php"; if (file_exists($path)) { /** @noinspection PhpIncludeInspection */ return require $path; } $path = "{$this->path}/{$service}-{$version}.{$type}.json"; if (file_exists($path)) { return Utils::jsonDecode(file_get_contents($path), true); } throw new \RuntimeException('Cannot load file: ' . $path); }