isJsonString() public static method

NOTICE: Since this does a json_decode to determine its validity you probably should consider, when using the variable content afterwards, just to decode and check for yourself instead of using this assertion.
public static isJsonString ( mixed $value, string | null $message = null, string | null $propertyPath = null ) : boolean
$value mixed
$message string | null
$propertyPath string | null
return boolean
 /**
  * @param string $jsonString
  * @return Result
  */
 public static function fromJSON($jsonString)
 {
     Assertion::isJsonString($jsonString);
     $itemJsonStrings = json_decode($jsonString, true);
     $items = array();
     foreach ($itemJsonStrings as $anItemJsonString) {
         $items[] = Item::fromJSON($anItemJsonString);
     }
     return new ItemCollection($items);
     $p = new Paginator();
 }
 /**
  * @param string $jsonString
  * @return LazyItemsLoader
  * @throws \RuntimeException
  */
 public static function fromJSON($jsonString)
 {
     if (is_null(static::$itemsLoaderManager)) {
         throw new \RuntimeException('ItemsLoaderManager is missing');
     }
     Assertion::isJsonString($jsonString);
     $anItemsLoaderName = json_decode($jsonString);
     Assertion::string($anItemsLoaderName);
     $anItemsLoader = static::$itemsLoaderManager->get($anItemsLoaderName);
     return new LazyItemsLoader($anItemsLoader);
 }
Exemplo n.º 3
0
 public function execute(Request $request, Response $response, callable $next = null)
 {
     $pathParams = $request->getAttribute('pathParams');
     $params = $request->getQueryParams();
     $body = $request->getBody();
     \Assert\Assertion::keyExists($pathParams, 'name');
     \Assert\Assertion::keyExists($pathParams, 'id');
     \Assert\Assertion::isJsonString($body);
     $collection = $pathParams['name'];
     $id = $pathParams['id'];
     $data = json_decode($body, true);
     $this->boot()->db->table($collection)->update($data, "`id` = {$id}");
     $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}");
     $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray());
     $response = $response->withBody($this->toStream($hal->asJson()));
     if ($next) {
         $response = $next($request, $response);
     }
     return $response;
 }
Exemplo n.º 4
0
 /**
  * @param string $requestMethod
  * @param string $path
  * @param array $options
  * @return array
  * @throws RequestException If the request fails
  * @throws ResponseException If the response does not contain valid json / the expected structure
  */
 public function request($requestMethod, $path, array $options = [])
 {
     $url = $this->buildRequestUrl($path);
     $options = $this->buildRequestConfiguration($options);
     try {
         $response = $this->client->request($requestMethod, $url, $options);
         if (!$response instanceof Response) {
             throw new \Exception('An error occurred while calling the API');
         }
         $content = $response->getBody()->getContents();
         Assertion::isJsonString($content);
         $content = json_decode($content, true);
         return $this->getResult($content);
     } catch (GuzzleException $exception) {
         throw new RequestException('An error occurred while calling the API');
     } catch (InvalidArgumentException $exception) {
         throw new ResponseException('The response did not contain valid JSON');
     } catch (\Exception $exception) {
         throw new RequestException('An error occurred while calling the API');
     }
 }
Exemplo n.º 5
0
 public function execute(Request $request, Response $response, callable $next = null)
 {
     try {
         $pathParams = $request->getAttribute('pathParams');
         \Assert\Assertion::keyExists($pathParams, 'name');
         $body = $request->getBody();
         \Assert\Assertion::isJsonString($body);
         $collection = $pathParams['name'];
         $data = json_decode($body, true);
         $id = $this->boot()->db->table($collection)->insert($data);
         $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}");
         $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray());
         $response = $response->withBody($this->toStream($hal->asJson()));
     } catch (\Exception $ex) {
         $problem = new \Crell\ApiProblem\ApiProblem($ex->getMessage(), $request->getUri()->getPath());
         $problem->setDetail($ex->getTraceAsString())->setStatus($ex->getCode());
         return $response->withStatus(500)->withBody($this->toStream($problem->asJson()));
     }
     if ($next) {
         $response = $next($request, $response);
     }
     return $response;
 }
Exemplo n.º 6
0
 /**
  * @dataProvider isJsonStringInvalidStringDataprovider
  */
 public function testIsJsonStringExpectingException($invalidString)
 {
     $this->setExpectedException('Assert\\AssertionFailedException', null, Assertion::INVALID_JSON_STRING);
     Assertion::isJsonString($invalidString);
 }
Exemplo n.º 7
0
 /**
  * @param string $jsonString
  * @return Item
  * @throws \Assert\AssertionFailedException
  */
 public static function fromJSON($jsonString)
 {
     Assertion::isJsonString($jsonString);
     return new Item(json_decode($jsonString, true));
 }
 /**
  * @param $argumentsString
  * @return Arguments
  */
 public function fromString($argumentsString)
 {
     Assertion::isJsonString($argumentsString, "ArgumentsString must be valid JSON Format");
     return new Arguments(json_decode($argumentsString, true));
 }